# auth.md: credentials for AI agents at ScrapeBadger

> How an AI agent gets a ScrapeBadger API key or OAuth token: discover what each surface accepts, pick a method, register, hand the claim to your human, and use the credential. Everything here is live at https://scrapebadger.com.

ScrapeBadger has two protected surfaces:

| Surface | Credential | Header |
|---|---|---|
| REST API, `https://scrapebadger.com/v1/*` | API key | `X-API-Key: <key>` |
| Hosted MCP server, `https://mcp.scrapebadger.com/mcp` | OAuth 2.1 access token, or an API key | `Authorization: Bearer <token or key>` |

## Step 1 — Discover

### 1a. Fetch the Protected Resource Metadata

The MCP server is an OAuth 2.1 protected resource. An unauthenticated request gets `401` with a `WWW-Authenticate` header that points at its metadata:

```http
WWW-Authenticate: Bearer resource_metadata="https://mcp.scrapebadger.com/.well-known/oauth-protected-resource"
```

That document (RFC 9728) lists the authorization server, the supported scopes and `bearer_methods_supported: ["header"]`.

### 1b. Fetch the Authorization Server metadata

`https://scrapebadger.com/.well-known/oauth-authorization-server` (RFC 8414) gives the authorization, token and registration endpoints. It supports PKCE (`S256`), Client ID Metadata Documents and dynamic client registration.

The REST API does not use OAuth. A `401` from `/v1/*` means the `X-API-Key` header is missing or the key is invalid. Its `RateLimit-Policy` header shows how many failed attempts are allowed before the credential is blocked for a while.

## Step 2 — Pick a method

1. **Your client speaks MCP and can open a browser for your human:** use OAuth 2.1 against the MCP server (Step 3, OAuth). Claude, ChatGPT, Cursor and Codex do this for you.
2. **Your human has a ScrapeBadger account and you have a shell or HTTP:** use device sign-in (Step 3, device sign-in). Your human approves the request and you receive a new API key. No key passes through the conversation.
3. **There is no account yet:** mint an anonymous trial key yourself (Step 3, anonymous trial key). Your human can claim it later (Step 4).

About the WorkOS auth.md extension: ScrapeBadger does not publish an `agent_auth` block, an `identity_endpoint` or a `claim_endpoint`. It does not accept `identity_assertion` (ID-JAG, `urn:ietf:params:oauth:token-type:id-jag`) or `service_auth` registrations. Use the methods on this page.

## Step 3 — Register

### Anonymous trial key (no account)

1. `POST https://scrapebadger.com/v1/public/agent/trial/challenge` returns `{"challenge", "difficulty"}`.
2. Find a decimal `nonce` such that `sha256(challenge + ":" + nonce)` has at least `difficulty` leading zero bits.
3. `POST https://scrapebadger.com/v1/public/agent/trial` with `{"challenge", "nonce", "label"}` returns `201`:

```json
{
  "api_key": "sb_live_...",
  "key_id": "...",
  "credits": 50,
  "expires_at": "2026-12-08T12:00:00+00:00",
  "max_concurrency": 1,
  "bound_ip": "203.0.113.7",
  "scopes": ["google", "amazon", "..."],
  "claim_url": "https://scrapebadger.com/claim-key?token=...",
  "notice": "I minted a free ScrapeBadger trial key ..."
}
```

A trial key has 50 credits and expires after 7 days. It covers read APIs only (its `scopes`) and runs one call at a time. It works only from the IP that minted it, so it does not work through the hosted MCP server. Each IP gets one per day, and none from proxy, VPN or Tor networks. The CLI does all of this with `scrapebadger trial`.

### Device sign-in (your human's account)

