---
title: FastAPI Observability with OpenTelemetry | Pydantic Logfire
description: >-
  Instrument FastAPI with OpenTelemetry in one line. Trace every route, database
  query, and Pydantic validation with Logfire, then query it all with SQL.
canonical: 'https://pydantic.dev/logfire/fastapi'
---

> Markdown version of [Trace, monitor and debug your FastAPI app](https://pydantic.dev/logfire/fastapi) — the canonical HTML page.
>
> Site index: [/llms.txt](https://pydantic.dev/llms.txt)

---

# Trace, monitor and debug your FastAPI app

Logfire for FastAPI

FastAPI Observability from the team behind Pydantic. One line instruments every route, and each request shows its dependencies, database queries, and Pydantic validation in one connected trace. Query everything in plain SQL. Standard OpenTelemetry, no lock-in.

[Try Logfire for free](https://logfire.pydantic.dev/login?intent=signup) [See the FastAPI docs](https://pydantic.dev/docs/logfire/integrations/web-frameworks/fastapi/)

Set up Logfire with your coding agent:

uvx logfire-cli wizard

Install and configure

```bash
pip install 'logfire[fastapi]'
logfire auth   # or set LOGFIRE_TOKEN in production
```

Instrument your app

```python
import logfire
from fastapi import FastAPI

logfire.configure()

app = FastAPI()
logfire.instrument_fastapi(app)
```

Full details are in the [FastAPI setup guide](https://pydantic.dev/docs/logfire/integrations/web-frameworks/fastapi/).

Trusted by teams building production software and AI

**Customers:** Motorola, Sophos, Airbyte, Pictet, Vox Media, Weaviate, Amboss, Tiger Data

What you get

## One request, one trace

![A Logfire trace of three FastAPI requests: a successful order lookup with its SQLAlchemy queries, outbound HTTP call and Pydantic response validation, then a 404 and a 500 shown in red](https://pydantic.dev/assets/logfire/language-traces/fastapi-trace.png)

Every request becomes a trace, the full timeline of one request, built from nested spans, each a timed unit of work, from the route down to each query, validation, and outbound call, with timing and errors attached.

Try it on your stack

## See your own FastAPI trace

Start free with 10 million spans, logs, and metrics each month. No credit card required.

[Start free](https://logfire.pydantic.dev/login?intent=signup) [Read the FastAPI setup guide](https://pydantic.dev/docs/logfire/integrations/web-frameworks/fastapi/)

How it works

## See the whole request, not just the route

### Trace the whole request, not just the route

The route span is the start. Add the libraries the endpoint uses, one line each: logfire.instrument\_sqlalchemy(engine=engine), logfire.instrument\_asyncpg(), logfire.instrument\_httpx(), logfire.instrument\_redis(). A request to POST /orders then shows the incoming call, the Pydantic validation, each database query with its timing, and the outbound call to your payments provider. When something is slow, you can see which part.

### See your Pydantic validation

FastAPI request bodies are Pydantic models, and instrument\_fastapi already captures the validated arguments and validation errors: a malformed request shows the model, the input, and the exact field that failed, turning a vague wave of 422s into a specific, fixable cause. For models you validate outside the endpoint signature, add logfire.instrument\_pydantic() to trace those too. Captured arguments can include request data, so note that Logfire scrubs common sensitive fields by default and the scrubbing rules are configurable.

### Trace your own endpoint logic

For the business logic inside a handler, open a span where the real work begins with logfire.span("create\_order", customer=order.customer\_id). It becomes the parent of the auto-instrumented queries and HTTP calls it triggers, with customer\_id captured as a queryable attribute.

### One query surface for FastAPI telemetry

Query request spans, validation errors, database calls, metrics, and structured logs in one Logfire project. Filter by route or your own attributes, then save the same SQL as a dashboard or alert when the question recurs.

### Open standard, no lock-in

Logfire is OpenTelemetry underneath. Your FastAPI instrumentation is portable: send the same data to another OTel backend or self-host, and nothing in your app changes. You instrument once against the open standard and keep your options open.

Query

## Query your FastAPI telemetry with SQL

If you can ask it, SQL can find out

* Query your traces, metrics and logs with real SQL
* No proprietary query language to learn
* AI models are great at writing SQL
* From a one-off investigation to an alert, dashboard, or SLO

```sql
select
  attributes->>'http.route' as route,
  count(*) as requests,
  avg(duration) as avg_seconds
from records
where http_response_status_code >= 500
group by route
order by requests desc;
```

Proof

## In production

> We are finding and fixing issues in five minutes instead of an hour, which is obviously increasing uptime for our clients.

Zach Silver, Staff Software Engineer, BoostedAI [Read the case study](https://pydantic.dev/case-studies/boostedai)

Decision guide

## Is Logfire right for you?

### Choose Logfire if

* You want to instrument FastAPI in one line and see every request as a trace
* You want request validation errors visible in production, down to the field
* You want database queries and outbound calls timed inside each request
* You want to query traces, metrics, and logs with PostgreSQL-compatible SQL
* You want the portability of OpenTelemetry with Python-first ergonomics

### Choose a traditional APM if

* You are already deeply integrated with a specific APM vendor's ecosystem
* You need a vendor-only integration or compliance capability Logfire does not offer
* You want one vendor's agent and dashboards, and are comfortable with their pricing model

FAQ

## Common questions

### How do I add OpenTelemetry to a FastAPI application?

Install Pydantic Logfire, call logfire.configure(), then logfire.instrument\_fastapi(app). That one line captures every route, request, response, and exception. Add instrument\_sqlalchemy(engine=engine), instrument\_httpx(), and similar to trace the database and outbound calls each request makes. Logfire is OpenTelemetry-native, so standard OTel tooling works too.

### Can I see FastAPI request validation errors in production?

Yes. FastAPI validates requests with Pydantic, and because Logfire is built by the Pydantic team, calling logfire.instrument\_pydantic() surfaces validation as spans. A failed request shows the model, the input, and the exact field that did not match, so a wave of 422s becomes a specific, fixable cause.

### How do I exclude health checks from FastAPI tracing?

Set the standard OpenTelemetry variable OTEL\_PYTHON\_FASTAPI\_EXCLUDED\_URLS to comma-separated regexes, for example '/health$,/metrics$'.

### Does instrumenting FastAPI add much overhead?

Auto-instrumentation is built for production. Telemetry is batched and exported asynchronously, and traces can be sampled to control volume, so the runtime cost is small relative to the visibility gained into slow endpoints and failing calls.

## See inside your FastAPI app

Get started with 10 million free spans, logs, and metrics per month. No credit card required.

* [Start free](https://logfire.pydantic.dev/login?intent=signup)
* [Book a demo](https://pydantic.dev/contact)
* [View pricing plans](https://pydantic.dev/pricing)
