FastAPI Integration
Serving your agencies and tools as APIs with FastAPI.
Agency Swarm supports serving your agencies and tools as production-ready HTTP APIs using FastAPI. This enables you to interact with your agents and tools over HTTP, integrate with other services, or connect it to web frontends.
Installation
FastAPI integration is an optional installation. To install all required dependencies, run:
Setting Up FastAPI Endpoints
You can expose your agencies and tools as API endpoints using the run_fastapi()
function.
Example: Create an api endpoint for a single agency using instance method
Optionally, you can specify following parameters:
- host (default:
"0.0.0.0"
) - port (default:
8000
) - app_token_env (default:
"APP_TOKEN"
) - Name of the env variable storing app token.
This will create 2 endpoints for the agency:
/test_agency/get_completion
/test_agency/get_completion_stream
Both of these endpoints will accept following input parameters:
Additionally, you will need to provide a bearer token in the authorization if you have "APP_TOKEN"
specified (or a differently named variable if you provided app_token_env). If the token is not specified in the env variables, authentication will be disabled.
Example: Serving Multiple Agencies and Tools
You can deploy multiple agencies and tools in a single function call by using run_fastapi function from the integrations directory
This will create 6 following endpoints:
/test_agency_1/get_completion
/test_agency_1/get_completion_stream
/test_agency_2/get_completion
/test_agency_2/get_completion_stream
/tool/ExampleTool
/tool/TestTool
Inputs for the tool endpoints will follow their pydantic schemas respectively.
API Usage Example
You can interact with your agents and tools using HTTP requests. Here’s an example using Python’s requests
library:
Endpoint Structure
-
Agency Endpoints:
Each agency is served at:/your_agency_name/get_completion
(POST)/your_agency_name/get_completion_stream
(POST, streaming responses)
-
Tool Endpoints:
Each tool is served at:/tool/ToolClassName
(POST)