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

# Better Auth

> Authenticate AI agents alongside Better Auth by resolving users by email and provisioning agent-created accounts.

Better Auth manages human sessions, cookies, and social logins; AgentOnboard provides the API token layer so AI agents can call your protected routes.

## Copy-paste agent prompt

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

1. Inspect the codebase and install `@agentonboard/sdk`.
2. Read these docs before editing:
   - https://docs.agentonboard.xyz/better-auth.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 ORM/user table holds Better Auth users, and which routes should accept agent tokens?
   - Should unknown emails return `403 ACCOUNT_REQUIRED` (reject) or create a verified 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 Better Auth users

In your route handler, use `requireAgentAccount` and query your Better Auth user table with `normalizeEmail(email)`:

```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.query.user.findFirst({
        where: (users, { eq }) => eq(users.email, normalizeEmail(email)),
      });

      return user?.id ?? null;
    },
  });

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

  // session.user is your Better Auth user ID
  return Response.json({ success: true, user_id: session.user });
}
```

***

## 2. Enabling agent signup with Better Auth

Add an explicit signup route using `createAgentAccount`:

```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.query.user.findFirst({
        where: (users, { eq }) => eq(users.email, normalizeEmail(email)),
      });
      return user?.id ?? null;
    },
    createAccount: async ({ email }) => {
      const [newUser] = await db
        .insert(usersTable)
        .values({
          id: crypto.randomUUID(),
          email: normalizeEmail(email),
          emailVerified: true,
          createdAt: new Date(),
          updatedAt: new Date(),
        })
        .returning();

      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]
>
> **`emailVerified: true`**
>
> AgentOnboard tokens are issued only to Users who have verified their email, so you can set `emailVerified: true` on creation.


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