> ## 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.

# Clerk

> Authenticate AI agents alongside Clerk by mapping verified agent tokens to Clerk users and provisioning agent signups.

Clerk handles your human users in the browser; AgentOnboard handles AI agents calling your API.

When an agent presents a 5-minute Agent Identity Token, read the verified email and look up the matching Clerk user.

## Copy-paste agent prompt

```text theme={null}
Integrate AgentOnboard with Clerk in this repository.

1. Inspect the codebase and install `@agentonboard/sdk`.
2. Read these docs before editing:
   - https://docs.agentonboard.xyz/clerk.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 protected route(s) should accept agent tokens?
   - Should unknown emails return `403 ACCOUNT_REQUIRED` (reject) or create a passwordless Clerk user 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:
   - Use `requireAgentAccount` (and `createAgentAccount` on `POST /api/signup` if enabled).
   - Hardcode `audience` to the normalized production Domain.
   - Normalize emails with `normalizeEmail(email)` from `@agentonboard/sdk` (never raw `email.toLowerCase()`).
   - Serve 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. Verifying existing Clerk users

Use the Clerk Backend SDK inside `resolveAccount` to find the user by normalized email:

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

export async function POST(request: Request) {
  const client = await clerkClient();

  const session = await requireAgentAccount(request, {
    audience: "notes.com",
    resolveAccount: async ({ email }) => {
      const users = await client.users.getUserList({
        emailAddress: [normalizeEmail(email)],
      });

      return users.data[0]?.id ?? null;
    },
  });

  // Returns 401 (expired/invalid) or 403 (no Clerk account found)
  if (!session.ok) return session.response;

  // session.user is the Clerk User ID (e.g. user_2abc...)
  return Response.json({ success: true, clerk_user_id: session.user });
}
```

If the email is not in Clerk, the SDK returns `403 ACCOUNT_REQUIRED`, prompting the agent to follow your `auth.md` instructions.

***

## 2. Enabling agent signup with Clerk

To let agents provision accounts for new users, add an explicit signup handler using `createAgentAccount`:

```ts title="app/api/signup/route.ts" theme={null}
import { clerkClient } from "@clerk/nextjs/server";
import { createAgentAccount, normalizeEmail } from "@agentonboard/sdk";

export async function POST(request: Request) {
  const client = await clerkClient();

  const outcome = await createAgentAccount({
    token: request.headers.get("Authorization") ?? "",
    audience: "notes.com",
    resolveAccount: async ({ email }) => {
      const users = await client.users.getUserList({
        emailAddress: [normalizeEmail(email)],
      });
      return users.data[0]?.id ?? null;
    },
    createAccount: async ({ email }) => {
      const newUser = await client.users.createUser({
        emailAddress: [normalizeEmail(email)],
        skipPasswordRequirement: true,
      });

      return newUser.id;
    },
  });

  if (!outcome.ok) {
    return Response.json(
      { code: outcome.code, error: outcome.error },
      { status: outcome.code === "RESOLVER_ERROR" || outcome.code === "CONFIG_ERROR" ? 500 : 401 },
    );
  }

  return Response.json(
    { user_id: outcome.user, created: outcome.created },
    { status: 201 },
  );
}
```

> [!NOTE]
>
> **Why `skipPasswordRequirement` works**
>
> AgentOnboard verifies the User's email before issuing any token. Passing `skipPasswordRequirement: true` lets the agent create the Clerk user immediately without getting blocked on a password field.


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