Tesseral for FastAPI
Tesseral’s FastAPI SDK lets you add authentication to your Python backend using FastAPI.
The Tesseral FastAPI SDK is open-source and available on GitHub.
Getting Started
Install the Tesseral FastAPI SDK by running:
Then, in your FastAPI application, add the RequireAuthMiddleware
to your app:
Replace publishable_key_...
with your project’s Publishable Key. You can find
it in the API Keys
Settings of the
Tesseral Console.
Once you’ve added RequireAuthMiddleware
, all HTTP requests to your server will
automatically be authenticated. Inauthentic requests receive a 401 Unauthorized
response before reaching your route handlers.
Accessing details about the authenticated request
The Tesseral SDK makes information about the current authenticated request
available through the Auth
object and dependency injection.
Getting the Auth object
To access authentication information in your route handlers, use the get_auth
dependency:
See FastAPI’s documentation on dependency
injection and
fastapi.Depends
for
more information on this FastAPI pattern.
Getting the current Organization
To find out what Organization the request is for, use organization_id()
:
This is the most common identifier you’ll use in a B2B multitenant application.
Getting the request’s authenticated credentials
If your architecture forwards requests between internal services that need to
re-authenticate, use credentials()
:
Do not log or expose this value. You usually don’t need to use this unless you’re building internal service-to-service calls.
Getting details about the current User
To access more information about the authenticated User, use
access_token_claims()
:
access_token_claims
returns an
AccessTokenClaims
,
which contains details about the current Session ID, User, and Organization.
If the request is from an API Key, then
access_token_claims
will throw a NotAnAccessTokenError
.
We recommend that you mostly use organization_id()
in the vast majority of
your code; that is almost always the correct piece of information for most B2B
SaaS code should pay attention to. For more details, see B2B
Multitenancy.