Skip to the page

Reference · Project API

Sign-in

Sign people in with a standard OAuth 2.0 PKCE exchange against the project’s own routes, and get a bearer session token back.

CLI
—
In the schema
ext::auth::Identity

PKCE application sign-in flow#

In TypeScript and Dart, SignIn from the client SDK runs the three PKCE steps for you and keeps the session. See sign-in in the TypeScript SDK. The steps below show the flow for any other language.

S256 PKCE across every factor

OAuth and OIDC redirects, passwords, magic links and WebAuthn passkeys all exchange a one-time authorization code and PKCE verifier for ext::auth::Identity tokens.

Time- and tool-scoped agent grants

OAuth 2.1 consent mints revocable ext::napalm::AgentGrant sessions optionally restricted to read-only posture and an allowedQueries allowlist.

  1. Generate a PKCE verifier and challenge

    Store a cryptographically random 32-byte verifier in session storage and compute its base64url-encoded SHA-256 digest as the challenge.

    Browser client
    const verifier = base64url(crypto.getRandomValues(new Uint8Array(32)))const challenge = base64url(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier)))const back = encodeURIComponent('https://app.example.com/signed-in')location.href = `https://notes.napalmdata.com/sign-in/authorize?provider=builtin::oauth_github&redirect_to=${back}&challenge=${challenge}`
  2. Authenticate the user

    For OAuth or OpenID Connect providers, GET /sign-in/authorize redirects the user to the provider and back through /sign-in/callback to your allowed redirect_to URL with an authorization code. For email/password, magic links or WebAuthn passkeys, POST directly to the corresponding factor route listed below with your challenge.

  3. Exchange the authorization code for a session token

    Request · curl
    curl "https://notes.napalmdata.com/sign-in/token?code=$CODE&verifier=$VERIFIER"
    Response
    {  "auth_token": "eyJhbGciOi…",  "identity_id": "0192c3f4-6b1a-7d8e-9f00-1a2b3c4d5e6f",  "refresh_token": "nsr_…",  "expires_in": 3600}

    Pass auth_token in Authorization: Bearer <auth_token> on subsequent Project API requests. Keep refresh_token to renew the session. Upstream OAuth provider tokens remain inside your database and are never exposed to the client.

See Access and sign-in for configuring providers, SMTP delivery and session lifetimes.

Session renewal and sign-out#

A session lasts as long as the project’s sign-in settings keep people signed in. The session’s auth_token lasts expires_in seconds, which is one hour or less if the session ends sooner. Trade the refresh_token for the next token before the current token expires.

Request
curl -X POST https://notes.napalmdata.com/sign-in/token \  -H "Content-Type: application/json" \  -d '{ "grant_type": "refresh_token", "refresh_token": "nsr_…" }'

The answer has the same four fields, including a new refresh_token, and the traded refresh token is retired. Napalm accepts the old token again for 30 seconds so a retry can recover a lost answer. Using the old token after those 30 seconds ends the session, so a copied token is caught.

