Setup guide for FastAPI
Set up your FastAPI application with Apitally to monitor API requests, errors, performance and uptime.
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:
- Monitor traffic to your application on our simple dashboard
- Keep track of errors, response times and payload sizes
- Understand how individual API consumers use your application
- Monitor your application’s uptime and receive alerts when it’s down
Create app
To get started, create a new app in the Apitally dashboard and select as your framework.
Here you can also configure the environments (e.g. prod
and dev
) for your app, or simply accept the defaults.
Take a note of the client ID displayed after submitting. You will need it in the next step.
Add middleware
Next, install the Apitally client library in your project with the extra.
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 FastAPI
from apitally.fastapi import ApitallyMiddleware
app = 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 your application is sending data to Apitally and you are able to monitor requests, errors, response times etc. However, you aren't able to filter by consumers yet.
Identify consumers
In order to analyze API traffic by consumers in Apitally, your application must identify which consumers requests are coming from.
How you identify API consumers depends on your application and use case. If your application uses authentication, it would make sense to use the authenticated identity (e.g. username) as the consumer identifier.
To associate requests with consumers, set the apitally_consumer
attribute on the request.state
object.
You could do this or another middleware, for example.
from typing import Annotated
from fastapi import FastAPI, Depends, Request
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
class User(BaseModel):
user_id: str
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> User:
user = fake_decode_token(token)
return user
def identify_consumer(request: Request, current_user: Annotated[User, Depends(get_current_user)]) -> None:
request.state.apitally_consumer = current_user.user_id
app = FastAPI(dependencies=[Depends(identify_consumer)])
The consumer identifier should be a string (max. 128 characters long) that uniquely identifies the consumer of the API, e.g. customer-123
.
Once the first request is received from a consumer, it will show up in the Apitally dashboard.
Now the Consumers page in the Apitally dashboard shows you information about all consumers that have made requests to your application. You can also filter insights on the Traffic page by consumer and better understand how each of them use your application.
Name and group consumers
By default, Apitally generates a name for new consumers from the consumer identifier. You can always change it in the Apitally dashboard, and you can assign consumers to groups there too.
If you have many consumers, you may prefer to set a name and group programmatically from your application.
from typing import Annotated
from fastapi import FastAPI, Depends, Request
from fastapi.security import OAuth2PasswordBearer
from apitally.fastapi import ApitallyMiddleware, ApitallyConsumer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
class User(BaseModel):
user_id: str
name: str
group: str
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> User:
user = fake_decode_token(token)
return user
def identify_consumer(request: Request, current_user: Annotated[User, Depends(get_current_user)]) -> None:
request.state.apitally_consumer = ApitallyConsumer(
identifier=current_user.user_id,
name=current_user.name,
group=current_user.group,
)
app = FastAPI(dependencies=[Depends(identify_consumer)])