Fastify Quickstart
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
The Clerk Fastify SDK
Run the following command to install the SDK:
npm install @clerk/fastifypnpm add @clerk/fastifyyarn add @clerk/fastifybun add @clerk/fastifyAdd the following keys to your .env 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 and .
- Paste your keys into your
.envfile.
The final result should resemble the following:
CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEYConfigure clerkPlugin() for all routes
The clerkPlugin()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, see the reference docs
import 'dotenv/config'
import Fastify from 'fastify'
import { clerkPlugin } from '@clerk/fastify'
const fastify = Fastify({ logger: true })
fastify.register(clerkPlugin)
const start = async () => {
try {
await fastify.listen({ port: 8080 })
} catch (error) {
fastify.log.error(error)
process.exit(1)
}
}
start()Protect your routes using getAuth()
The getAuth()request object. It returns the Auth object
The following example uses getAuth() to protect a route and load the user's data. If the user is authenticated, their userId is passed to clerkClient.users.getUser()401 status code.
// dotenv must be imported before @clerk/fastify
import 'dotenv/config'
import Fastify from 'fastify'
import { clerkClient, clerkPlugin, getAuth } from '@clerk/fastify'
const fastify = Fastify({ logger: true })
fastify.register(clerkPlugin)
// Use `getAuth()` to protect this route
fastify.get('/protected', async (request, reply) => {
try {
// Use `getAuth()` to access `isAuthenticated` and the user's ID
const { isAuthenticated, userId } = getAuth(request)
// If user isn't authenticated, return a 401 error
if (!isAuthenticated) {
return reply.code(401).send({ error: 'User not authenticated' })
}
// Use `clerkClient` to access Clerk's JS Backend SDK methods
// and get the user's User object
const user = await clerkClient.users.getUser(userId)
return reply.send({
message: 'User retrieved successfully',
user,
})
} catch (error) {
fastify.log.error(error)
return reply.code(500).send({ error: 'Failed to retrieve user' })
}
})
const start = async () => {
try {
await fastify.listen({ port: 8080 })
} catch (error) {
fastify.log.error(error)
process.exit(1)
}
}
start()Next steps
Learn how to protect routes, handle authentication and authorization, and prepare your Clerk app for production using the following guides.
Protect routes using clerkPlugin()
Learn how to protect specific routes from unauthenticated users.
Protect routes based on authorization status
Learn how to protect a route based on both authentication and authorization status.
Deploy to production
Learn how to deploy your Clerk app to production.
Clerk Fastify SDK reference
Learn about the Clerk Fastify SDK and how to integrate it into your app.
Feedback
Last updated on