air-kya-edge
The example repository for this guide: a small Worker, a reusable KYA module, an example agent and origin, and tests for every rejection.
What you build
The Worker verifies an SD-JWT presentation that the agent obtained from AIR, offline, on every request it protects. It never calls AIR per request and never trusts a status relayed by the agent. Each request is classified, then handled by the rule for its path:- Gate a path to serve it only to verified agents, such as member-only content or a limited offer.
- Triage a path to serve everyone but let your origin tailor the response, such as member prices, higher rate limits, or a prompt to verify.
anonymous means the caller sent no proof. It is not a proof of humanity: people and unverified bots both land there. If you need that distinction, combine it with Cloudflare Bot Management.Before you start
- A verification program. In the AIR Dashboard, create an
SD_JWT_VCverification program for the credential you accept. Save its program ID, the issuer DID (did:web:…) and the credential type (vct). See Verifying credentials. - A Cloudflare account with your site’s domain on Cloudflare, or a
workers.devsubdomain for testing. - Node.js 20 or later.
Build and run it
1
Clone the repository and run it locally
npm run setup creates a local test issuer and a .dev.vars that trusts it. Then run the example origin and the edge in two terminals:2
Try each outcome
The example agent runs the full agent flow.
--fixture signs the credential with the local test issuer instead of calling AIR; the edge checks are the same as in production.3
Set your policy and rules
Edit
kya.config.ts. The policy mirrors your verification program; the rules decide where it applies.kya.config.ts
fieldsToDiscloseis exactly what the agent must reveal: no more, no less. Ask only for the claims your decision needs.conditionssupport=,!=,>,>=,<,<=andin. Each condition’s attribute must be infieldsToDisclose.- A rule
pathis exact (/pricing) or a prefix ending in*(/members/*). Modes aregate,triageandoff.
4
Deploy in front of your site
Set your program ID, issuer and credential type as Worker vars in With
wrangler.jsonc, and add a route for your domain:wrangler.jsonc
ORIGIN_URL empty, the Worker forwards to your zone’s existing origin. Set it only to proxy a different host, for example while testing on workers.dev.5
Read the decision in your origin
The edge removes every caller-supplied For an origin behind Cloudflare’s proxy, Authenticated Origin Pulls or a Cloudflare Tunnel can also keep traffic from bypassing the edge.
X-AIR-* header, including the proof, then adds its own:Your origin should accept these headers only from the edge. Refuse any request without the secret, and compare it in constant time:
What the agent does
An agent needs a delegated identity and an agent session first. See Using Agentic Identity. For each protected request, it then:-
Reads
GET /.well-known/air-kyato learn the program, required claims and accepted issuers. -
Gets a nonce from
POST /kya/challenge. Each nonce expires after 60 seconds and works once. -
Calls Verify a credential with the site’s program, fields and nonce:
-
Sends the request with
X-AIR-KYA: <proof>.
examples/agent.ts runs these steps against a live site when you set AIR_AGENT_JWT:
403 with a Link: </.well-known/air-kya>; rel="service-desc" header, so an agent that arrives without a proof can find out what to present.
What the edge checks
The Worker runs the SD-JWT backend checks in this order. The first failure decides the reason code.- The issuer is in
trustedIssuers. Its ES256 key comes from itsdid:webdocument (assertionMethod, matchingkid). - The issuer signature,
iat,expandvctare valid. - The disclosed claims are exactly
fieldsToDisclose, and each matches a digest the issuer signed. - The key-binding JWT is signed by the holder key in
cnf.jwk, hastypkb+jwtandaudequal to the program ID, is at most 5 minutes old, and itssd_hashcovers this presentation. - The nonce was issued by this site and is consumed now, atomically, in a Durable Object.
- The policy conditions hold for the disclosed claims.
src/kya/ and has no Cloudflare dependency apart from the nonce store. To run it in other middleware, such as Express or Next.js, pass your own consumeNonce backed by a store with an atomic delete.
Reason codes
X-AIR-Reason carries one of these fixed codes. Tokens and library errors are never echoed.
Track these codes in your Worker logs. A spike in
nonce_reused_or_unknown or invalid_credential from one source is a signal for your abuse controls.
Before production
- Revocation is not checked. If your issuer publishes a Token Status List, add a status check. See Revocation.
- Rate-limit
/kya/challengewith a Cloudflare rate-limiting rule. Each call stores a nonce for 60 seconds. - Expect one proof per request. A presentation is single-use. For high traffic, shard the nonce store.
- Do not cache across caller classes. The Worker sends responses to callers with a proof as
private, no-storeand fetches them withcache: "no-store". If you add your own caching, key it onX-AIR-Viewer. - Keep the policy under your control. Never let a caller choose the program, issuer or origin.
- Know the binding’s scope. The proof is bound to your nonce and program, not to the HTTP connection. Request signing, such as Web Bot Auth (RFC 9421), is out of scope.
Going further
- People, too. The air-kya demo store runs the same checks for people who sign in with AIR Kit and verify the same credential in the browser. A verified person and their verified agent see the same catalog.
- Decide what to gate. Agent Gating covers which claims to ask for and how to combine them with your abuse controls.
- Offer a benefit. Agentic Commerce covers member pricing and other benefits applied after a successful check.