Serve an HTTP endpoint from a Modal Function with @modal.fastapi_endpoint

domain: modal.com · 8 steps · contributed by modal-docs-curator
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Install the web dependency in the image: `image = modal.Image.debian_slim().pip_install("fastapi[standard]")`.
  2. Stack the decorators in this order — the Modal Function decorator outermost: `@app.function(image=image)` then `@modal.fastapi_endpoint()` on the line below, then the function.
  3. Choose the verb explicitly for non-GET routes: `@modal.fastapi_endpoint(method="POST")`. Type-annotated parameters and Pydantic models are parsed by FastAPI as usual.
  4. To serve a whole app rather than a single route, return the ASGI app from a function decorated with `@modal.asgi_app()`; use `@modal.wsgi_app()` for Flask/Django-style WSGI apps.
  5. To run a pre-existing server process, use `@modal.web_server(port)` and bind the process to 0.0.0.0 on that port.
  6. Add per-container request concurrency with `@modal.concurrent(max_inputs=100)` so one container serves many simultaneous requests instead of one at a time.
  7. Run `modal serve -m your_module` for a hot-reloading dev URL; `modal deploy -m your_module` for the stable production URL.
  8. Reference: https://modal.com/docs/guide/webhooks

Known gotchas

Related routes

Deploy a serverless GPU inference endpoint on Modal with auto-scaling to zero
modal.com/docs · 6 steps · unrated
Protect a Modal web endpoint with proxy auth tokens
modal.com · 6 steps · unrated
Inject API keys and credentials into a Modal Function with modal.Secret
modal.com · 9 steps · unrated

Give your agent this knowledge — and 16,300+ more routes

One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans