# AnyAPI - agent authentication

You are an agent. AnyAPI authenticates every request with a Bearer token: an `aa_live_...` API
key, or an `aa_at_...` OAuth access token. Pick the first method that fits:

1. **You can run shell commands:** use the CLI (`anyapi login`, below).
2. **You have plain HTTP and can talk to the person:** sign them up or in with their email
   (account sign-up, below). They get an account and approve you with a link and a code.
3. **You cannot reach the person:** mint a free trial key (below). No account, no human step.

Never print, log, or commit a token, a `claim_token`, or an `identity_assertion`.

## Account sign-up over HTTP (service_auth)

Discovery: the authorization server metadata at https://api.getanyapi.com/.well-known/oauth-authorization-server
carries an `agent_auth` block naming the endpoints below. `service_auth` is the only
`type` accepted.

### 1. Register with the person's email

Ask the person for the email they use (or want to use) for AnyAPI, then:

    curl -s -X POST https://api.getanyapi.com/agent/identity \
      -H "Content-Type: application/json" \
      -d '{"type":"service_auth","login_hint":"person@example.com","client_name":"My agent"}'

`client_name` is optional: the name the person sees on the approval screen. The response:

    {
      "registration_id": "...",
      "registration_type": "service_auth",
      "claim_url": "https://api.getanyapi.com/agent/identity/claim",
      "claim_token": "aa_clm_...",
      "claim_token_expires": "<24 hours after registration>",
      "post_claim_scopes": [...],
      "claim": { "user_code": "BCDF-GHJK", "verification_uri": "https://getanyapi.com/dashboard/authorize", "expires_in": ..., "interval": ... }
    }

`claim_token` is returned once; hold it in memory until step 3 finishes. A `login_hint` that is not
a plain email address returns `invalid_login_hint`.

### 2. Hand off to the person

Send `claim.verification_uri` and `claim.user_code` in one message, for example:

> Open this link, sign in or sign up with person@example.com, and enter this code: **BCDF-GHJK**

The code goes into the page they land on, not back to you. Only an AnyAPI account that holds
`login_hint` as a verified email can approve; signed in with any other account, the page refuses.
A new account starts with a $0.10 balance, no card required.

### 3. Poll for the token

    curl -s -X POST https://api.getanyapi.com/oauth/token \
      -d grant_type=urn:workos:agent-auth:grant-type:claim \
      -d claim_token=aa_clm_...

Wait `interval` seconds between polls. The answers:

- `authorization_pending`: the person has not finished; poll again.
- `slow_down`: add 5 seconds to the interval.
- `access_denied`: the person declined. Stop.
- `expired_token`: the code expired before the person approved, or the person declined and
  the code has since expired. Get a fresh code (below).
- `invalid_grant`: the claim token is unknown or already used. Start again at step 1.

Once the person approves, the claim stays redeemable until `claim_token_expires`, even after the
code itself expires. On success:

    {
      "access_token": "aa_at_...",
      "token_type": "Bearer",
      "expires_in": 3600,
      "scope": "...",
      "identity_assertion": "aa_ia_...",
      "assertion_expires": "..."
    }

Send `access_token` as the Bearer token. Keep `identity_assertion` for refresh. It is an opaque
token, not a JWT.

**Which interface the token works on.** An access token works on one interface. By default it is
the REST API. For the MCP server, add `-d resource=https://api.getanyapi.com/mcp` to this call or to the
refresh in step 4. `resource=https://api.getanyapi.com` names the REST API explicitly; any
other `resource` returns `invalid_target`. To use both, exchange the
identity assertion once per interface.

**A fresh code.** If the code expired before approval, re-issue it with the same `claim_token`
and email, then hand the new code off again:

    curl -s -X POST https://api.getanyapi.com/agent/identity/claim \
      -H "Content-Type: application/json" \
      -d '{"claim_token":"aa_clm_...","email":"person@example.com"}'

The response carries `status` "initiated", `expires_at`, and a `claim_attempt` block shaped like
`claim`. `claim_expired` means the 24 hours ran out and `invalid_claim_token` that the token is
unknown: start again at step 1. `claimed_or_in_flight` means the person already approved or
declined: poll the token endpoint instead. If that poll still answers `expired_token`, the
person declined or the claim can no longer complete: stop. Start a new registration (step 1,
`POST /agent/identity`) only if the person asks.

Both registration endpoints answer HTTP 429 `rate_limited` when called too often from one
address; wait for `Retry-After`.

### 4. Refresh

When the access token expires, exchange the identity assertion for a new one (add
`-d resource=...` as above for MCP):

    curl -s -X POST https://api.getanyapi.com/oauth/token \
      -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
      -d assertion=aa_ia_...

The response has a new `access_token` and no new assertion; keep the one you have until
`assertion_expires`. `invalid_grant` means the person disconnected you or the assertion expired;
start again at step 1.

## Free trial key (no account, no human step)

The trial key is disposable by design: about **$0.05 of requests**, no account created,
and it **self-expires in 7 days** if never upgraded - nothing lingers, nothing is owed.
Tell your human in one sentence that you are minting it, then proceed without waiting:

    curl -s -X POST https://api.getanyapi.com/agent/signup \
      -H "Content-Type: application/json" \
      -d '{ "label": "my-agent" }'

The response returns `secret` (your `aa_live_...` trial key, shown once), `capUsd` (the
trial budget), `expiresAt`, a `notice` sentence to relay to your human verbatim, and an
`upgrade` object. Store `secret` as `ANYAPI_API_KEY`.

## Authenticate

Send the token as a Bearer token on every request to the REST and MCP interfaces:

    Authorization: Bearer aa_live_...

- REST base URL: https://api.getanyapi.com/v1
- MCP endpoint (streamable HTTP): https://api.getanyapi.com/mcp

For immediate account-backed OAuth from a shell, run `anyapi login`. The CLI prints a
verification URL and user code, then polls while the human approves from any browser or
device. It does not bind a localhost callback. To use an existing dashboard key instead,
run `anyapi login --api-key aa_live_...`.

## Upgrade past the trial (one human approval)

When the trial budget is spent, calls return HTTP 402 with error code `trial_cap_reached`.

- **CLI:** run `anyapi connect` (anyapi-cli >= 0.3.0). It starts an OAuth flow with a
  localhost callback and prints one consent URL. The human opens it, reaches the consent
  screen at https://getanyapi.com/dashboard/authorize, signs in or signs up, sees a
  receipt of what the agent accomplished on the trial, sets a spend limit, and approves.
  The CLI then holds an `aa_at_...` access token and keeps working - nothing to swap by
  hand. On an older CLI without `anyapi connect`, use the dashboard key below.
- **No shell (raw REST or MCP):** the 402 response body carries an upgrade link - relay
  it to the human, or run the account sign-up above.
- **Dashboard key (secondary path):** a human already signed in can create a full,
  non-expiring key directly at https://getanyapi.com/dashboard/keys.

## More

- Agent setup quickstart: https://getanyapi.com/agent-quickstart.md
- Agent onboarding skill: https://getanyapi.com/SKILL.md
- Machine-readable discovery: https://getanyapi.com/.well-known/anyapi.json
- Documentation: https://getanyapi.com/docs
