client.send.message call emails the user and puts the result in their inbox.
first_send.py, then run it with your API key and email address:
courier-fastapi-quickstart
The finished FastAPI app from this guide, ready to clone and run.
Build this with a coding agent
To have a coding agent add Courier to your FastAPI app, paste this prompt into Claude Code, Cursor, or Codex from the project root:npx skills add trycourier/courier-skills, and connect the Courier MCP server so the agent can check Courier Logs and templates itself.
Rules for AI agents
- Use
trycourier(from courier import AsyncCourier), with one client created in themain.pylifespan. - Read
COURIER_API_KEYfrom the server’s environment, and never hard-code it. - Queue each send with
background_tasks.add_task, so the response returns first. - Pass one
idempotency_keyper event:welcome-<user_id>per saved user,agent-run-<run_id>-finishedper run. - Route a finished run to
["email", "inbox"]with"method": "all", for the user fromcurrent_user, never the request body. - Catch
CourierErrorinsend()and log it with the key, because the SDK already retried what Courier runs again.
What you’ll build
This FastAPI app has three routes, and each Courier send runs in a background task:- A welcome email from
POST /signup, which returns202right away - An email and an inbox message when a simulated agent run finishes
- A
GET /agent-runs/{run_id}route that returns the summary and Courier request id
Prerequisites
For the first send
- A Courier API key
- Python 3.12 or newer
For the full build
- A FastAPI project
Clone the finished FastAPI app
Clone the Courier FastAPI quickstart to run the finished app with your own key:Install the Courier Python SDK in FastAPI
The Courier Python SDK istrycourier on PyPI, and its async client, AsyncCourier, suits FastAPI. Add it to requirements.txt with FastAPI and python-dotenv:
requirements.txt
.env next to main.py with your key and address:
.env
Write the Courier send functions for FastAPI
notifications.py holds every Courier call in the FastAPI app. Each one is POST /send with an idempotency key, and inbox delivers to the Courier inbox of the user_id in to.
notifications.py
send() returns None after the SDK retries what Courier runs again.
Notify the user from FastAPI when an AI agent task finishes
In FastAPI,run_and_notify runs the agent in a background task, then notifies the user through Courier, so the request that starts the run returns at once.
agent.py
Queue Courier notifications from FastAPI routes with BackgroundTasks
main.py creates one AsyncCourier client in the FastAPI lifespan and hands it to each sending route with Depends. Both POST routes queue their send with BackgroundTasks and return 202.
main.py
current_user, which returns the demo user user_123, with your auth dependency.
Verify
Start one FastAPI agent run, then find its Courier notification in Logs. In the cloned repo,python demo.py starts a run, waits for it to finish, and prints the request id, with no server running.
In your own app, run fastapi dev main.py and start a run:
id to get Courier’s request id:
user_123.
Go to production with Courier
Courier is notification infrastructure for humans and AI agents. Go live moves your FastAPI app from Test to Production:- Switch keys. Set a Production
COURIER_API_KEYand connect your email provider there. - Move long runs to a worker. Run long agent tasks in a worker, such as Celery or arq, or a restart loses the run and its notification.
- Honor opt-outs. Map the notification to a subscription topic so users can mute agent updates in a preference center.
FAQ
How do I send email from FastAPI?
How do I send email from FastAPI?
Sending email from FastAPI takes one
await client.send.message(...) call on Courier’s AsyncCourier client. Courier delivers it through the provider you connect, such as SendGrid, Amazon SES, or Resend.Should I send notifications in a FastAPI background task?
Should I send notifications in a FastAPI background task?
A FastAPI background task suits a Courier send, because the response goes out first. For sends that must survive a restart, use a worker such as Celery or arq, which can retry with the same key.
How do I notify a user when an AI agent task finishes?
How do I notify a user when an AI agent task finishes?
Notifying a user when an AI agent task finishes takes one Courier send at the end of the task, routed to email and inbox with
"method": "all". Key it agent-run-<run_id>-finished so a retried task notifies once.Can I use this with Flask?
Can I use this with Flask?
Flask sends through Courier with the synchronous
Courier client from first_send.py and the same message and idempotency_key. A send in a Flask view waits for Courier, so queue it with Celery or RQ in production.