Apitally Blog

FastAPI monitoring made easy

Understand how your API is being used and how it's performing without deploying, configuring and maintaining complex monitoring tools.

Simon Gurcke//2 min read

I was looking for an easy way to monitor my FastAPI applications recently, and was surprised how complex it seemed to be. It inspired me to start building Apitally, a simple API monitoring & analytics tool that works seamlessly with FastAPI and is effortless to set up.

Monitoring can mean different things, so let me quickly summarize what Apitally actually helps you to do:

  • Understand API traffic, usage and adoption
  • Track client and server errors
  • Analyze API performance (i.e. response times)
  • Log, find and inspect individual API requests and responses
  • Monitor API uptime with synthetic health checks and heartbeats
  • Get notified when something goes wrong using custom alerts

Dashboards

One key difference to other tools is that Apitally is tailored specifically to monitoring REST APIs, making it particularly easy to use. It provides intuitive API monitoring dashboards out of the box, so you don’t have to spend time building your own.

The dashboards provide a clear overview of what’s happening with your API, while allowing you to drill down into any area of interest.

Screenshots of Apitally dashboard
API monitoring dashboards in Apitally

How it works

Apitally’s open-source SDK integrates with FastAPI using a lightweight middleware, which automatically captures key API metrics for each request and response. It aggregates data client-side and sends it to the Apitally dashboard asynchronously, without affecting your application’s performance.

By default, Apitally doesn’t capture any sensitive data, making it suitable for environments with strict privacy requirements. Request logging is opt-in and highly configurable to meet your specific needs.

Getting started

Setting Apitally up for your FastAPI project only takes a few minutes.

  1. The first step is to grab a client ID by signing up and creating an app in the Apitally dashboard.

  2. Then, add the apitally package with the fastapi extra to your project’s dependencies:

pip install "apitally[fastapi]"
  1. Finally, add the Apitally middleware to your app by copying & pasting a couple of lines of code, including your client ID:
from fastapi import FastAPI
from apitally.fastapi import ApitallyMiddleware

app = FastAPI()
app.add_middleware(
    ApitallyMiddleware,
    client_id="your-client-id",  # provided in Apitally dashboard
    env="prod",  # or "dev" etc.
)

After deploying your application, you should start seeing data in the Apitally dashboard within a few seconds.

Identifying consumers

Apitally allows you to track API usage per consumer. To enable this, you can associate requests with consumers in your application by setting the apitally_consumer property on the request.state object.

If your application uses authentication, it makes sense to set apitally_consumer based on the authenticated user. You could do this anywhere within your existing authentication code, or in a dedicated function, like in the example below.

def identify_consumer(
    request: Request,
    current_user: Annotated[User, Depends(get_current_user)],
):
    request.state.apitally_consumer = current_user.username

app = FastAPI(dependencies=[Depends(identify_consumer)])

Conclusion

Using Apitally can help you gain valuable API insights with a minimal amount of effort. Sometimes a simple solution is all you need.

This article was just a quick overview of what Apitally can do for you and how to get started with your FastAPI project. For further instructions, including how to configure request logging, check out our detailed setup guide for FastAPI.

Happy monitoring!