Skip to Content
You are viewing a beta version of Clerk Docs
Visit the latest docs
Clerk logo

Clerk Docs

Ctrl + K
Go to clerk.com

getToken()

Retrieves a token for a JWT Template that is defined in the Clerk Dashboard on the JWT Templates(opens in a new tab) page.

function getToken: (sessionId: string, template: string) => Promise<Token>;

Parameters

NameTypeDescription
sessionIdstringThe ID of the session to retrieve a token for.
templatestringThe name of the JWT template from the Clerk Dashboard(opens in a new tab) to generate a new token from.
For example: 'firebase', 'grafbase', or your custom template's name.

Example

const sessionId = 'sess_123'; const template = 'test'; const response = await clerkClient.sessions.getToken(sessionId, template); console.log(response); /* _Token { jwt: 'eyJhbG...' } */

Examples with frameworks

app/api/get-token-example/route.ts
import { auth, clerkClient } from "@clerk/nextjs/server"; export async function GET() { const { sessionId } = auth(); if (!sessionId){ return Response.json({ message: "Unauthorized" }, { status: 401 }); } const template = 'test'; const token = await clerkClient.sessions.getToken(sessionId, template) return Response.json({ token }) }
pages/api/getToken.ts
import { clerkClient, getAuth } from '@clerk/nextjs/server'; import type { NextApiRequest, NextApiResponse } from 'next'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { sessionId } = getAuth(req); if (!sessionId) { return res.status(401).json({ error: "Unauthorized" }) } const template = 'test'; const token = await clerkClient.sessions.getToken(sessionId, template); return res.json({ token }); }

Last updated on April 8, 2024

What did you think of this content?

Clerk © 2024