Does FastAPI Work With Cloudflare Pages?
FastAPI cannot run directly on Cloudflare Pages, but you can deploy FastAPI to Cloudflare Workers or use Pages for the frontend with FastAPI on a separate backend.
Quick Facts
How FastAPI Works With Cloudflare Pages
Cloudflare Pages is a static site hosting platform optimized for JAMstack architectures—it doesn't support running Python runtimes directly. However, you have two practical approaches: First, deploy FastAPI to Cloudflare Workers using a Python-to-JavaScript transpiler or run it on a separate backend (Heroku, Railway, etc.) and connect from a Pages frontend via API calls. Second, use Wrangler with Workers to create a lightweight API layer that proxies to your FastAPI backend. The most common pattern is hosting a static frontend (React, Vue, etc.) on Pages and FastAPI on Workers or an external server, connected via CORS-enabled API calls. This architecture actually works well because Pages' global CDN excels at serving frontend assets while your FastAPI instance handles business logic separately. Developers typically use this when they want Pages' simplicity for static assets but need Python's data science or async capabilities for APIs.
Best Use Cases
FastAPI Backend + Pages Frontend Setup
pip install fastapi uvicorn python-multipart# FastAPI backend (deploy to Workers or external server)
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://yoursite.pages.dev"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/data")
async def get_data():
return {"message": "Hello from FastAPI"}
@app.post("/api/submit")
async def submit_data(data: dict):
return {"received": data}
# Run with: uvicorn main:app --host 0.0.0.0 --port 8000Known Issues & Gotchas
Pages cannot execute Python code—you cannot deploy FastAPI directly
Fix: Deploy FastAPI to Workers, Railway, Fly.io, or another backend service. Use Pages only for static assets and frontend code.
CORS errors when calling FastAPI from Pages frontend due to different origins
Fix: Configure FastAPI with CORSMiddleware to accept requests from your Pages domain, or use Cloudflare Workers as an API proxy.
Cold starts on serverless FastAPI deployments can impact user experience
Fix: Use Workers for lightweight APIs or keep-alive services. Consider Railway or Fly.io for always-on instances.
Workers have CPU time limits (50ms for free tier) that may timeout complex FastAPI logic
Fix: Use Workers as a proxy/gateway and offload heavy processing to a dedicated backend server.
Alternatives
- •Next.js on Vercel with FastAPI backend—better Python support and SSR capabilities
- •Hugo/static site on Pages with Lambda functions for API logic—easier serverless setup
- •Remix on Cloudflare Pages with Workers for full-stack—native Cloudflare integration
Resources
Related Compatibility Guides
Explore more compatibility guides