Supporting FastAPI Authentication

Supporting FastAPI Authentication

FastAPI is a modern, fast, web framework for building APIs with Python. You can get an API up and running incredibly quickly and one of my personal favorite features is it's dependency injection.

At PropelAuth, we previously had a generic python library for handling authentication and authorization (and support for other frameworks like Flask), but now we're excited to show off our first-class support for FastAPI.

With PropelAuth, you can easily build and configure login, signup, account pages, organization management, transactional emails, and more in a matter of minutes. We support a wide range of frontends, from Webflow to Next.js.

You can then write an API route as simple as:

@app.get("/whoami")
def who_am_i(user: User = Depends(auth.require_user)):
    return {"user_id": user.user_id}

The user is injected directly into your function. Users without valid access tokens are rejected and all you have to do is manage a user_id. The library also supports B2B auth - you can easily check if a user is a member of an organization or has permission to perform a specific action.

Want to see it in action? Check out our guide where we build a secure application with a React frontend, FastAPI backend, and PropelAuth for authentication.

Example application built in the guide}