Clerk logo

Clerk Docs

Ctrl + K
Go to clerkstage.dev

API Routes

The getAuth() helper retrieves the authentication state allowing you to protect your API routes or gather relevant data.

1
import { getAuth } from "@clerk/nextjs/server";
2
import type { NextApiRequest, NextApiResponse } from 'next'
3
4
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
5
const { userId } = getAuth(req);
6
// Load any data your application needs for the API route
7
return res.status(200).json({data})
8
};
1
import { getAuth } from "@clerk/nextjs/server";
2
3
export default async function handler(req, res) {
4
const { userId } = getAuth(req);
5
// Load any data your application needs for the API route
6
return res.status(200).json({data});
7
}

Before Next.js 12.2

You can use withAuth to retrieve data and use requireAuth to protect your route automatically while retrieving data.

1
import { withAuth } from '@clerk/nextjs/api'
2
import { NextApiResponse, NextApiRequest } from 'next'
3
import { ServerGetToken } from '@clerk/types'
4
5
//Interface example with Clerk
6
interface ClerkRequest extends NextApiRequest {
7
auth: {
8
userId?: string | null
9
sessionId?: string | null
10
getToken: ServerGetToken
11
}
12
}
13
14
export default withAuth(async (req: ClerkRequest, res: NextApiResponse) => {
15
const { userId, sessionId, getToken } = req.auth
16
// Load any data your application needs for the API route
17
res.status(200).json({ data })
18
})
1
import { withAuth } from "@clerk/nextjs/api";
2
3
export default withAuth(async (req, res) => {
4
const { userId } = req.auth;
5
// Load any data your application needs for the API route
6
res.status(200).json({ data })
7
});
1
import { requireAuth } from '@clerk/nextjs/api'
2
import { NextApiResponse, NextApiRequest } from 'next'
3
import { ServerGetToken } from '@clerk/types'
4
5
interface ClerkRequest extends NextApiRequest {
6
auth: {
7
userId?: string | null
8
sessionId?: string | null
9
getToken: ServerGetToken
10
}
11
}
12
13
export default requireAuth(async (req: ClerkRequest, res: NextApiResponse) => {
14
const { userId } = req.auth
15
// Load any data your application needs for the API route
16
res.status(200).json({ data})
17
})
1
import { requireAuth } from '@clerk/nextjs/api';
2
3
export default requireAuth(async (req, res) => {
4
const { userId} = req.auth;
5
// Load any data your application needs for the API route
6
res.status(200).json({ data })
7
});

Example Response

{
sessionId: 'sess_2GaMqUCB3Sc1WNAkWuNzsnYVVEy',
userId: 'user_2F2u1wtUyUlxKgFkKqtJNtpJJWj',
orgId: null,
getToken: [AsyncFunction (anonymous)],
claims: {
azp: 'http://localhost:3000',
exp: 1666622607,
iat: 1666622547,
iss: 'https://clerk.quiet.muskox-85.lcl.dev',
nbf: 1666622537,
sid: 'sess_2GaMqUCB3Sc1WNAkWuNzsnYVVEy',
sub: 'user_2F2u1wtUyUlxKgFkKqtJNtpJJWj'
}
}

More detailed information about the fields in this object can be found in the Authentication Object documentation.

Was this helpful?

Clerk © 2023