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

# Accounts & Agent Signup

> Map verified emails to App Accounts with verifyAgentAccount, decide between rejecting or creating unknown users, and wire POST /api/signup.

`verifyAgentToken` proves **who the User is** (`email`). `verifyAgentAccount` adds the second step: checking whether that email has an App Account in your database and returning your internal user ID or record.

```ts theme={null}
import { verifyAgentAccount, normalizeEmail } from "@agentonboard/sdk";

const outcome = await verifyAgentAccount({
  token,
  audience: "notes.com",
  resolveAccount: async ({ email }) => {
    const user = await db.user.findUnique({
      where: { email: normalizeEmail(email) },
    });
    return user?.id ?? null; // null or undefined means "no account"
  },
});

if (!outcome.ok) {
  // outcome.code is one of the 9 verification codes, ACCOUNT_REQUIRED, or RESOLVER_ERROR
  return { code: outcome.code, error: outcome.error };
}

// outcome.user is your resolved database user ID or object
```

`verifyAgentAccount` accepts every [`verifyAgentToken` option](https://docs.agentonboard.xyz/verify#options) plus `resolveAccount`.

> [!NOTE]
>
> **Framework bindings return `{ ok: true, user }` only**
>
> While `verifyAgentAccount` returns `{ ok: true, email, user }`, framework bindings (`@agentonboard/sdk/next`, `/express`, `/hono`) return `{ ok: true, user }` so route handlers scope queries by your internal `session.user` ID.

***

## The `resolveAccount` contract

```ts theme={null}
type AccountResolver<TUser> = (
  identity: { readonly email: string }
) => TUser | null | undefined | Promise<TUser | null | undefined>;
```

Key rules of `resolveAccount`:

1. **Receives `{ email }` only:** Runs only after cryptographic verification succeeds.
2. **Always use `normalizeEmail(email)`:** Pass `normalizeEmail(email)` (from `@agentonboard/sdk`) into database queries instead of raw `email.toLowerCase()` so case and whitespace match consistently.
3. **Return `null` or `undefined` when no user exists:** Both trigger `403 ACCOUNT_REQUIRED`. Numeric `0` and empty string `""` count as valid user IDs.
4. **Runs once per request:** The SDK never caches `resolveAccount` results, so suspended or deleted users lose access immediately.
5. **Exceptions become `500 RESOLVER_ERROR`:** If your query throws, the SDK catches the error and hides the raw exception message so connection strings cannot leak. Log database errors inside `resolveAccount` before rethrowing.

> [!WARNING]
>
> **Return `403 Forbidden` for `ACCOUNT_REQUIRED`, never `401`**
>
> HTTP `401` tells an agent its token is invalid or expired, causing it to mint a new token and retry in a loop. HTTP `403` with `ACCOUNT_REQUIRED` tells the agent the token is valid, and directs it to follow your `auth.md` instructions. All framework bindings return `403` for `ACCOUNT_REQUIRED` automatically.

***

## Handling unknown users: Reject vs. Create

When an email is valid but absent from your database, pick one of two models:

| Property | Reject (Default) | Create (Explicit Signup) |
| - | - | - |
| SDK functions used | `verifyAgentAccount` or `requireAgentAccount` on all routes | `verifyAgentAccount` on all routes, plus `createAgentAccount` on `POST /api/signup` |
| What an unknown email receives | `403 ACCOUNT_REQUIRED` → User connects their account in your browser UI, then agent retries | `403 ACCOUNT_REQUIRED` → agent calls `POST /api/signup` with the same token, then retries |
| Human step required? | Yes (one-time browser connection or registration) | No (agent completes signup and retries automatically) |
| Where accounts are created | Your existing web signup or connect flow | `POST /api/signup` only |

### When to Reject (Default)

Keep the default `verifyAgentAccount` / `requireAgentAccount` behavior across all routes if:

* Your API serves existing paying customers, enterprise workspaces, or an invite-only beta.
* New accounts need billing setup, organization assignment, or manual approval before API access.
* You want Users to complete web signup before granting agent access.

### When to Create (`POST /api/signup`)

Add a dedicated `POST /api/signup` handler with `createAgentAccount` if:

* Your product is self-serve and you want a User's agent to register without opening a browser.
* You apply rate limits and abuse controls to self-serve signups.
* You document `POST /api/signup` in your `auth.md` file.

> [!WARNING]
>
> **Agent signup is open self-registration**
>
> Anyone with a verified AgentOnboard email can call `POST /api/signup`, just like submitting a public web signup form. Apply the same rate limits, trial tiers, or email domain allowlists you enforce on web signups.

***

## Setting up explicit agent signup (`createAgentAccount`)

Follow the two-step signup pattern:

**1. Guard regular routes with requireAgentAccount**

Keep `requireAgentAccount` (or `verifyAgentAccount`) on all regular endpoints so unknown emails receive `403 ACCOUNT_REQUIRED` instead of creating accounts as a side effect:

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

export async function POST(request: Request) {
  const session = await requireAgentAccount(request, {
    audience: "notes.com",
    resolveAccount: async ({ email }) => {
      const user = await db.user.findUnique({
        where: { email: normalizeEmail(email) },
      });
      return user?.id ?? null;
    },
  });

  if (!session.ok) return session.response;

  return Response.json({ created_by: session.user });
}
```

**2. Add POST /api/signup using createAgentAccount**

`createAgentAccount` verifies the token first, checks `resolveAccount`, and runs `createAccount` only when `resolveAccount` returns `null` or `undefined`:

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

export async function POST(request: Request) {
  const outcome = await createAgentAccount({
    token: request.headers.get("Authorization") ?? "",
    audience: "notes.com",
    resolveAccount: async ({ email }) => {
      const user = await db.user.findUnique({
        where: { email: normalizeEmail(email) },
      });
      return user?.id ?? null;
    },
    createAccount: async ({ email }) => {
      const user = await db.user.create({
        data: { email: normalizeEmail(email) },
      });
      return user.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: outcome.user, created: outcome.created },
    { status: 201 },
  );
}
```

```ts theme={null}
type CreateAccountOutcome<TUser> =
  | { ok: true;  email: string; user: TUser; created: boolean }
  | { ok: false; code: AccountErrorCode; error: string };
```

Operational guarantees of `createAgentAccount`:

* **Token verified before any database call:** Invalid or expired tokens return `401` without running `resolveAccount` or `createAccount`.
* **Idempotent on existing users:** When `resolveAccount` finds an existing user, `createAccount` is skipped and `outcome.created` is `false`.
* **Failed inserts return `500 RESOLVER_ERROR`:** Exceptions inside `createAccount` return `RESOLVER_ERROR` with status `500`.
* **Your app owns post-signup lifecycle:** Welcome emails, rate limits, password setup, and account deletion remain in your application. Revoking an AgentOnboard API Key stops future token minting without deleting App Accounts.

**3. Publish POST /api/signup in auth.md**

Document `POST /api/signup` and reference it in the `403 ACCOUNT_REQUIRED` row of your `auth.md` rejection table. See [Publishing auth.md](https://docs.agentonboard.xyz/auth-md.md) for the full template.

***

## Next steps

> [card] [Publishing auth.md](https://docs.agentonboard.xyz/auth-md.md)
>
> Copy-paste auth.md templates for Reject and Create models.

> [card] [Testing & Production](https://docs.agentonboard.xyz/testing-and-production.md)
>
> Run the six curl checks against your signup and protected routes.


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