POST /sign-in/revoke
Ends a session. Pass its refresh_token or auth_token as token. Both tokens are refused from then on, and open streams on the session end with session_ended.
POST /sign-in/revoke-all
Ends every session of the person who owns the session in token. Both routes answer 200 with an empty body for any token, so neither route reveals whether a token was real.
A token for a page
A server that holds the session trades the session’s auth_token at POST /sign-in/token with grant_type=urn:ietf:params:oauth:grant-type:token-exchange, subject_token and subject_token_type=urn:ietf:params:oauth:token-type:access_token. The answer is a five-minute token of the same session for the browser. The page token can read, and with scope=database:write can also write. A page token cannot be traded, renewed or used to sign out.
Refusals
The revocation and token routes follow OAuth for refusals and answer 400 with {"error", "error_description"}. The codes are invalid_grant (an ended session, a spent or unknown token), invalid_request, invalid_scope and unsupported_grant_type.
On a stream
A stream ends with token_expired thirty seconds before its token runs out. Reconnect with the next token and the stream’s bookmark to continue from the same point. The client libraries reconnect automatically.
A page’s token
{  "access_token": "eyJhbGciOi…",  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",  "token_type": "Bearer",  "expires_in": 300}

OAuth 2.1 agent delegation#

End users can connect external OAuth-enabled AI clients (such as Claude and ChatGPT connectors) directly to your project’s MCP server so an agent can act on their behalf. When an unauthenticated MCP client connects to /mcp, the 401 response points the client to your project’s OAuth 2.1 metadata (/.well-known/oauth-authorization-server). To enable agent delegation, set agentConsentUrl in your project’s endpoint settings to a consent screen in your web app.

  1. Redirect to your application’s consent page

    When an agent initiates GET /sign-in/agents/authorize, Napalm forwards the browser to your agentConsentUrl with a signed ?request= parameter. Decode the first base64url segment to display the requesting client_name and redirect_uri.

    An agent can request a narrower grant in scope: database:read asks for read-only access, and query: followed by a deployed query’s name asks for that query (such as query:listNotes). Napalm ignores unrecognized scope values, so an agent’s default scope never blocks sign-in, while a query: value for a query unavailable to agents redirects back with error=invalid_scope. Scopes cannot set globals, so the user always picks the tenant on your consent page rather than the agent.

  2. Approve or deny the grant

    The signed-in user selects read-only or read-write access and an expiration window (1, 7, 30 or 90 days). Your consent page posts the user’s decision to POST /sign-in/agents/allow (or POST /sign-in/agents/deny) with the user’s own session token.

    Fetch the agent’s requested permissions with GET /sign-in/agents/request?request=… and the user’s session token. The response returns asked (read, write or unsaid), askedQueries (the agent’s list, or null), the branch’s grantGlobals and queries. queries lists the deployed queries the branch offers agents, each with name, description and isWrite (filtered to askedQueries when the agent requested specific queries). Both the signed request parameter and the user’s session token are required.

    To restrict the agent to chosen tools, pass selected names from queries as allowedQueries (an empty array is rejected). The grant can then call only those queries, and any other query returns 403 forbidden. Omit allowedQueries to allow every deployed query, or to accept askedQueries when the agent supplied a list. A grant cannot widen the agent’s request, so every name in allowedQueries must come from askedQueries when present, and an agent that requested read access always receives a read-only grant.

    To hold the agent to one tenant of your app, send the user’s pick in globals: { 'default::current_org': organization.id }. Each name must be one of the branch’s grantGlobals. Because the user chooses the value, read such a global in a deny rule alone. See Grants held to one tenant.

    To cap how often the agent calls, pass callBudget, such as { calls: 1000, period: 'hour' }. Budgets accept 1 to 1,000,000,000 calls per UTC clock hour or day, and values outside that range are rejected. Calls past the budget are refused until the next UTC period starts, and unknown fields in the request body are rejected.

    Consent page handler
    // On your consent page, with the user signed in and ?request= from the query string:const request = new URL(location.href).searchParams.get('request')const sealed = JSON.parse(atob(request.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')))// sealed.client_name and sealed.redirect_uri name the agent and the agent's return address.const asked = await (await fetch(`https://notes.napalmdata.com/sign-in/agents/request?request=${encodeURIComponent(request)}`, {  headers: { Authorization: `Bearer ${session}` },})).json()// asked.asked is 'read', 'write' or 'unsaid'. asked.askedQueries is the agent's list, or null.// asked.queries holds the queries to offer, and asked.grantGlobals the globals the user may set.const answer = await fetch('https://notes.napalmdata.com/sign-in/agents/allow', {  method: 'POST',  headers: { Authorization: `Bearer ${session}`, 'Content-Type': 'application/json' },  // allowedQueries is optional: left out, the agent may call every deployed query or the agent's own list.  body: JSON.stringify({ request, isReadOnly: true, days: 30, allowedQueries: ['listNotes'] }),})location.href = (await answer.json()).redirectTo
  3. Token issuance and renewal

    Napalm redirects the browser back to the agent with a PKCE authorization code. The agent exchanges the code at POST /sign-in/agents/token for a 1-hour access token and a renewable refresh token. The response’s scope lists the granted permissions: database:read or database:write, followed by query:<name> for each allowed query. Renewing the token preserves the grant’s scope.

Every approved connection creates an ext::napalm::AgentGrant record in your database (exposed on agent requests via global ext::napalm::ClientTokenAgentGrant). Each person can read only their own grants, and deleting a grant revokes the agent’s access immediately. Because API statements cannot reference the grant type directly, declare an alias over it in your schema and query the alias instead, as Watching and stopping an agent shows.

Sign-in route reference#

Method & routeDescription
GET /sign-in#Returns public branding metadata (app name, logos, accent color) and enabled sign-in providers.
GET /sign-in/authorize#Starts an OAuth or OIDC sign-in redirect with PKCE challenge, provider and redirect_to.
GET /sign-in/callback#Receives the OAuth/OIDC callback. Register the callback route with upstream identity providers.
GET /sign-in/token#Exchanges a one-time authorization code and PKCE verifier for a session: auth_token, identity_id, refresh_token and expires_in.
POST /sign-in/token#Renews a session with its refresh_token, or trades a session token for a five-minute page token.
POST /sign-in/revoke#Ends a session given its refresh token or auth token.
POST /sign-in/revoke-all#Ends every session of the person on every device.
GET /sign-in/cli#Completes browser sign-in for your generated project CLI (delivers the code locally or displays it for SSH).
GET /sign-in/agents/authorize#Starts OAuth 2.1 authorization for AI agents and redirects to your agentConsentUrl. scope may ask for database:read and for individual deployed queries via query:<name>.
GET /sign-in/agents/request#Returns the agent’s requested permissions, the branch’s grantGlobals and the deployed queries offered to agents (request, authenticated with the user’s session token).
POST /sign-in/agents/allow#Approves an agent consent request using the user’s session token (request, isReadOnly, days and optional allowedQueries, globals and callBudget).
POST /sign-in/agents/deny#Denies an agent consent request (request).
POST /sign-in/agents/token#Issues OAuth 2.1 tokens for authorization-code exchange, refresh-token renewal and action handlers exchanging a call assertion for a session (RFC 8693).
POST /sign-in/agents/register#Registers OAuth 2.1 agent clients dynamically (RFC 7591).
POST /sign-in/agents/revoke#Revokes the calling OAuth 2.1 agent’s grant token.
POST /sign-in/register#Registers a new user account with email and password.
POST /sign-in/authenticate#Signs in an existing user with email and password.
POST /sign-in/verify#Confirms an email address with a verification code or a verification link’s token.
POST /sign-in/resend-verification-email#Sends the verification code or link again.
POST /sign-in/send-reset-email#Sends a password-reset email.
POST /sign-in/reset-password#Completes a password reset with the reset code or the reset link’s token.
POST /sign-in/magic-link/register#Signs a new user up by magic link.
POST /sign-in/magic-link/email#Sends a magic link to an existing user.
GET /sign-in/webauthn/register/options#Generates a WebAuthn passkey registration challenge.
POST /sign-in/webauthn/register#Verifies and stores a newly registered passkey credential (pass options.user.id as user_handle).
GET /sign-in/webauthn/authenticate/options#Generates a WebAuthn passkey assertion challenge.
POST /sign-in/webauthn/authenticate#Completes sign-in using a signed WebAuthn passkey assertion.

Type a word or two to search every docs page.