Skip to content

Beta. consent-kit is at 0.x: expect breaking changes between releases, and treat everything it generates as a draft. Report a problem or share feedback.

Run @consent-kit/server as a Cloudflare Worker that receives consent receipts and appends them to a D1 database. This guide follows examples/cloudflare-receipts, which is a complete Worker you can copy.

For what the endpoint does and what a stored receipt proves, read consent receipts first.

  • A site built with consent-kit whose registry declares receipts.endpoint.
  • A Cloudflare account with the site’s zone, and wrangler.
  • A place to keep a secret key for as long as you keep the receipts.

On the site’s own origin (recommended). Serve the Worker on the site’s zone at the path of receipts.endpoint:

receipts:
endpoint: /consent-receipts

The endpoint stays site-relative, the site’s connect-src 'self' already allows it, and the beacon is same-origin.

On another host. Use an https: URL, and declare it as an endpoint artefact with its processor, bound to a purpose that is not based on consent. A receipt is sent for a rejection too, so the endpoint cannot wait for consent:

receipts:
endpoint: https://receipts.example.net/consent-receipts
artefacts:
- id: consent_receipts
kind: endpoint
url: https://receipts.example.net/consent-receipts
processor: receipts-host
purposes: [essential]

Its origin is added to connect-src automatically. The validator refuses an undeclared third-party endpoint (receipts/undeclared-endpoint) and one bound to a consent purpose (receipts/endpoint-needs-consent).

Copy examples/cloudflare-receipts, or create these files.

src/worker.ts:

import { createD1ReceiptStore, createReceiptHandler, type D1DatabaseLike } from '@consent-kit/server';
import manifest from '../consent.gen/receipts.manifest.gen.json';
export interface Env {
/** The D1 binding of wrangler.toml, migrated with migrations/0001_consent_receipts.sql. */
DB: D1DatabaseLike;
/** The HMAC key consent ids are hashed with: `wrangler secret put RECEIPTS_HMAC_KEY`. */
RECEIPTS_HMAC_KEY?: string;
/** The site's origins, comma-separated (`[vars]` in wrangler.toml). */
ALLOWED_ORIGINS?: string;
}
type Handler = (request: Request) => Promise<Response>;
let cached: { env: Env; handle: Handler } | undefined;
/** One handler per isolate. It throws on a missing or short key, so every request fails with a 500. */
function handlerFor(env: Env): Handler {
if (cached?.env !== env) {
const handle = createReceiptHandler({
store: createD1ReceiptStore(env.DB),
hmacKey: env.RECEIPTS_HMAC_KEY,
allowedOrigins: (env.ALLOWED_ORIGINS ?? '').split(',').map((origin) => origin.trim()).filter((origin) => origin !== ''),
manifest,
});
cached = { env, handle };
}
return cached.handle;
}
export default {
fetch(request: Request, env: Env): Promise<Response> {
return handlerFor(env)(request);
},
};

wrangler.toml:

name = "consent-receipts"
main = "src/worker.ts"
compatibility_date = "2026-07-01"
# routes = [{ pattern = "www.example.com/consent-receipts", zone_name = "example.com" }]
[vars]
# The origins of the site's pages, comma-separated, exactly as browsers send them in `Origin`.
ALLOWED_ORIGINS = "https://www.example.com"
[[d1_databases]]
binding = "DB"
database_name = "consent-receipts"
database_id = "00000000-0000-0000-0000-000000000000"
migrations_dir = "migrations"

migrations/0001_consent_receipts.sql: copy it from packages/server/migrations/0001_consent_receipts.sql. It creates the consent_receipts table, its two indexes and the trigger that refuses updates. The columns are in the reference.

package.json scripts, with @consent-kit/server as a dependency and consent-kit and wrangler as development dependencies:

{
"scripts": {
"generate": "consent-kit build --registry ../static-site/privacy.registry.yaml --out consent.gen",
"dev": "pnpm generate && wrangler d1 migrations apply DB --local && wrangler dev",
"deploy": "pnpm generate && wrangler d1 migrations apply DB --remote && wrangler deploy"
}
}

generate runs consent-kit build on the site’s registry and writes the output into the Worker’s consent.gen/. The Worker imports only receipts.manifest.gen.json from there. Point --registry at your site’s registry; the lock file next to it supplies the release history. Add consent.gen/ and .dev.vars to .gitignore.

