Does Flask Work With Cloudflare Pages?

Works With WorkaroundsLast verified: 2026-02-20

Flask cannot run directly on Cloudflare Pages, but you can deploy Flask as a serverless function via Cloudflare Workers or use Pages Functions as a lightweight alternative.

Quick Facts

Compatibility
workaround
Setup Difficulty
Moderate
Official Integration
No — community maintained
Confidence
medium
Minimum Versions
Flask: 2.0.0

How Flask Works With Cloudflare Pages

Cloudflare Pages is a JAMstack platform optimized for static site generation and edge functions, not traditional WSGI server applications like Flask. Flask requires a persistent runtime environment (ASGI/WSGI server), which Pages doesn't provide natively. However, you have two viable paths: (1) Deploy Flask to Cloudflare Workers using a WSGI-to-fetch adapter, though this has cold start overhead and memory constraints; (2) Use Cloudflare Pages Functions (lightweight serverless functions) to replace Flask routes entirely, leveraging Cloudflare's edge network without the Flask framework overhead. The first approach works if you have an existing Flask codebase and need minimal changes, while the second requires rewriting your application logic. Most developers find the Pages Functions approach more aligned with Cloudflare's architecture, offering better performance and cost efficiency. Neither solution is ideal for heavy Flask applications with complex routing, middleware stacks, or database ORM operations.

Best Use Cases

Migrating a simple Flask API to serverless while maintaining some Flask code
Building a hybrid setup with Pages Functions handling API routes and Flask handling background jobs
Prototyping a Flask app and testing it in a serverless environment before full migration
Using Flask locally during development, then deploying equivalent logic as Pages Functions to production

Flask Logic as Cloudflare Pages Function

bash
pip install flask
python
# functions/api/[[route]].py
from flask import Flask, Request, jsonify

app = Flask(__name__)

@app.route('/api/hello', methods=['GET'])
def hello():
    return jsonify({"message": "Hello from Cloudflare Pages!"})

@app.route('/api/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
    return jsonify({"id": user_id, "name": "User"})

# For Pages Functions, export as serverless handler
def on_request(request):
    with app.test_request_context(request.url, method=request.method):
        return app.full_dispatch_request()

# Alternative: Use Pages Functions directly (recommended)
# export default {
#   async fetch(request) {
#     const url = new URL(request.url);
#     if (url.pathname === '/api/hello') {
#       return new Response(JSON.stringify({message: 'Hello'}));
#     }
#     return new Response('Not found', {status: 404});
#   }
# }

Known Issues & Gotchas

warning

Cold starts and execution timeouts on Workers/Functions

Fix: Keep functions small and lightweight. Move heavy computation to background jobs or external services. Minimize dependencies.

critical

No persistent filesystem; WSGI servers won't work as expected

Fix: Refactor to stateless architecture. Use Cloudflare KV, Durable Objects, or external databases for state management instead of in-memory caches.

warning

Flask's synchronous nature conflicts with serverless event-driven model

Fix: Consider async frameworks (FastAPI, Quart) if rewriting, or stick with simple route-based Flask apps that translate easily to Functions.

info

Dependency size limits; Python packages bloat Workers bundle

Fix: Minimize Flask imports, use lightweight alternatives like Starlite, or deploy via Workers with pre-built Python runtimes.

Alternatives

  • FastAPI + Cloudflare Workers (better async support, modern Python framework)
  • Next.js API Routes + Vercel (tighter JAMstack integration with full Node.js runtime)
  • Express.js + Cloudflare Workers (JavaScript ecosystem, easier serverless conversion)

Resources

Related Compatibility Guides

Explore more compatibility guides