# Manual JWT verification

Your Clerk-generated [session tokens](https://clerk.com/docs/guides/sessions/session-tokens.md) are essentially JWTs which are signed using your instance's private key and can be verified using your instance's public key. Depending on your architecture, these tokens will be in your backend requests either via a cookie named `__session` or via the Authorization header.

For every request, you must validate the token to ensure it hasn't expired or been tampered with (i.e., it's authentic and secure). If these validations succeed, then the user is authenticated to your application and should be considered signed in. The [`clerkClient.authenticateRequest()`](https://clerk.com/docs/reference/backend/authenticate-request.md) method handles these validations for you. Alternatively, you can manually verify the token without using the SDK. See the following sections for more information.

## Use `authenticateRequest()` to verify a session token

The [`authenticateRequest()`](https://clerk.com/docs/reference/backend/authenticate-request.md) method accepts the `request` object and authenticates the session token in it.

The following example uses the `authenticateRequest()` method to verify the session token. _It also performs networkless authentication_ by passing `jwtKey`. This verifies if the user is signed into the application.

`authenticateRequest()` requires `publishableKey` to be set. If you are importing `clerkClient` from a higher-level SDK, such as Next.js, then `clerkClient` infers the `publishableKey` from your [environment variables](https://clerk.com/docs/guides/development/clerk-environment-variables.md#clerk-publishable-and-secret-keys).

filename: app/api/example/route.ts
```tsx
  import { clerkClient } from '@clerk/nextjs/server'

  export async function GET(req: Request) {
    // Initialize clerkClient
    const client = await clerkClient()

    // Use the `authenticateRequest()` method to verify the token
    const { isAuthenticated } = await client.authenticateRequest(req, {
      authorizedParties: ['https://example.com'],
+     jwtKey: process.env.CLERK_JWT_KEY,
    })

    // Protect the route from unauthenticated users
    if (!isAuthenticated) {
      return Response.json({ error: 'Unauthorized' }, { status: 401 })
    }

    // Add logic to perform protected actions

    return Response.json({ message: 'This is a reply' })
  }
```

## Manually verify a session token

1. ### Retrieve the session token

   Retrieve the session token from either `__session` cookie for a same-origin request or from the `Authorization` header for cross-origin requests.
2. ### Get your instance's public key

   Use one of the three ways to obtain your public key:

   1. Use the Backend API in JSON Web Key Set (JWKS) format at the following endpoint [https://api.clerk.com/v1/jwks](https://clerk.com/docs/reference/backend-api/tag/jwks/GET/jwks){{ target: '_blank' }}.
   2. Use your Frontend API URL in JWKS format, also known as the JSON Web Key Set (JWKS) URL. The format is your Frontend API URL with `/.well-known/jwks.json` appended to it.
   3. Use your **JWKS Public Key**, which can be found on the [**API keys**](https://dashboard.clerk.com/~/api-keys) page in the Clerk Dashboard.
3. ### Verify the token signature

   To verify the token signature:

   1. Verify that the token's algorithm has the expected value.
   2. Use your instance's public key to verify the token's signature.
   3. Validate that the token isn't expired by checking the `exp` ([expiration time](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4)) and `nbf` ([not before](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5)) claims.
   4. Validate that the `azp` (authorized parties) claim equals any of your known origins permitted to generate those tokens. For better security, it's highly recommended to explicitly set the `authorizedParties` option when authorizing requests. The value should be a list of domains allowed to make requests to your application. Not setting this value can open your application to [CSRF attacks](https://owasp.org/www-community/attacks/csrf). For example, if you're permitting tokens retrieved from `http://localhost:3000`, then the `azp` claim should equal `http://localhost:3000`. You can also pass an array of strings, such as `['http://localhost:4003', 'https://clerk.dev']`. If the `azp` claim doesn't exist, you can skip this step.
4. ### Optional: Check for a `sts` claim

   If you are using Clerk's [Organizations](https://clerk.com/docs/guides/organizations/overview.md) feature and have not enabled Personal Accounts, users are _required to be part of an Organization before accessing your application_. If the user has completed registration, but is not yet part of an Organization, a valid session token will be created, but the token will contain a `sts` (status) claim set to `pending`. You may want to reject requests to your backend with pending statuses to ensure that users are not able to work around the Organization requirement.
5. ### Finished

   If the above process succeeds, the user is considered signed in to your application and authenticated. You can also retrieve the session ID and user ID from of the token's claims.

### Example

The following example manually verifies a session token.

```tsx
import Cookies from 'cookies'
import jwt from 'jsonwebtoken'

export default async function (req: Request, res: Response) {
  // Your public key should be set as an environment variable
  const publicKey = process.env.CLERK_PEM_PUBLIC_KEY
  // Retrieve session token from either `__session` cookie for a same-origin request
  // or from the `Authorization` header for cross-origin requests
  const cookies = new Cookies(req, res)
  const tokenSameOrigin = cookies.get('__session')
  const tokenCrossOrigin = req.headers.authorization

  if (!tokenSameOrigin && !tokenCrossOrigin) {
    res.status(401).json({ error: 'Not signed in' })
    return
  }

  try {
    let decoded
    const options = { algorithms: ['RS256'] } // The algorithm used to sign the token.
    const permittedOrigins = ['http://localhost:3000', 'https://example.com'] // Replace with your permitted origins

    if (tokenSameOrigin) {
      decoded = jwt.verify(tokenSameOrigin, publicKey, options)
    } else {
      decoded = jwt.verify(tokenCrossOrigin, publicKey, options)
    }

    // Validate the token's expiration (exp) and not before (nbf) claims
    const currentTime = Math.floor(Date.now() / 1000)
    if (decoded.exp < currentTime || decoded.nbf > currentTime) {
      throw new Error('Token is expired or not yet valid')
    }

    // Validate the token's authorized party (azp) claim
    if (decoded.azp && !permittedOrigins.includes(decoded.azp)) {
      throw new Error("Invalid 'azp' claim")
    }

    res.status(200).json({ sessionToken: decoded })
  } catch (error) {
    res.status(400).json({
      error: error.message,
    })
  }
}
```

---

## Sitemap

[Overview of all docs pages](https://clerk.com/docs/llms.txt)
