Skip to main content
Add email and in-app notifications to a FastAPI app by queuing Courier sends in background tasks. When an AI agent task finishes, one client.send.message call emails the user and puts the result in their inbox.
Save it as first_send.py, then run it with your API key and email address:
It prints a request id to find in Courier Logs. On a Test key, send to your Courier login address, which test email reaches with no provider connected.

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:
Install the Courier skill with 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 the main.py lifespan.
  • Read COURIER_API_KEY from 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_key per event: welcome-<user_id> per saved user, agent-run-<run_id>-finished per run.
  • Route a finished run to ["email", "inbox"] with "method": "all", for the user from current_user, never the request body.
  • Catch CourierError in send() 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 returns 202 right 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
Tested in November 2026 with FastAPI 0.165, trycourier 10.2.0, and Python 3.12.

Prerequisites

For the first send

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:
The steps below add or change these files:

Install the Courier Python SDK in FastAPI

The Courier Python SDK is trycourier on PyPI, and its async client, AsyncCourier, suits FastAPI. Add it to requirements.txt with FastAPI and python-dotenv:
requirements.txt
Install them in a virtualenv:
Create .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
A failed send in a background task is logged, because no caller is waiting for it. 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
Take the user from your session, never from the request. Replace 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:
After about five seconds, read the run back by its id to get Courier’s request id:
Open the request id in Logs to see an email and an inbox delivery. To show the inbox in your app, add an inbox signed in as 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_KEY and 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

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.
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.
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.
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.