Skip to main content

Authentication and scopes

Two credentials reach a secured route. Pick one by where the code runs.

CredentialTransportWho uses itLifetime
Bearer tokenAuthorization: Bearer <token>Staff portals and partner rescue integrationsOne hour, with a refresh token that lasts thirty days
API keyX-API-Key: <key>Server jobs and POS register terminalsUntil 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.

  1. Open the interactive reference. The dropdown lists Sandbox gateway first, so pick it if another target shows.
  2. Open the authentication panel and press Connect on bludhaven_oauth.
  3. Approve the consent screen. It lists the scopes the client asked for, and the Approve and Deny buttons both work.
  4. 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.

  1. The client generates a verifier and sends code_challenge with the SHA-256 hash of that verifier to https://sandbox.bludhaven-pets.workers.dev/oauth/authorize.
  2. The staff member approves the requested scopes on the consent screen.
  3. The gateway redirects to the client with an authorization code, valid for two minutes.
  4. The client posts the code and the original verifier to https://sandbox.bludhaven-pets.workers.dev/oauth/token.
  5. The response carries access_token, token_type, expires_in, the granted scope, and a refresh_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"
200 OK
{
"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:

KeyReachesTypical caller
bpk_sandbox_pos_7f3a91c4read:retail, write:retailPoint-of-sale register
bpk_sandbox_ops_2d8be05fread:animals, write:animals, write:adoptions, read:retailNightly 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"}'
403 Forbidden
{
"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:

  1. Issue the replacement.
  2. Deploy the new value.
  3. 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.

StatusMeaningChallenge
401No credential, a forged or expired token, a retired session, or a key outside the registryBearer … error="invalid_token", or ApiKey … error="invalid_key"
403A valid credential that lacks a scope the route requireserror="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.

ScopeGrants
read:animalsAnimal records, intake history, and adoption eligibility
write:animalsCreating, amending, and withdrawing animal records
read:adoptionsHolds and adoption applications
write:adoptionsHold placement, hold release, and application submission
read:applicantsApplicant accounts during counselor review
write:applicantsCreating and amending applicant accounts
vet:clearanceFiling veterinary certificates. Licensed practitioners only.
read:retailInventory levels and supply orders
write:retailPlacing and canceling supply orders
admin:adoptionsOverriding 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>"}'
200 OK
{
"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​

TargetCredential check
Browser demoNone. Every route answers without a credential.
Sandbox gatewayToken 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.
ProductionThe 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 contractWhat it identifiesWhere it fits
bludhaven_oauth, authorization code with PKCEA staff member or partner, with a consent stepStaff portals, partner rescue integrations
bludhaven_oauth, client credentialsA service, with no human presentNightly jobs, reconciliation scripts
api_keyA device or integration, with a fixed scope setPOS registers, server-to-server sync
Bearer token from POST /sessionsAn adopter, for one browser sessionThe 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.dev host 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.