Sign in with AnyRouter

Let users sign in to your app with their AnyRouter account and run AI requests billed to them, with no API keys to collect or store.

Add AI to your app without holding users' API keys. Each user signs in with OAuth 2.1 authorization code and PKCE, and your app gets a temporary, inference-only key billed to that user's own account (their credits or free tier). Your app is a public client: no client secret, so it works from a browser-only app. For minting an extra key for your own local tool, see OAuth: mint a key via PKCE.

Quickstart

Registration is open and needs no AnyRouter account. Redirect URIs must match exactly and use https (localhost is allowed for development).

bash
curl https://anyrouter.dev/api/v1/mcp/oauth/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "My AI App",
    "redirect_uris": ["https://myapp.example/callback"],
    "app_type": "signin",
    "origin_url": "https://myapp.example"
  }'

The response includes a public client_id, safe to embed in frontend code.

Create a PKCE verifier and S256 challenge in the browser, keep the verifier and state (for example in sessionStorage), then redirect:

js
const b64url = (b) => btoa(String.fromCharCode(...new Uint8Array(b)))
  .replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")
const verifier = b64url(crypto.getRandomValues(new Uint8Array(32)))
const challenge = b64url(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier)))
const state = b64url(crypto.getRandomValues(new Uint8Array(16)))
sessionStorage.setItem("ar_verifier", verifier)
sessionStorage.setItem("ar_state", state)

const url = new URL("https://anyrouter.dev/api/v1/mcp/oauth/authorize")
url.search = new URLSearchParams({
  client_id: CLIENT_ID,
  redirect_uri: REDIRECT_URI,
  response_type: "code",
  code_challenge: challenge,
  code_challenge_method: "S256",
  state,
}).toString()
location.href = url.toString()

The user sees your app's name and the requested permissions, and approves.

The user returns to redirect_uri with ?code=...&state=...&iss=https://anyrouter.dev. Check state, then exchange:

js
const params = new URLSearchParams(location.search)
if (params.get("state") !== sessionStorage.getItem("ar_state")) throw new Error("State mismatch")

const res = await fetch("https://anyrouter.dev/api/v1/mcp/oauth/token", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    grant_type: "authorization_code",
    code: params.get("code"),
    redirect_uri: REDIRECT_URI,
    client_id: CLIENT_ID,
    code_verifier: sessionStorage.getItem("ar_verifier"),
  }),
})
const token = await res.json()
// { "access_token": "sk-ar-v1-...", "token_type": "Bearer",
//   "scope": "inference read:profile", "expires_in": 2592000 }

Send Authorization: Bearer <access_token> to https://anyrouter.dev/api/v1/chat/completions (or any inference endpoint); the user is billed. To show "Signed in as ...", call GET https://anyrouter.dev/api/v1/me with the same header for display name, avatar and plan.

Permissions

A sign-in token has exactly two scopes; requesting more has no effect.

ScopeAllows
inference/api/v1/chat/completions, /messages, /responses, /embeddings, billed to the user
read:profileGET /api/v1/me

It cannot create or revoke keys, read usage or billing, or change account settings.

Lifetime, revocation and MCP

  • Tokens last 30 days. There are no refresh tokens: on 401, re-run the sign-in flow (a signed-in user passes consent in seconds).
  • Users revoke your app under Dashboard → Connected apps. That invalidates all its tokens for that user, and requests return 401.
  • The token also works on the MCP server at https://anyrouter.dev/api/v1/mcp: core read-only tools, plus Firecrawl tools when the user has Firecrawl connected. Management tools and the user's other MCP connections are not exposed.

Best practices

  • Store one token per user and never share one across users.
  • Keep the state check. Treat every 401 as "re-authenticate".
  • Every redirect from AnyRouter, including errors, carries iss=https://anyrouter.dev (RFC 9207). Reject a different iss if your app uses several OAuth servers.
  • Omit resource, or send https://anyrouter.dev. Any other value, including the MCP server URL, returns invalid_target.
  • Redirect URIs in the authorize and token requests must match a registered one exactly.
  • Never log sk-ar-v1-... tokens.

AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer