# Agents

> How an AI agent gets an API key with a person's approval (device authorization), reads its account, handles 402 responses, asks for a checkout link, and stays under the spending caps a person sets.

An AI agent can sign up for an API key by itself, but a person always approves it. The agent asks for a code, the person approves it in the dashboard and chooses what the key may do and how much it may spend, and the agent collects the key. Only people can pay, change a plan or raise a cap.

Agents can also use the [MCP server](https://ringingbytes.com/docs/mcp) with the same key. A [skill file](https://ringingbytes.com/skills/ai-image-check/SKILL.md) teaches a coding agent the steps on this page, and the API serves the same guide at [`/auth.md`](https://api.ringingbytes.com/auth.md).

## Get a key

The flow is the OAuth device authorization grant ([RFC 8628](https://www.rfc-editor.org/rfc/rfc8628)). Neither request needs a key.

1. Ask for a code. `client_name` is shown to the person: a short plain name, without links or email addresses.

   ```sh
   curl -X POST https://api.ringingbytes.com/v1/agent/device/code \
     -H "Content-Type: application/json" \
     -d '{"client_name": "Research notebook", "requested_scopes": ["checks:create", "account:read"], "requested_monthly_cap_usd": 20}'
   ```

   ```json
   {
     "device_code": "…",
     "user_code": "CFMN-LWPQ",
     "verification_uri": "http://localhost:4101/device",
     "verification_uri_complete": "http://localhost:4101/device?user_code=CFMN-LWPQ",
     "expires_in": 900,
     "interval": 5
   }
   ```

   Keep `device_code` secret: it is how you collect the key.

2. Show the person `verification_uri_complete` and the `user_code`. They sign in or create an account, check that the page shows the same code, choose the permissions and a monthly cap, and approve. The request expires after 15 minutes.

3. Poll for the key every `interval` seconds:

   ```sh
   curl -X POST https://api.ringingbytes.com/v1/agent/device/token \
     -H "Content-Type: application/json" \
     -d '{"device_code": "<device_code>"}'
   ```

   Until the person decides, the answer is HTTP 400:

   ```json
   {
     "defined": false,
     "code": "authorization_pending",
     "status": 400,
     "message": "Waiting for a person to approve the request.",
     "data": {
       "error": "authorization_pending",
       "error_description": "Waiting for a person to approve the request.",
       "interval": 5
     },
     "error": "authorization_pending",
     "error_description": "Waiting for a person to approve the request."
   }
   ```

   | `code` | What to do |
   | --- | --- |
   | `authorization_pending` | Keep polling. |
   | `slow_down` | You polled too fast. Add 5 seconds to your interval (`data.interval` has the new one). |
   | `access_denied` | The person denied the request. Stop. |
   | `expired_token` | Fifteen minutes passed. Start again at step 1. |
   | `invalid_grant` | Unknown device code, or the key was already collected. |

4. Once approved, the next poll returns the key, once:

   ```json
   {
     "access_token": "rb_live_…",
     "token_type": "Bearer",
     "scope": "checks:create account:read",
     "api_key": "rb_live_…",
     "key_id": "UvcTyXNUOSs2ewIj5T3ViuPgkFBhflc9",
     "organization_id": "x3l3yw7sjuhu23l4x16anvefe4pookoc",
     "scopes": ["checks:create", "account:read"],
     "free_checks_remaining": 0,
     "monthly_cap_usd": 5,
     "account_url": "https://api.ringingbytes.com/v1/account",
     "docs_url": "https://api.ringingbytes.com/auth.md"
   }
   ```

   `access_token` and `api_key` are the same key. Store it now, for example as `RINGING_BYTES_API_KEY`: it is never shown again. The person may have granted fewer permissions or a different cap than you asked for.

Limits per IP address: 10 code requests an hour, and 3 waiting for a person at once. Over them you get 429 `TOO_MANY_REQUESTS` or `TOO_MANY_PENDING_REQUESTS`.

The key then works like any other: send it as `Authorization: Bearer` to [`POST /v1/detect`](https://ringingbytes.com/docs/detect/images).

## Permissions

The person approving can grant fewer permissions than you asked for, never more. Without `requested_scopes` you ask for the first two.

| Scope | Allows |
| --- | --- |
| `checks:create` | Checking images and reading this organization's results. Uses its image checks and may be billed. |
| `account:read` | `GET /v1/account`: the plan, the free and included image checks left, and spending. |
| `billing:checkout` | `POST /v1/billing/checkout`: a checkout link, also emailed to the organization's owners. |

Keys created in the dashboard have all three. A request without the right scope gets 403 `INSUFFICIENT_SCOPE` (see [Errors](https://ringingbytes.com/docs/errors#missing-permission)).

## Spending caps

Each agent key can have a monthly cap, and the organization can have one too. A cap counts only billed checks in the current billing period: free and included image checks don't count. When the next check would go over a cap, the API answers 402 with `key_cap_reached` or `org_cap_reached`, and checks that cost nothing still run.

Only owners and admins can set or raise caps, in the dashboard (**API keys** for a key, **Settings > Billing** for the organization). No API endpoint can change one. Owners get an email at 50, 80 and 100 percent of a cap.

## Read the account

```sh
curl https://api.ringingbytes.com/v1/account \
  -H "Authorization: Bearer $RINGING_BYTES_API_KEY"
```

```json
{
  "object": "account",
  "organization": { "id": "x3l3yw7sjuhu23l4x16anvefe4pookoc", "name": "Test's organization" },
  "plan": { "id": "free", "name": "Free", "family": "api", "subscription_status": "none" },
  "free_checks_remaining": 0,
  "included_checks": { "per_period": 0, "remaining": 0 },
  "period": { "start": "2026-11-01T00:00:00.000Z", "end": "2026-12-01T00:00:00.000Z" },
  "can_run_checks": true,
  "payment_failed": false,
  "helpdesk_scanning_allowed": true,
  "api_key": {
    "id": "UvcTyXNUOSs2ewIj5T3ViuPgkFBhflc9",
    "scopes": ["checks:create", "account:read"],
    "created_via": "agent",
    "monthly_cap_usd": 5,
    "spend_usd": 0
  },
  "organization_cap": { "monthly_cap_usd": null, "spend_usd": 0.678 },
  "billing_url": "http://localhost:4101/billing"
}
```

`can_run_checks` is false when checks are refused for money (free checks used, or a failed payment). `api_key` and `organization_cap` show each cap and the billed spend so far this period. Needs `account:read`.

## When a check returns 402

A 402 keeps `code: "PAYMENT_REQUIRED"` and says why in `data.reason` and what to do in `data.next`:

```json
{
  "defined": false,
  "code": "PAYMENT_REQUIRED",
  "status": 402,
  "message": "The free image checks are used. Choose a plan in the dashboard (Billing) to keep making requests.",
  "data": {
    "reason": "free_checks_exhausted",
    "free_checks_remaining": 0,
    "cap": null,
    "next": {
      "action": "choose_plan",
      "who": "agent_or_human",
      "description": "The free image checks are used. Ask for a checkout link (POST checkout_url with a plan) and give it to a person to pay, or ask a person to choose a plan in the dashboard. Then poll account_url until can_run_checks is true.",
      "checkout_url": "https://api.ringingbytes.com/v1/billing/checkout",
      "plans": ["payg", "pro", "business", "helpdesk_starter", "helpdesk_growth", "helpdesk_scale"],
      "pricing_url": "https://api.ringingbytes.com/pricing.md",
      "dashboard_url": "http://localhost:4101/billing",
      "account_url": "https://api.ringingbytes.com/v1/account",
      "docs_url": "https://api.ringingbytes.com/auth.md"
    }
  }
}
```

| `reason` | `next.action` | `next.who` | What to do |
| --- | --- | --- | --- |
| `free_checks_exhausted` | `choose_plan` | `agent_or_human` | Ask for a [checkout link](#ask-for-a-checkout-link) and give it to your person, or ask them to choose a plan. |
| `payment_failed` | `update_payment_method` | `human` | Ask your person to update the payment method at `next.dashboard_url`. |
| `key_cap_reached` | `raise_key_cap` | `human` | Wait for the next billing period, or ask an owner or admin to raise the key's cap. |
| `org_cap_reached` | `raise_organization_cap` | `human` | Wait for the next billing period, or ask an owner or admin to raise the organization's cap. |

For a cap, `data.cap` has `scope` (`key` or `organization`), `monthly_cap_usd`, `spend_usd` and `resets_at`. Don't retry a 402 in a loop: nothing changes until a person acts or the period renews. Prices are at [`/pricing.md`](https://api.ringingbytes.com/pricing.md).

## Ask for a checkout link

With `billing:checkout`, an agent can ask for a hosted checkout for an organization without a plan:

```sh
curl -X POST https://api.ringingbytes.com/v1/billing/checkout \
  -H "Authorization: Bearer $RINGING_BYTES_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"plan": "payg"}'
```

The answer is `{ "url", "expires_at", "plan", "emailed_to_owners", "owner_emails": { "owners", "sent" } }`. The same link is emailed to the organization's owners; `emailed_to_owners` is `true` only when every owner's email went out (failed emails aren't retried). Either way, give the link to your person: only a person can pay. Then poll `GET /v1/account` until `can_run_checks` is true.

An organization that already has a plan gets 409 `SUBSCRIPTION_EXISTS`: agents can't change or cancel plans. At most 5 links an hour per organization.
