Just a few simple steps to get started with Apitally.
This page guides you through the steps of configuring your FastAPI application to work with Apitally. If you don’t have an account yet, now would be a good time to sign up.Once you’re done with this guide, you will be able to:
Get detailed metrics on API usage, errors, and performance
Track API adoption and usage by individual consumers
Log individual API requests, responses, and correlated application logs
See what’s causing slow API requests with traces
Monitor uptime and set up custom alerts
Running FastAPI on Cloudflare Workers? Follow this setup guide instead.
To get started, create a new app in the Apitally dashboard and select as your framework.You can also configure the environments (e.g. prod and dev) for your app, or simply accept the defaults.After submitting, you will see tailored setup instructions for your app. These include code snippets you can copy and paste into your project.
The client ID provided in the setup instructions uniquely identifies your app for the purpose of data ingestion only. It does not grant any kind of read access to your data.
Next, install the Apitally SDK in your project with the extra.
pip install "apitally[fastapi]"
poetry add "apitally[fastapi]"
uv add "apitally[fastapi]"
Add the Apitally middleware to your FastAPI application and provide the client_id for your app.
You’ll find the client_id on the Setup instructions page for your app in the Apitally dashboard, which is displayed immediately after creating the app.
from fastapi import FastAPIfrom apitally.fastapi import ApitallyMiddlewareapp = FastAPI()app.add_middleware( ApitallyMiddleware, client_id="your-client-id", env="dev", # or "prod" etc.)
If you’re also using other middlewares, add the ApitallyMiddleware last, so
that it wraps the existing stack of middlewares.
Deploy your application with these changes, or restart if you’re testing locally.
At this point the basic setup for your application is complete and you will start seeing data in the Apitally dashboard.
To analyze and filter API traffic by consumers, you can associate requests with consumer identifiers in your application.In most cases, use the authenticated identity to identify the consumer.
The identifier should be a string, such as a username, email address, or any other unique identifier.Optionally, you can also provide a display name and group for each consumer.Use the set_consumer function to associate requests with consumers, for example in a dependency, middleware or directly in your endpoint functions.
Logging of individual requests and responses is disabled by default to protect potentially sensitive data.
If you enable it, you can configure in detail what parts of the request and response should be logged.
You can also mask sensitive information (e.g. in headers) and exclude certain requests from logging.The SDK applies default masking rules for common sensitive headers, query parameters and request/response body fields.
from fastapi import FastAPIfrom apitally.fastapi import ApitallyMiddlewareapp = FastAPI()app.add_middleware( ApitallyMiddleware, client_id="your-client-id", env="dev", # or "prod" etc. enable_request_logging=True, log_request_headers=True, log_request_body=True, log_response_body=True, capture_logs=True, capture_traces=False, # requires instrumentation)
from fastapi import FastAPIfrom apitally.fastapi import ApitallyMiddlewareapp = FastAPI()app.add_middleware( ApitallyMiddleware, client_id="your-client-id", env="dev", # or "prod" etc. enable_request_logging=True, log_query_params=True, log_request_headers=True, log_request_body=True, log_response_headers=True, log_response_body=True, log_exception=True, capture_logs=True, capture_traces=False, # requires instrumentation # Mask query parameters using regex mask_query_params=[r"^card_number$"], # Mask headers using regex mask_headers=[r"^X-Sensitive-Header$"], # Mask request/response body fields using regex mask_body_fields=[r"^sensitive_field$"], # Mask request/response body with custom logic via callbacks (e.g. for admin endpoints) # The callback should return None to mask the whole body, or return the (modified) raw body mask_request_body_callback=lambda request: None if request["path"].startswith("/admin/") else request["body"], mask_response_body_callback=lambda request, response: None if request["path"].startswith("/admin/") else response["body"], # Exclude paths from request logging using regex (common health check paths are excluded by default) exclude_paths=[r"/metrics$"], # Exclude requests from logging with custom logic via callback (e.g. exclude requests from a specific consumer) # The callback should return True to exclude the request exclude_callback=lambda request, response: request["consumer"] == "some-consumer",)
The Request logs dashboard now shows individual requests handled by your application. You can filter, search, and inspect them in detail.
Tracing gives you a detailed breakdown of time spent during the handling of each request, showing the duration of database queries, HTTP calls, and other operations.To enable tracing, set capture_traces to True and instrument the libraries you want to trace with OpenTelemetry. See the tracing guide for further instructions.