Skip to main content
OpenAPI callbacks allow you to document scenarios where your API makes requests to external APIs provided by your users. This is useful when your API needs to notify external systems or trigger actions on user-provided endpoints.

What Are Callbacks?

A callback occurs when:
  1. A user sends a request to your API
  2. Your API processes the request
  3. Your API sends a request to an external API (provided by the user)
  4. The external API processes and responds
Callbacks are essentially “your API calling their API” - documenting what the external API should look like to receive your requests.

Callback Use Case Example

Imagine you’re building an invoice processing API:
  1. External developers create invoices through your API
  2. Your API sends invoices to customers
  3. Your API collects payment
  4. Your API notifies the external developer by making a POST request to their API (callback)

Creating an API with Callbacks

Let’s build a complete example showing how to document callbacks.

Define Data Models

First, define the models for invoices and callback events:
The HttpUrl type from Pydantic validates that the callback URL is properly formatted.

Create a Callback Router

Create an APIRouter specifically for documenting the callback:

Understanding the Callback Path

The callback path uses OpenAPI expressions:
This expression:
  • {$callback_url}: References the callback_url query parameter from the original request
  • {$request.body.id}: References the id field from the request body
OpenAPI 3 expressions allow you to construct dynamic URLs using data from the original request.

Add the Callback to Your Endpoint

Use the callbacks parameter to attach the callback documentation:
Note that you pass invoices_callback_router.routes (the .routes attribute), not the router itself.

How Callbacks Work in Practice

Here’s a real-world example flow:

1. User Sends Request to Your API

2. Your API Processes and Calls Back

After processing, your API makes a callback request:

3. External API Responds

The external API (implemented by your user) responds:

Implementing the Actual Callback

The callback router only documents the callback. You still need to implement the actual HTTP request:
Use BackgroundTasks to send callbacks asynchronously without blocking the response to the user.

Multiple Callbacks

You can document multiple callbacks for different events:

OpenAPI Path Expressions

Callbacks support various OpenAPI 3 expressions:

Query Parameter Reference

Header Reference

Body Field Reference

Multiple Parameters

Viewing Callback Documentation

Once implemented, your callbacks appear in the API documentation:
  1. Start your application: uvicorn main:app --reload
  2. Open the docs: http://127.0.0.1:8000/docs
  3. Find your endpoint and expand the “Callbacks” section
The documentation shows external developers exactly how to structure their API to receive your callbacks.

Best Practices

Document what you send: Focus on documenting the requests your API makes, not how to implement your callback logic.
  • Use clear naming: Name your callbacks descriptively (e.g., payment-completed, invoice-sent)
  • Include authentication: Document required headers or authentication for callbacks
  • Handle failures gracefully: Implement retries and error handling for callback requests
  • Use background tasks: Don’t block user requests while sending callbacks
  • Timeout appropriately: Set reasonable timeouts for callback requests
  • Version your callbacks: Include version info if your callback format may change

Complete Example