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.)
1. Set up your project
Section titled “1. Set up your project”mkdir -p ~/tmp/genkit-plain-fastapicd ~/tmp/genkit-plain-fastapi
uv inituv add genkit genkit-google-genai fastapi uvicorn2. Configure your Genkit app
Section titled “2. Configure your Genkit app”Create flows.py:
from genkit import Genkitfrom 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.text3. Create a web server
Section titled “3. Create a web server”Create main.py:
import hmacimport os
from fastapi import Depends, FastAPI, HTTPException, statusfrom fastapi.security import HTTPAuthorizationCredentials, HTTPBearerfrom 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}4. Run the app
Section titled “4. Run the app”export GEMINI_API_KEY=<your API key>export FLOW_API_KEY=<your flow API key>uv run uvicorn main:app --reload --port 8000Or with the Developer UI:
export GEMINI_API_KEY=<your API key>export FLOW_API_KEY=<your flow API key>genkit start -- uv run uvicorn main:app --reload --port 8000Invoke the endpoint:
curl -X POST "http://127.0.0.1:8000/joke" \ -H "Authorization: Bearer $FLOW_API_KEY" \ -H "Content-Type: application/json" \ -d '{"topic":"bananas"}'5. Deploy the project
Section titled “5. Deploy the project”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_KEYandFLOW_API_KEYvia your platform’s secret manager. LeaveGENKIT_ENVandGENKIT_REFLECTION_ENABLEDunset 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.