G
YouTube TikTok Games
Skip to content

Deploy to any platform

Prerequisites: make sure you’ve completed the Get Started guide.

This page shows one way to deploy a Python Genkit app to any platform: run a FastAPI server that calls your flows. (FastAPI is ASGI and works well on most Python hosting providers.)

Terminal window
mkdir -p ~/tmp/genkit-plain-fastapi
cd ~/tmp/genkit-plain-fastapi
uv init
uv add genkit genkit-google-genai fastapi uvicorn

Create flows.py:

flows.py
from genkit import Genkit
from genkit_google_genai import GoogleAI
ai = Genkit(
plugins=[GoogleAI()],
model=GoogleAI.gemini('gemini-flash-latest'),
)
@ai.flow()
async def joke_flow(topic: str) -> str:
response = await ai.generate(
prompt=f'Tell a medium-sized joke about {topic}',
)
if response.error:
raise RuntimeError(f'Joke generation failed: {response.error.message}')
return response.text

Create main.py:

main.py
import hmac
import os
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from pydantic import BaseModel
from flows import joke_flow
app = FastAPI()
security = HTTPBearer(auto_error=False)
FLOW_API_KEY = os.environ['FLOW_API_KEY']
class JokeRequest(BaseModel):
topic: str
async def require_auth(
credentials: HTTPAuthorizationCredentials | None = Depends(security),
) -> None:
# Replace with your JWT/OIDC verification, or gate at your platform proxy.
if credentials is None or not hmac.compare_digest(credentials.credentials, FLOW_API_KEY):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail='not authorized',
)
@app.get('/health')
async def health() -> dict[str, str]:
return {'status': 'ok'}
@app.post('/joke')
async def joke(request: JokeRequest, _auth: None = Depends(require_auth)) -> dict[str, str]:
text = await joke_flow(request.topic)
return {'joke': text}
Terminal window
export GEMINI_API_KEY=<your API key>
export FLOW_API_KEY=<your flow API key>
uv run uvicorn main:app --reload --port 8000

Or with the Developer UI:

Terminal window
export GEMINI_API_KEY=<your API key>
export FLOW_API_KEY=<your flow API key>
genkit start -- uv run uvicorn main:app --reload --port 8000

Invoke the endpoint:

Terminal window
curl -X POST "http://127.0.0.1:8000/joke" \
-H "Authorization: Bearer $FLOW_API_KEY" \
-H "Content-Type: application/json" \
-d '{"topic":"bananas"}'

Configure the following settings on your hosting provider:

  • Runtime: Python 3.10+
  • Start command: uv run uvicorn main:app --host 0.0.0.0 --port ${PORT:-8000}
  • Environment variables: set GEMINI_API_KEY and FLOW_API_KEY via your platform’s secret manager. Leave GENKIT_ENV and GENKIT_REFLECTION_ENABLED unset so the Developer UI reflection server stays off in production.

If you serve flows with genkit-fastapi, genkit-flask, genkit-django, or genkit.web instead, see Authorization and integrity to authenticate callers.