Authentication and scopes
Two credentials reach a secured route. Pick one by where the code runs.
| Credential | Transport | Who uses it | Lifetime |
|---|---|---|---|
| Bearer token | Authorization: Bearer <token> | Staff portals and partner rescue integrations | One hour, with a refresh token that lasts thirty days |
| API key | X-API-Key: <key> | Server jobs and POS register terminals | Until an administrator rotates it |
Both credentials work on the sandbox gateway, and each secured route verifies whichever one it accepts. The two are alternatives, not a sequence. Send an X-API-Key on a retail route and the call passes without a token.
Five routes stay open to everyone. GET /animals, GET /animals/{animalId}, POST /applicants, POST /sessions, and POST /webhooks/test-dispatch carry no security requirement. A prospective adopter browses the catalog before owning an account, and a receiver test needs no credential.
Sign in from the reference
The sandbox gateway runs the authorization server, so the Connect button completes a real sign-in.
- Open the interactive reference. The dropdown lists Sandbox gateway first, so pick it if another target shows.
- Open the authentication panel and press Connect on
bludhaven_oauth. - Approve the consent screen. It lists the scopes the client asked for, and the Approve and Deny buttons both work.
- The console returns with a token. Send a secured request and the gateway verifies the signature, the expiry, and the scope.
The consent screen lives on the gateway, so the flow leaves the page for a moment. If the reference runs inside an embed and the popup can't return the code, open the standalone reference and sign in there.
The authorization code flow
The gateway publishes both endpoints, and the contract declares them under bludhaven_oauth.
- The client generates a verifier and sends
code_challengewith the SHA-256 hash of that verifier tohttps://sandbox.bludhaven-pets.workers.dev/oauth/authorize. - The staff member approves the requested scopes on the consent screen.
- The gateway redirects to the client with an authorization code, valid for two minutes.
- The client posts the code and the original verifier to
https://sandbox.bludhaven-pets.workers.dev/oauth/token. - The response carries
access_token,token_type,expires_in, the grantedscope, and arefresh_token.
PKCE matters here because a partner integration runs on a device nobody controls. An intercepted code is useless without the verifier.
The client ID is bludhaven-staff-portal. A public demo can't keep a secret, so this client authenticates with PKCE alone and no secret exists to leak.
Mint a token with curl
A script has no browser to approve a consent screen. The client credentials grant mints a token in one POST.
curl -X POST https://sandbox.bludhaven-pets.workers.dev/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=bludhaven-staff-portal" \
-d "scope=read:animals write:animals"
{
"access_token": "blud_at_eyJzdWIiOiJjbHVk...",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "read:animals write:animals"
}
Omit scope and the gateway grants the four read scopes. Ask for a scope outside the catalog and the call returns 400 with invalid_scope.
Every later call repeats the token in the header.
curl https://sandbox.bludhaven-pets.workers.dev/animals/pet_krypto_210/health-records \
-H "Authorization: Bearer <ACCESS_TOKEN>"
That route wants read:animals, so the token above opens it. A token holding only write:animals returns 403 instead.
Refresh a token
Post the refresh token before the access token expires. The gateway returns a new pair and the old access token stops mattering.
curl -X POST https://sandbox.bludhaven-pets.workers.dev/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token" \
-d "refresh_token=<REFRESH_TOKEN>" \
-d "client_id=bludhaven-staff-portal"
A client credentials token carries no refresh token, because the client can mint another one at any time.
Authenticate with an API key
A register terminal and a nightly job have no browser and no session. Each carries a key, and each key covers only the routes its job needs.
The sandbox publishes two working keys. A test credential has to be copyable, so these live in the open:
| Key | Reaches | Typical caller |
|---|---|---|
bpk_sandbox_pos_7f3a91c4 | read:retail, write:retail | Point-of-sale register |
bpk_sandbox_ops_2d8be05f | read:animals, write:animals, write:adoptions, read:retail | Nightly integration job |
Send the key in the header and skip every token step:
curl https://sandbox.bludhaven-pets.workers.dev/retail/inventory \
-H "X-API-Key: bpk_sandbox_pos_7f3a91c4"
The register key reads stock and places supply orders. Point the same key at an animal write and the gateway refuses:
curl -X POST https://sandbox.bludhaven-pets.workers.dev/animals \
-H "X-API-Key: bpk_sandbox_pos_7f3a91c4" \
-H "Content-Type: application/json" \
-d '{"name": "Orion", "species": "dog", "microchip_id": "985141002348999", "intake_branch_id": "loc_harbor_04"}'
{
"type": "https://api.bludhavenpets.com/errors/insufficient-scope",
"title": "Insufficient Scope",
"status": 403,
"detail": "This API key lacks write:animals. Use a key assigned that scope, or send an OAuth token.",
"instance": "/animals"
}
Swap in the ops key and the same request returns 201. Keys carry scope sets rather than the whole catalog: a register left on a counter overnight shouldn't be able to withdraw an animal record.
An unrecognized value returns 401, and the challenge names the scheme rather than the token:
WWW-Authenticate: ApiKey realm="bludhaven", error="invalid_key"
Rotate a key in three steps:
- Issue the replacement.
- Deploy the new value.
- Remove the old key after the last caller moves.
A key that rotates on a Friday evening leaves a register unable to sell on Saturday.
When a call fails
The gateway answers two failures, and the header tells them apart.
| Status | Meaning | Challenge |
|---|---|---|
401 | No credential, a forged or expired token, a retired session, or a key outside the registry | Bearer … error="invalid_token", or ApiKey … error="invalid_key" |
403 | A valid credential that lacks a scope the route requires | error="insufficient_scope", scope="write:animals" |
The 403 body names the missing scope in detail. Request that scope at authorization time, then retry. A staff portal that hits this in production asked for too little during sign-in.
The 401 body names the cause in prose, so a reader learns which credential the route wanted. A route that takes only a token refuses a key with That route takes an OAuth bearer token, not an API key.
The scope catalog
Each scope grants one job. Request the narrow set a workflow needs, because a partner token with admin:adoptions can override a live hold.
| Scope | Grants |
|---|---|
read:animals | Animal records, intake history, and adoption eligibility |
write:animals | Creating, amending, and withdrawing animal records |
read:adoptions | Holds and adoption applications |
write:adoptions | Hold placement, hold release, and application submission |
read:applicants | Applicant accounts during counselor review |
write:applicants | Creating and amending applicant accounts |
vet:clearance | Filing veterinary certificates. Licensed practitioners only. |
read:retail | Inventory levels and supply orders |
write:retail | Placing and canceling supply orders |
admin:adoptions | Overriding hold conflicts and issuing refunds on any cost line |
Adopter sessions
An applicant never sees a scope. The public portal exchanges email and password for a short-lived token.
curl -X POST https://sandbox.bludhaven-pets.workers.dev/sessions \
-H "Content-Type: application/json" \
-d '{"email": "nadia.okafor@fastmail.com", "password": "<PASSWORD>"}'
{
"token": "blud_sess_eyJzdWIiOiJ1c3JfOTkyMWJjIn0.9c1f4d2b7a",
"expires_at": "2027-01-15T21:04:11Z",
"scope": "read:applicants write:applicants"
}
The gateway mints a fresh token on every call, so copy the value from your own response. The example above shows the shape, and that shape isn't a working credential.
The portal holds the token in memory and never writes it to storage. It reaches the applicant reads and nothing else, so a stolen session can't touch intake or retail. DELETE /sessions/current retires the token, and a later call with it returns 401.
Which target validates what
| Target | Credential check |
|---|---|
| Browser demo | None. Every route answers without a credential. |
| Sandbox gateway | Token signature, expiry, and scope, or a registry lookup and scope check for a key. A bad credential returns 401, and a missing scope returns 403. |
| Production | The same checks, plus a revocable client registry and an audit trail per key. |
Production adds nothing a reader can't exercise on the sandbox, because the demo ships a real authorization server.
Schemes the platform supports
Four credentials reach the API. The contract declares three as security schemes, and the fourth rides the bearer scheme as a token type.
| Scheme in the contract | What it identifies | Where it fits |
|---|---|---|
bludhaven_oauth, authorization code with PKCE | A staff member or partner, with a consent step | Staff portals, partner rescue integrations |
bludhaven_oauth, client credentials | A service, with no human present | Nightly jobs, reconciliation scripts |
api_key | A device or integration, with a fixed scope set | POS registers, server-to-server sync |
Bearer token from POST /sessions | An adopter, for one browser session | The public adoption portal |
OAuth covers an integration that needs a credential from a person, and a key covers a credential that lives on a device.
What production adds
Two capabilities sit behind the production gateway, because a public demo can't carry them.
- Staff single sign-on. OpenID Connect delegates sign-in to the municipal identity provider, and the token exchange stays the same.
- Mutual TLS. Back-office peers present a client certificate on a custom domain. Cloudflare's
workers.devhost doesn't accept one, so the sandbox can't demonstrate it.
Outbound webhooks use HMAC request signing (X-Bludhaven-Signature), and the sandbox provides an endpoint that accepts live test dispatches (webhooks guide).
HTTP Basic doesn't appear here. A base64 pair travels as plain text on every request, and a bearer token covers the same server-to-server case with a scope attached.
Request credentials
Write to budbaker@bludhavenpets.com for a partner client ID or an assigned API key. Name the scopes the integration needs and the branch it serves. Include the X-Request-Id from any failing response.