Skip to content

FastAPI Integration

FastAPI is a modern, high-performance Python web framework that’s perfect for building REST APIs with Genkit. This guide shows you how to integrate Genkit flows with FastAPI applications.

You should be familiar with Genkit’s concept of flows, and how to write them.

Create a new Python project with uv:

Terminal window
uv init my-genkit-app
cd my-genkit-app

Install FastAPI and Genkit dependencies:

Terminal window
uv add fastapi uvicorn genkit genkit-plugin-google-genai

Create a file for your Genkit flows, for example flows.py:

from genkit import Genkit
from genkit.plugins.google_genai import GoogleAI
# Initialize Genkit
ai = Genkit(
plugins=[GoogleAI()],
model='googleai/gemini-2.5-flash',
)
@ai.flow()
async def menu_suggestion_flow(theme: str) -> str:
"""Generate a menu item for a themed restaurant.
Args:
theme: The restaurant theme.
Returns:
A suggested menu item.
"""
response = await ai.generate(
prompt=f'Invent a menu item for a {theme} themed restaurant.',
)
return response.text

Create your FastAPI application and expose your flows as API endpoints:

from fastapi import FastAPI
from pydantic import BaseModel
from flows import menu_suggestion_flow
app = FastAPI()
class MenuRequest(BaseModel):
theme: str
@app.post("/menuSuggestion")
async def menu_suggestion(request: MenuRequest):
"""Generate a menu suggestion."""
result = await menu_suggestion_flow(request.theme)
return {"menuItem": result}

You can call your FastAPI endpoints using any HTTP client:

import requests
response = requests.post(
'http://localhost:8000/menuSuggestion',
json={'theme': 'pirate'}
)
print(response.json())

Add authentication to your API routes using FastAPI’s dependency injection:

from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
security = HTTPBearer()
async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)) -> dict:
"""Verify JWT token."""
token = credentials.credentials
# Add your token verification logic here
# Return user info or raise HTTPException
return {"user_id": "123", "email": "user@example.com"}
@app.post("/menuSuggestion")
async def protected_menu_suggestion(
request: MenuRequest,
user: dict = Depends(verify_token)
):
"""Generate a menu suggestion with authentication."""
result = await menu_suggestion_flow(request.theme)
return {"menuItem": result, "user": user["email"]}
Terminal window
uv run uvicorn main:app --reload
Terminal window
genkit start -- uv run uvicorn main:app --reload

Set up credentials for your model provider:

Gemini (Google AI)

Terminal window
export GEMINI_API_KEY=<your API key>
uv run uvicorn main:app --reload

Gemini (Vertex AI)

Terminal window
export GOOGLE_CLOUD_PROJECT=<your project ID>
export GOOGLE_CLOUD_LOCATION=us-central1
gcloud auth application-default login
uv run uvicorn main:app --reload

When you deploy your app, you’ll need to make credentials available. See:

FastAPI automatically generates interactive API documentation:

  • Swagger UI: Visit http://localhost:8000/docs
  • ReDoc: Visit http://localhost:8000/redoc