Cloudflare
Add KYA token enforcement to a website or API you already serve through Cloudflare, using a Cloudflare Worker and optional WAF rules.
This guide is for teams already running on Cloudflare who want to add KYA token-based access control to their existing delivery stack. It uses a Cloudflare Workers function, a serverless function that runs at Cloudflare's edge before a request reaches your origin. A Worker validates the incoming kya token on each request and blocks traffic that doesn't present a valid one. For what a kya token is and why validation works this way, see How it works in Enforce KYA-Based Access Control.
Before you start: you'll need a Cloudflare account with Workers enabled, and your website or API already served through Cloudflare. A reference implementation is available in the skyfire-solutions-cloudflare-news-crawler-demo repo.
Identify your setup
Two questions determine which setup below applies to you.
Cloudflare CDN, if you're serving a website. Cloudflare API Gateway, found under Endpoint Management in the dashboard, if you manage APIs there.
Bots only, letting human visitors through while enforcing tokens on bots. Bots and humans (all), requiring a valid token from every caller.
Together, these answers point you to one of four setups below: CDN or API Gateway, each with either bot filtering or blanket enforcement.
Follow the matching setup
Select the tab below that matches your product, then the sub-tab that matches which traffic should require a token.
Use this if your website is served through Cloudflare's CDN.
1. Create and configure your Worker
- Create a Worker.
- Install the packages the Worker code needs:
npm install validator jose @cloudflare/workers-types - In
wrangler.jsonc, set these environment variables undervars. They tell the Worker which Skyfire environment to validate tokens against:"vars": { "BACKEND_API_URL": "https://api.skyfire.xyz", "OFFICIAL_SKYFIRE_JWT_ALGORITHM": "ES256", "OFFICIAL_SKYFIRE_JWT_ISSUER": "https://app.skyfire.xyz" }
2. Choose which traffic requires a token, and deploy
This path adds a Cloudflare WAF rule, a request-filtering rule that runs at Cloudflare's edge, so tokens are only required from bots. Human visitors pass through unchecked.
- Start from this path's example Worker code, which validates the
kyatoken. Adapt it to your needs, then deploy it. - In the Cloudflare Dashboard, go to Security → Security rules.
- Create a custom rule that checks for bot-likeliness (Cloudflare's Bot Score) plus the presence of a
kyapay-token(and deprecatedskyfire-pay-id) header, and blocks the request on this hostname otherwise.
Example rule from our live demo:


3. Connect your Worker to your website
With your Worker deployed, add a route: open it in the Cloudflare Dashboard, go to Domains, and add a route to your CDN-hosted website under Custom Domains & Routes.

What to return when blocking a request
When your Worker blocks a request, return a clear error that tells the caller how to get a token. Use these responses:
| Reason | Status | Response body |
|---|---|---|
| Missing token | 403 | Error: Missing KYAPay token in the kyapay-token header. Please create an account at https://app.skyfire.xyz and create a kya token (https://docs.skyfire.xyz/reference/create-token) and include it in your request in the kyapay-token header. |
| Invalid token | 401 | Error: Invalid KYAPay token in the kyapay-token header. Please create an account at https://app.skyfire.xyz and create a kya token (https://docs.skyfire.xyz/reference/create-token) and include it in your request in the kyapay-token header. |
For the full set of recommended responses, including plain-text versus structured JSON for LLM callers, see Handling Missing or Invalid Tokens.
Demo
References
Updated 1 day ago