Terminal window
pnpm install
echo "RECEIPTS_HMAC_KEY=$(openssl rand -base64 48)" > .dev.vars # git-ignored
pnpm dev # generate, apply the migration to a local D1, then wrangler dev

wrangler dev keeps the local database in .wrangler/. Nothing in this step talks to Cloudflare. Look at the rows with:

Terminal window
pnpm exec wrangler d1 execute DB --local --command "SELECT * FROM consent_receipts"
Terminal window
pnpm exec wrangler login
pnpm exec wrangler d1 create consent-receipts

Copy the database_id it prints into wrangler.toml, replacing the placeholder.

The consent id is never stored. In its place the handler stores HMAC-SHA-256 of it under this key.

Terminal window
openssl rand -base64 48 # keep it in your password manager
pnpm exec wrangler secret put RECEIPTS_HMAC_KEY # paste it when asked
  • The key must be at least 32 bytes. With a shorter or missing key, the handler refuses to start and every request fails with a 500.
  • It is a secret, never a [vars] entry in wrangler.toml.
  • Keep a copy, for as long as you keep the receipts. A visitor’s rows are found by hashing the consent id they present with this key. A lost or rotated key makes the existing rows unreachable by consent id.

In wrangler.toml:

  • Set ALLOWED_ORIGINS to the origins of the site’s pages, exactly as browsers send them in the Origin header: scheme and host, no path, no trailing slash. Several origins are separated by commas.
  • Uncomment routes and set the site’s host and zone, with the path of receipts.endpoint.

A POST from an origin that is not in the list is answered 403 request/origin-forbidden.

Terminal window
pnpm run deploy

(pnpm deploy without run is a different pnpm command.) The script generates the manifest, applies the migration to the remote database and deploys the Worker.

A request without an Origin header did not come from the banner and must be refused:

Terminal window
curl -i -X POST https://www.example.com/consent-receipts -H 'Content-Type: text/plain' -d '{}'
HTTP/2 403
…
{"error":"request/origin-missing"}

Then open the site, make a choice in the banner, and look for the row:

Terminal window
pnpm exec wrangler d1 execute DB --remote --command "SELECT id, event, accepted, rejected, revision, received_at FROM consent_receipts ORDER BY id DESC LIMIT 5"

If no row arrives, look at the request in the browser’s network panel. The status and the error code say why; every code is in the reference. The usual ones:

Answer Cause
403 request/origin-forbidden The page’s origin is not in ALLOWED_ORIGINS. Check www, the scheme and the port.
404 request/not-found The route does not match the path of receipts.endpoint.
422 receipt/unknown-policy The Worker’s manifest was generated from a different registry state than the site was built from. Redeploy the Worker.
500 The key is missing or shorter than 32 bytes.

You can also run consent-kit audit against the deployed site. Its check “Consent receipts: one per decision” verifies that each decision sends one well-formed receipt. It does not verify that your endpoint stored it.

Terminal window
pnpm run deploy

Every release changes the policy hash. The Worker accepts a receipt only for a policy in its manifest, so a Worker that is not redeployed refuses the new site’s receipts. The new manifest keeps accepting every earlier release from the lock’s history, so pages still open on an older policy keep working. Deploy the Worker before or together with the site.

To refuse receipts for policies that were replaced long ago, pass maxSupersededAgeMs to createReceiptHandler, for example 30 days. By default every listed policy is accepted for as long as it is listed.

  • Rate limiting. The Worker has none, and the endpoint is unauthenticated by nature. Add a rate limiting rule for the route in the Cloudflare dashboard.
  • Retention. No sweep is built. Delete old rows by received_at on a schedule you decide; the column is indexed.
  • Access and erasure requests. No tooling is built. Hash the consent id the visitor presents with subjectHasher(key) from @consent-kit/server and query or delete by subject_hash.
  • Your privacy documents. With an endpoint, the generated privacy policy says that each choice is kept as proof and what the record holds. If Cloudflare is a processor for your site, declare it in the registry.

createReceiptHandler is a plain Fetch API handler: (request: Request) => Promise<Response>. It runs in Deno, Bun and Node.js 22 or newer. Implement the one-method ReceiptStore interface for your database, and keep the same two properties: store the hash, not the consent id, and never store the IP address.

consent-kit

Generate your cookie banner and privacy-policy drafts from one registry, and audit what your site really does against it.

© 2026 consent-kit · Released under the MIT licence.