1. `POST https://scrapebadger.com/v1/public/agent/device/code` with `{"client_name", "label"}` returns `device_code`, `user_code`, `verification_uri` (https://scrapebadger.com/device), `verification_uri_complete`, `expires_in` and `interval`.
2. Show your human the `user_code` and the `verification_uri`. They sign in, check the request and approve it.
3. Continue with Step 5.

### OAuth 2.1 (MCP clients)

Register as a client: send an HTTPS client id (Client ID Metadata Document), or use dynamic client registration at `https://scrapebadger.com/api/oauth/register`. Then send your human to `https://scrapebadger.com/oauth/authorize` with PKCE (`code_challenge_method=S256`), `resource=https://mcp.scrapebadger.com` and `scope=scrapebadger`.

## Step 4 — Claim ceremony

Only a trial key needs a claim.

### 4a. Get the ceremony materials

The trial response carries a `claim_url` and a `notice` written for your human. `GET /v1/account/me` with the trial key also returns `agent_trial.claim_url`.

### 4b. Hand off to the user

Show your human the `notice` (or the `claim_url`). They open it, sign in or sign up, and accept the terms. The key then belongs to their account as a normal API key: their credits, their plan's limits and APIs. Unused trial credits do not carry over.

### 4c. Poll for completion

Call `GET https://scrapebadger.com/v1/account/me` with the same key. While the key is a trial, the response has an `agent_trial` object (credits left, expiry and the claim URL). Once your human has claimed it, `agent_trial` is `null`. The key itself does not change.

## Step 5 — Exchange the assertion

- **Device sign-in:** poll `POST https://scrapebadger.com/v1/public/agent/device/token` with `{"device_code"}` every `interval` seconds. Until your human answers, you get `authorization_pending` (or `slow_down`: wait longer). Then you get `{"api_key", "key_id", "key_name"}`, exactly once. You can also get `access_denied` or `expired_token`.
- **OAuth:** exchange the authorization code at `https://scrapebadger.com/api/oauth/token` (`grant_type=authorization_code` with your PKCE `code_verifier`). The token response has an access token and a refresh token. Refresh with `grant_type=refresh_token`.
- **Trial key:** nothing to exchange. The mint response is the key.

## Step 6 — Use the access_token

- REST: `curl -H "X-API-Key: $SCRAPEBADGER_API_KEY" https://scrapebadger.com/v1/account/me`
- MCP: `Authorization: Bearer <access_token>`, or `Authorization: Bearer <api key>` for clients without OAuth.

A key may be limited to some APIs (its permissions) and to some IPs. Each operation in https://scrapebadger.com/v1/openapi.json names the permission it needs.

## Errors

| Where | Status and `error` | What to do |
|---|---|---|
| Any `/v1` call | `401` | The key is missing or invalid. Don't retry the same key in a loop: too many failures block it for a while (`429` with `Retry-After`; the limit is in `RateLimit-Policy`) |
| Any `/v1` call | `403 insufficient_scope` | The key isn't allowed to call this API. Use another key, or ask your human to grant the API |
| Any `/v1` call | `403 ip_not_allowed` | The key is limited to other IP addresses |
| Any `/v1` call | `402` | Out of credits: the body has `upgrade_url` (or `claim_url` for a trial key) |
| Any `/v1` call | `429 concurrency_limit_exceeded` | Too many calls at once. Wait `Retry-After` and retry. Not billed |
| Trial | `429 too_many_challenges`, `429 trial_limit_reached` | This IP or network already has its trial for today |
| Trial | `503 trial_capacity_reached` | Today's trials are taken. Ask your human to sign up: https://scrapebadger.com/auth/signup |
| Trial | `400 invalid_proof`, `400 challenge_expired`, `409 challenge_used` | Request a new challenge and solve it again |
| Device sign-in | `authorization_pending`, `slow_down`, `access_denied`, `expired_token` | Keep polling, slow down, stop, or start over |

## Revocation

- **API keys:** your human deletes or rotates a key in the dashboard at https://scrapebadger.com/api-keys. A deleted key answers `401` at once. Keys can also carry an expiry date.
- **Trial keys:** expire on their own after 7 days unless claimed.
- **MCP OAuth grants:** they can't be revoked from the dashboard yet. Email support@scrapebadger.com to revoke one.

More: https://scrapebadger.com/agents.md · https://docs.scrapebadger.com/authentication · https://docs.scrapebadger.com/ai-agents
