Handling requests with Node.js and Express
Node.js and Connect/Express Middleware
The Clerk Node SDK offers two authentication middlewares specifically for Express and Connect/Express compatible frameworks such as Gatsby and Fastify.
ClerkExpressWithAuth
is a lax middleware that returns an empty auth object when an unauthenticated request is made.
ClerkExpressRequireAuth
is a strict middleware that raises an error when an unauthenticated request is made.
ClerkExpressWithAuth()
1import { ClerkExpressWithAuth } from '@clerk/clerk-sdk-node';2import express from 'express';34const port = process.env.PORT || 3000;56const app = express();78// Use the lax middleware that returns an empty auth object when unauthenticated9app.get(10'/protected-endpoint',11ClerkExpressWithAuth({12// ...options13}),14(req, res) => {15res.json(req.auth);16}17);1819app.listen(port, () => {20console.log(`Example app listening at http://localhost:${port}`);21});22
1import {2ClerkExpressWithAuth,3LooseAuthProp,4WithAuthProp,5} from '@clerk/clerk-sdk-node';6import express, { Application, Request, Response } from 'express';78const port = process.env.PORT || 3000;910const app: Application = express();1112declare global {13namespace Express {14interface Request extends LooseAuthProp {}15}16}1718// Use the lax middleware that returns an empty auth object when unauthenticated19app.get(20'/protected-route',21ClerkExpressWithAuth({22// ...options23}),24(req: WithAuthProp<Request>, res: Response) => {25res.json(req.auth);26}27);2829app.use((err, req, res, next) => {30console.error(err.stack);31res.status(401).send('Unauthenticated!');32});3334app.listen(port, () => {35console.log(`Example app listening at http://localhost:${port}`);36});
ClerkExpressRequireAuth()
1import { ClerkExpressRequireAuth } from '@clerk/clerk-sdk-node';2import express from 'express';34const port = process.env.PORT || 3000;5const app = express();67// Use the strict middleware that raises an error when unauthenticated8app.get(9'/protected-endpoint',10ClerkExpressRequireAuth({11// ...options12}),13(req: RequireAuthProp<Request>, res) => {14res.json(req.auth);15}16);1718app.use((err, req, res, next) => {19console.error(err.stack);20res.status(401).send('Unauthenticated!');21});2223app.listen(port, () => {24console.log(`Example app listening at http://localhost:${port}`);25});26
1import {2ClerkExpressRequireAuth,3RequireAuthProp,4StrictAuthProp,5} from '@clerk/clerk-sdk-node';6import express, { Application, Request, Response } from 'express';78const port = process.env.PORT || 3000;9const app: Application = express();1011declare global {12namespace Express {13interface Request extends StrictAuthProp {}14}15}1617// Use the strict middleware that raises an error when unauthenticated18app.get(19'/protected-route',20ClerkExpressRequireAuth({21// ...options22}),23(req: RequireAuthProp<Request>, res) => {24res.json(req.auth);25}26);2728app.use((err, req, res, next) => {29console.error(err.stack);30res.status(401).send('Unauthenticated!');31});3233app.listen(port, () => {34console.log(`Example app listening at http://localhost:${port}`);35});36
Express Error Handlers
Express comes with a default error handler for errors encountered in the middleware chain.
Developers can also implement their own custom error handlers as detailed in the Express error handling guide. An example error handler can be found above.
If you are using the strict middleware variant, the err
pass to your error handler will contain enough context for you to respond as you deem fit.
Middleware options
Name | Type | Description |
---|---|---|
authorizedParties | string[] | Validate that the For more information refer to Manual JWT Verification. |
jwtKey | string | Clerk's JWT session token can be verified in a networkless manner using the JWT verification key. By default, Clerk will use our well-known JWKs endpoint to fetch and cache the key for any subsequent token verification. If you use the For more information refer to Networkless Token Verification. |
onError | (error: ClerkAPIResponseError) => unknown | This function can act as a custom error handler tailored to the needs of your application. |