Launching Agents with RESTful APIs
Introduction: Why Expose Agents via APIs?
Think about how you use your favorite apps: checking email on your phone, responding from your laptop, or asking your smart speaker to read messages. Each device talks to the same service through an API. This is the power of decoupling — separating core logic from any single interface so it can be accessed from anywhere.
In this course, we'll put three of those factors into practice by building a real API layer around an AI agent. Specifically, this lesson kicks off Factor 11 — Trigger from anywhere, meet users where they are and expands Factor 6 — Launch / Pause / Resume with simple APIs by implementing the launch part.
We'll build a FastAPI server that exposes an AI agent through REST endpoints, making it accessible from web browsers, mobile apps, scripts, or any tool that can make HTTP requests.
Project Structure
As we transition from building standalone agent logic to exposing it via a web service, our project structure reflects a clear separation between the API layer and the core logic. You are already familiar with the core directory, which houses the agent's decision-making logic, tools, and models. We now introduce a server directory to handle web requests and a top-level test.py script to simulate a client:
src/core/: Contains theAgentclass andStatemodels you've used in previous lessons.src/server/main.py: This is where you will write your FastAPI code. It imports the agent fromcoreand exposes it through REST endpoints.test.py: A standalone script that uses therequestslibrary to "talk" to your server, allowing you to test the full lifecycle of an agent task.
This separation is central to Factor 11: your agent logic remains independent of how it is triggered — whether it's via a CLI, a web API, a Slack bot, or a cron job. The server directory is just one thin adapter around a stable core.
Setting Up FastAPI
FastAPI is a modern Python web framework designed for building APIs quickly and efficiently. It uses Python type hints to validate data automatically and generates interactive documentation for your endpoints. To get started, you create an instance of the FastAPI application:
This single line creates your API application. The app object will be used to define routes using Python decorators. If you were installing FastAPI locally, you would run pip install fastapi uvicorn in your terminal. To start the server locally, you would run:
This tells uvicorn to load the app object from your script and serve it, usually on http://localhost:8000. The --reload flag automatically restarts the server whenever you change your code. On CodeSignal, this environment is already set up for you.
