Fastify Quickstart
You will learn the following:
- Install
@clerk/fastify
- Set your Clerk API keys
- Configure
clerkPlugin
for all routes - Use
getAuth()
to access the auth state and protect routes - Configure
clerkPlugin
for specific routes
Before you start
Example repository
Learn how to integrate Clerk into your Fastify backend for secure user authentication and management. This guide uses TypeScript and allows you to choose your frontend framework.
Install @clerk/fastify
Clerk's Fastify SDK provides a range of backend utilities to simplify user authentication and management in your application.
To get started using Clerk with Fastify, add the SDK to your project:
Add the following keys to your .env.local
file. These keys can always be retrieved from the API keys page in the Clerk Dashboard.
- In the Clerk Dashboard, navigate to the API keys page.
- In the Quick Copy section, copy your Clerk Publishable and Secret Keys.
- Paste your keys into your
.env
file.
The final result should resemble the following:
Configure clerkPlugin
for all routes
The clerkPlugin
is a Fastify plugin provided by Clerk to integrate authentication into your Fastify application. To ensure that Clerk's authentication and user management features are applied across your Fastify application, configure the clerkPlugin
to handle all routes or limit it to specific ones.
The following example registers the plugin for all routes. To register the plugin for specific routes only, refer to the Using Clerk for specific pages only section.
Use getAuth()
to access the auth state and protect routes
The following example uses getAuth()
to retrieve the userId
, which is used to protect the route and is passed to clerkClient.users.getUser()
to retrieve the current user's User
object.
Configure clerkPlugin
for specific routes
If you want to use Clerk for specific pages only, you can register the plugin for specific routes. In the following example, the routes are split into protected and public routes.
Feedback
Last updated on