In this tutorial, we show you how to integrate passkeys into a Python FastAPI web app using Corbado’s passkey-first web-js library.
Nicolai
Created: December 19, 2023
Updated: September 3, 2024
We aim to make the Internet a safer place using passkeys. That's why we want to support developers with tutorials on how to implement passkeys.
2. FastAPI Passkey Project Prerequisites
3. Repository Structure for FastAPI Passkey Project
4. Set Up Your Corbado Account and Project
5. FastAPI Passkey Project Setup
5.2 Configure Environment Variables
Recent Articles
In this tutorial, we will be walking through the process of building a sample application with passkey authentication using the FastAPI web framework made for Python. To make passkeys work, we use Corbado's passkey-first web-js package that automatically connects to a passkeys backend.
If you want to run the project straight away, please follow the README of our sample app Github repository.
The result looks as follows:
This tutorial assumes basic familiarity with FastAPI and Python as well as HTML and JavaScript. Let's dive in!
Become part of our Passkeys Community for updates and support.
JoinOur FastAPI project contains many files, but these are the most important ones:
Visit the Corbado developer panel to sign up and create your account (you'll see the passkey sign-up in action here!).
After sign-up, a project wizard will guide you through the necessary steps to get everything up and running:
Application URL
and Relying Party ID
. The Application URL
is the URL where you embed the Corbado UI component. In this example, we set it to http://localhost:8000
. The Relying Party ID
is the domain (no protocol, no port, and no path) where passkeys should be bound. Here, it's localhost
(you can define both values als in the Settings > General > URLs of the Corbado developer panel).Afterwards, you'll see the relevant HTML / JavaScript code snippets you need to integrate into the project. The subsequent sections of this article will explain them in detail.
As another step, we create an API secret
which will be needed to request user data from the
Corbado backend. Please create an API secret
in Settings > Credentials > API secrets.
If you haven't installed FastAPI yet, do so by executing
To initialize our project, we simply create a main.py
file with the following
content:
We create a .env
file in the root folder with the following contents (using
your values of course):
We use python-dotenv
to load the variables. Install it by running
Then, we can import it into our main.py
file and use it to obtain the variables
from our .env
file:
For our templates, we create a templates folder. The login page will be
in templates/login.html
.
It contains a script from Corbado with styles as well as the logic needed to display the authentication component.
See more info on how to use web-js here
Our second page is the profile page which the user will be redirected to after
authentication. Here, we show some basic user info which we will obtain in the Python
controller beforehand (we will create it later). We also provide
a logout button that will terminate the session Corbado had initiated after
authentication. The page is located at templates/profile.html
with the
following content:
Since we only build a comparatively small app, our controller will be placed
in our main.py
file. Therefore, it will hold the entire web app logic
comprised of two methods one for the index page and one for the profile
page. Inside the index method, we only need to inject the project ID, but
inside the profile method, we need to verify the integrity of the Corbado
session and extract the data stored in it. For this, we use the Corbado Python SDK
(passkeys):
The main.py
file should look like this afterwards:
Subscribe to our Passkeys Substack for the latest news, insights and strategies.
SubscribeTo run your FastAPI application, you need to install uvicorn:
Afterwards, use the following command:
Your FastAPI application will start when visiting http://localhost:8000 with a web browser. You should see the Authentication UI.
After successful sign up / login, you see the profile page:
This tutorial showed how easy it is to add passwordless authentication with passkeys to a FastAPI app with using Corbado. Besides the passkey-first authentication, Corbado provides simple session management, that we used for a retrieval of basic user data. If you want to read more about how Corbado's session management please check the docs here. If you want to add Corbado to your existing app with existing users, please see our documentation here.
Table of Contents
Enjoyed this read?
🤝 Join our Passkeys Community
Share passkeys implementation tips and get support to free the world from passwords.
🚀 Subscribe to Substack
Get the latest news, strategies, and insights about passkeys sent straight to your inbox.
We provide UI components, SDKs and guides to help you add passkeys to your app in <1 hour
Start for free
Recent Articles
Flask Passkeys: How to Implement Passkeys with Python Flask
Janina - September 15, 2023
Django Passkeys: How to Implement Passkeys with Python Django
Nicolai - November 30, 2023