FastAPI Integration¶
Honey Badgeria includes a FastAPI integration that automatically generates REST API endpoints from your flow definitions.
Setup¶
Install the FastAPI extra:
Scaffold a FastAPI project:
HoneyBadgeriaApp¶
The HoneyBadgeriaApp wraps a FastAPI application with auto-generated routes from your graph:
from honey_badgeria.integrations.fastapi import HoneyBadgeriaApp
app = HoneyBadgeriaApp(
graph=my_graph,
handlers={
"create_user": create_user_fn,
"validate": validate_fn,
"save": save_fn,
},
prefix="/api/v1",
)
app is a standard FastAPI instance — use it with uvicorn as usual:
Auto-Generated Routes¶
For each flow in your graph, HBIA generates a POST endpoint:
The request body is passed as initial_data to the flow execution. The response contains all vertex outputs.
Response Helpers¶
HBIA provides standardized response constructors:
from honey_badgeria.integrations.fastapi import (
hbia_success,
hbia_error,
hbia_paginated,
)
# Success response
return hbia_success(data={"user_id": 42}, status_code=200)
# Error response
return hbia_error(message="User not found", status_code=404)
# Paginated response
return hbia_paginated(
items=users,
total=100,
page=1,
size=20,
)
Example: User CRUD API¶
Flow Definition¶
# flows/create_user.yaml
flow:
create_user:
validate_input:
handler: vertices.users.validate_input
effect: pure
version: "1"
inputs:
username: str
email: str
outputs:
is_valid: bool
errors: list
next:
- save_user
save_user:
handler: vertices.users.save_user
effect: side_effect
version: "1"
inputs:
username: validate_input.username
email: validate_input.email
is_valid: validate_input.is_valid
outputs:
user_id: str
saved: bool
Handlers¶
# vertices/users.py
def validate_input(username: str, email: str) -> dict:
errors = []
if not username or len(username) < 3:
errors.append("Username must be at least 3 characters")
if not email or "@" not in email:
errors.append("Invalid email")
return {
"is_valid": len(errors) == 0,
"errors": errors,
"username": username,
"email": email,
}
def save_user(username: str, email: str, is_valid: bool) -> dict:
if not is_valid:
return {"user_id": None, "saved": False}
# In a real app, this saves to a database
user_id = f"user_{username}"
return {"user_id": user_id, "saved": True}
Application¶
# app.py
from honey_badgeria.back.dsl import load_graph_from_yaml
from honey_badgeria.integrations.fastapi import HoneyBadgeriaApp
from vertices.users import validate_input, save_user
graph = load_graph_from_yaml("flows/create_user.yaml")
app = HoneyBadgeriaApp(
graph=graph,
handlers={
"validate_input": validate_input,
"save_user": save_user,
},
prefix="/api/v1",
)
Run¶
Test¶
curl -X POST http://localhost:8000/api/v1/flows/create_user \
-H "Content-Type: application/json" \
-d '{"username": "alice", "email": "alice@example.com"}'
Configuration¶
The API prefix, max page size, and other settings are controlled via the Settings object:
from honey_badgeria.conf import Settings, configure
configure(Settings(
API_PREFIX="/api/v1",
MAX_PAGE_SIZE=100,
))
Or via environment variables: