> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentonboard.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Next.js

> Verify AI agent tokens in Next.js App Router route handlers using @agentonboard/sdk/next.

`@agentonboard/sdk/next` exports `requireAgentAccount` for Next.js App Router route handlers. It reads the Bearer token, verifies the `RS256` signature locally against public JWKS keys, and resolves your App Account by verified email.

## Copy-paste agent prompt

```text theme={null}
Integrate AgentOnboard into this Next.js App Router repository.

1. Inspect the codebase and install `@agentonboard/sdk`.
2. Read these docs before editing:
   - https://docs.agentonboard.xyz/nextjs.md
   - https://docs.agentonboard.xyz/verify.md
   - https://docs.agentonboard.xyz/accounts.md
   - https://docs.agentonboard.xyz/auth-md.md
3. Ask me three questions before writing code:
   - Which auth provider or users table should `resolveAccount` query?
   - Should unknown emails return `403 ACCOUNT_REQUIRED` (reject) or create an account on `POST /api/signup`?
   - What is the production Domain hostname (e.g. `notes.com` or `api.notes.com`)?
4. Implement the integration following these invariants:
   - Call `requireAgentAccount` from `@agentonboard/sdk/next` inside route handlers (never in middleware).
   - Hardcode `audience` to the normalized production Domain.
   - Normalize emails with `normalizeEmail(email)` from `@agentonboard/sdk` (never raw `email.toLowerCase()`).
   - Publish the discovery file at both `/auth.md` and `/.well-known/auth.md` with matching `## Audience` and `403` instructions.
5. Verify against localhost by minting a test token with `POST https://agentonboard.xyz/api/v1/token/exchange` and testing `403 ACCOUNT_REQUIRED`, `200 OK`, and `401 INVALID_SIGNATURE`.
```

***

## 1. Install the SDK

> [tab] npm
>
> ```bash theme={null}
> npm install @agentonboard/sdk
> ```

> [tab] pnpm
>
> ```bash theme={null}
> pnpm add @agentonboard/sdk
> ```

> [tab] yarn
>
> ```bash theme={null}
> yarn add @agentonboard/sdk
> ```

> [tab] bun
>
> ```bash theme={null}
> bun add @agentonboard/sdk
> ```

***

## 2. Guard your route handler

`requireAgentAccount(request, options)` accepts the standard Web API `Request` that App Router handlers receive (`NextRequest` satisfies it unchanged):

```ts title="app/api/notes/route.ts" theme={null}
import { normalizeEmail } from "@agentonboard/sdk";
import { requireAgentAccount } from "@agentonboard/sdk/next";

// Simulated database lookup
const accounts = new Map([["ada@example.com", "user_123"]]);

export async function POST(request: Request) {
  const session = await requireAgentAccount(request, {
    audience: "notes.com",
    // `email` is the verified identity claim. Normalize with `normalizeEmail`.
    resolveAccount: async ({ email }) => {
      return accounts.get(normalizeEmail(email)) ?? null;
    },
  });

  // Automatically returns 401 (invalid/expired) or 403 (ACCOUNT_REQUIRED)
  if (!session.ok) return session.response;

  // `session.user` is your own user id. Scope your mutations with confidence.
  return Response.json({ success: true, created_by: session.user });
}
```

> [!WARNING]
>
> **Call in route handlers, not in middleware**
>
> Next.js middleware cannot pass a resolved user record down to a route handler without writing it into request headers or cookies that a client could spoof. Always execute `requireAgentAccount` directly inside your route handler where queries are scoped.

***

## 3. How errors are handled

When verification fails or `resolveAccount` returns `null`, `session.ok` is `false` and `session.response` is a ready-to-return `Response` (`401` for token errors, `403` for `ACCOUNT_REQUIRED`, `500` for `RESOLVER_ERROR` or `CONFIG_ERROR`). See [Errors & Status Codes](https://docs.agentonboard.xyz/errors.md) and [Framework Overview](https://docs.agentonboard.xyz/frameworks.md#custom-failure-responses-onfailure) for `onFailure` customization.

***

## 4. Test with curl

Mint a 5-minute test token using [`POST /api/v1/token/exchange`](https://docs.agentonboard.xyz/api/token-exchange) and call your Next.js route:

```bash theme={null}
# Mint a scoped test token
TOKEN=$(curl -s -X POST https://agentonboard.xyz/api/v1/token/exchange \
  -H "Authorization: Bearer aon_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{"audience":"notes.com"}' \
  | jq -r '.token')

# Call your local or deployed endpoint
curl -i -X POST http://localhost:3000/api/notes \
  -H "Authorization: Bearer $TOKEN"
```

***

> [card] [Publishing auth.md](https://docs.agentonboard.xyz/auth-md.md)
>
> Host /auth.md and /.well-known/auth.md for your Next.js API.

> [card] [Testing & Production](https://docs.agentonboard.xyz/testing-and-production.md)
>
> Run all six curl checks and review JWKS caching.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.