Skip to main content
This guide takes you from zero to a running REST API that exposes your Swarms agents over HTTP. FastAPI handles routing, Pydantic validates requests, and Uvicorn serves the app.

Step 1: Install dependencies

Step 2: Create the API

Save the following as agent_api.py:

Step 3: Run it

Or with uvicorn’s reload mode for development:
The server is now reachable at:
  • API: http://localhost:8000
  • Docs: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

Step 4: Call it

curl

Python

Step 5: Add a specialised endpoint

When you want a dedicated endpoint for a specific agent persona, just create the agent inline:

Step 6: Production hardening

Agent factory

Centralise agent configs so endpoints stay thin:

Auth + rate limiting

Gunicorn for multi-worker production

Docker

docker-compose

Best practices

Troubleshooting

  • Port in use — change --port or kill the existing process.
  • Agent init fails — check API keys and model name; the error message usually points at the missing env var.
  • OOM — drop max_loops, or stream the response (see Agent Streaming).
  • Timeouts — long agent runs need higher proxy/uvicorn timeouts; consider returning a job id and polling for results.

See also