API monitoring & analytics made simple
Metrics, logs, traces, and alerts for your APIs — with just a few lines of code.

Apitally is a simple API monitoring and analytics tool that makes it easy to understand API usage, monitor performance, and troubleshoot issues. Get started in minutes by just adding a few lines of code. No infrastructure changes required, no dashboards to build.
The SDK is an OpenTelemetry distribution and works alongside an existing OpenTelemetry setup.
Learn more about Apitally on our 🌎 website or check out the 📚 documentation.
Important
Upgrading from 0.x? Version 1.0 is a full rewrite with a new setup API. See the migration guide for how to upgrade.
- API analytics: Traffic, error and performance metrics for your API, each endpoint, and per API consumer. Drill down from metrics to individual API requests.
- Request logs and traces: Every request as a searchable log entry, with optional capture of headers and request/response bodies. Requests are exported as OpenTelemetry spans, including spans from any other instrumentations you have.
- Application logs: Logs written via the standard
loggingmodule and Loguru are captured automatically and correlated with the requests they belong to. - Error tracking: Validation errors and exceptions with stack traces for server errors, automatically linked to Sentry events if you use Sentry.
- Server metrics: CPU and memory usage of your app's processes.
- API monitoring & alerts: Get notified if something isn't right using custom alerts, synthetic uptime checks and heartbeat monitoring. Alert notifications can be delivered via email, Slack and Microsoft Teams.
The SDK supports Python >= 3.10.
| Framework | Supported versions | Setup guide |
|---|---|---|
| FastAPI * | >=0.108.0 |
Link |
| Django REST Framework | >=3.12.0 |
Link |
| Django Ninja | >=1.0.0 |
Link |
| Flask | >=2.0.0 |
Link |
| Starlette | >=0.29.0 |
Link |
| Litestar | >=2.24.0 |
Link |
| BlackSheep | >=2.6.1 |
Link |
* For FastAPI on Cloudflare Workers use our Python Serverless SDK instead.
Apitally also supports many other web frameworks in JavaScript, Go and .NET via our other SDKs.
If you don't have an Apitally account yet, first sign up here. Then create an app in the Apitally dashboard. You'll see detailed setup instructions with code snippets you can copy and paste. These also include your write token.
Install the SDK with the fastapi extra:
pip install "apitally[fastapi]"Then call apitally.init() right after creating the app:
import apitally
from fastapi import FastAPI
app = FastAPI()
apitally.init(app, write_token="your-write-token", env="dev")For further instructions, see our setup guide for FastAPI.
Install the SDK with the django extra:
pip install "apitally[django]"Then call apitally.init() at the end of your settings.py module, after MIDDLEWARE is defined:
# settings.py
import apitally
MIDDLEWARE = [
# Your middleware ...
]
# ... at the very end of the file:
apitally.init(write_token="your-write-token", env="dev")For further instructions, see our setup guide for Django.
Install the SDK with the flask extra:
pip install "apitally[flask]"Then call apitally.init() after creating the app and after wrapping app.wsgi_app with any WSGI middleware:
import apitally
from flask import Flask
app = Flask(__name__)
apitally.init(app, write_token="your-write-token", env="dev")For further instructions, see our setup guide for Flask.
Install the SDK with the starlette extra:
pip install "apitally[starlette]"Then call apitally.init() right after creating the app:
import apitally
from starlette.applications import Starlette
app = Starlette(routes=[...])
apitally.init(app, write_token="your-write-token", env="dev")For further instructions, see our setup guide for Starlette.
Install the SDK with the litestar extra:
pip install "apitally[litestar]"Then add ApitallyPlugin to your app's plugins:
from litestar import Litestar
from apitally.litestar import ApitallyPlugin
app = Litestar(
route_handlers=[...],
plugins=[ApitallyPlugin(write_token="your-write-token", env="dev")],
)For further instructions, see our setup guide for Litestar.
Install the SDK with the blacksheep extra:
pip install "apitally[blacksheep]"Then call apitally.init() right after creating the app:
import apitally
from blacksheep import Application
app = Application()
apitally.init(app, write_token="your-write-token", env="dev")For further instructions, see our setup guide for BlackSheep.
Pass configuration options as keyword arguments to apitally.init(), or to ApitallyPlugin for Litestar. See the SDK reference for all available options.
To keep configuration out of your code, set these environment variables instead of passing the matching arguments:
APITALLY_WRITE_TOKEN: the write token (write_token)APITALLY_ENV: the environment name (env), defaults todevAPITALLY_DISABLED: set totrueto disable Apitally (disabled)
Apitally captures response headers by default. To also capture request headers and request and response bodies, pass these arguments:
apitally.init(
app,
write_token="your-write-token",
env="dev",
capture_request_headers=True,
capture_request_body=True,
capture_response_body=True,
)Apitally automatically masks common sensitive values, such as password fields and the Authorization header. To mask additional values, pass regular expressions to mask_query_params, mask_headers, and mask_body_fields. See our documentation on masking for details.
Apitally automatically excludes common health checks and static assets, such as /healthz and /robots.txt, from logs and traces. To exclude additional paths, pass regular expressions to exclude_paths.
To capture logs and traces for only a fraction of the remaining requests, set sample_rate, for example to 0.1 for 10%. To decide for each request individually, pass a callback to sample_on_request or sample_on_response.
Metrics always include all requests. See our documentation on sampling for details.
Apitally captures logs written with the standard logging module and Loguru, and correlates them with the requests they belong to. To mask or drop individual log records, pass a callback to mask_log_record. To turn off log capture, set capture_logs=False.
See our documentation on masking for details.
The following functions add context to the current request. Call them while handling a request, for example in an endpoint, middleware, or authentication code.
To associate the request with the API consumer making it, call set_consumer. Only the identifier is required.
apitally.set_consumer(
user.identifier,
name=user.name,
group=user.group,
attributes={"plan": user.plan},
)To attach custom data to the request's trace, call set_request_attribute. The sample_on_response, mask_request_body, and mask_response_body callbacks can also read these attributes.
apitally.set_request_attribute("tenant.id", tenant_id)See our documentation on attributes for details.
To capture an exception that your code handles itself, call capture_exception. Unhandled exceptions are captured automatically.
try:
process_order()
except Exception as exc:
apitally.capture_exception(exc)If your app already uses OpenTelemetry, set up your tracer provider and any framework instrumentation, such as FastAPIInstrumentor, before initializing Apitally. Apitally adds its span processor to your tracer provider and keeps your existing exporters.
Your tracer provider's sampler also applies to Apitally. Requests it drops have no request logs or traces in Apitally, but still count in metrics.
If your app runs behind a reverse proxy or load balancer, configure trusted proxies in your server or framework, for example with Uvicorn's --forwarded-allow-ips option or Werkzeug's ProxyFix middleware. Apitally records the client IP your framework reports and uses it for GeoIP.
If you need help please create a new discussion on GitHub or email us at support@apitally.io. We'll get back to you as soon as possible.
This library is licensed under the terms of the MIT license.