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:
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