# Receive data subject requests

> Add a request form to the site, receive requests in a Cloudflare Worker on D1, and email each one to the controller's mailbox.

Add a request form to the site, receive requests in a Cloudflare Worker on D1, and email each one to the controller's mailbox. This guide follows `examples/static-site` (the form) and `examples/cloudflare-receipts` (the Worker). Read [data subject requests](/docs/manual/concepts/subject-requests/) first.

It assumes the site already keeps consent receipts in a Worker, as in [deploy the receipt endpoint](/docs/manual/guides/deploy-the-receipt-endpoint/). The two share the HMAC key, the database and the Worker.

## 1. Declare the section

```yaml
subjectRequests:
  endpoint: /privacy/requests      # site-relative: on the site's own domain
  notify: privacy@example.com      # every request is emailed here
  types: [access, erasure]
  retention: 3y                    # how long a closed request is kept; no default
```

Run `consent-kit check`. Adding the section is not material, so it asks for no version bump and nobody is asked for consent again. The documents gain the built-in purpose "Handling your data protection requests" and a link to the form in the rights section. Run `consent-kit docs` and have the new phrases reviewed.

To email the requester a confirmation link too, declare the provider as a processor and name it:

```yaml
processors:
  - id: resend
    name: Resend
    legalEntity: Resend, Inc.
    country: US
    transferMechanism: dpf
subjectRequests:
  # …
  requesterEmail:
    processor: resend
```

## 2. Put the form on a page

`consent-kit build` writes `subject-request-form.<locale>.html` (a fragment) and `subject-request-page.<locale>.html` (a complete page). Embed the fragment in a page of the site and link that page from every page, for example next to the privacy policy. The static site example does it in `src/build.ts`:

```ts
.replace('{{SUBJECT_REQUEST_FORM}}', () => readFileSync(join(genDir, 'subject-request-form.en.html'), 'utf8'))
```

Style the form by its classes. Hide the honeypot, which people must never see:

```css
.ck-subject-request .ck-hp { display: none; }
```

If the policy links the form somewhere other than the endpoint, say where in `privacy.documents.json`:

```json
{ "subjectRequestFormLinks": { "en": "/privacy-request.html" } }
```

Without a page of your own, the handler serves the generated page at `GET /privacy/requests`.

## 3. Route the endpoint to the Worker

The endpoint must be on the site's zone, so that the browser sends `cc_cookie` with the form:

```toml
routes = [
  { pattern = "www.example.com/consent-receipts", zone_name = "example.com" },
  { pattern = "www.example.com/privacy/requests", zone_name = "example.com" },
]
```

`src/worker.ts` sends that path to `createSubjectRequestHandler` and passes the `ExecutionContext`, so the emails go out after the answer:

```ts
fetch(request, env, context) {
  return new URL(request.url).pathname === requestsManifest.endpoint
    ? handlers.requests(request, context)
    : handlers.receipts(request);
}
```

## 4. Migrate the database

`migrations/0002_subject_requests.sql` creates the request table and the rate-limit table. It is a copy of the one `@consent-kit/server` ships:

```sh
pnpm exec wrangler d1 migrations apply DB --remote
```

## 5. Email the controller

Set up Cloudflare Email Service for the domain the emails are sent from. Then bind the mailbox and set the sender:

```toml
[vars]
REQUESTS_FROM = "Privacy requests <requests@example.com>"

[[send_email]]
name = "PRIVACY_EMAIL"
destination_address = "privacy@example.com"
```

The binding can send to that one address only, which is all the notification and the reminder need. With `requesterEmail`, also set the provider's key: `pnpm exec wrangler secret put RESEND_API_KEY`.

## 6. Schedule the reminder and the sweep

```toml
[triggers]
crons = ["0 7 * * *"]
```

The Worker's `scheduled` handler deletes closed requests past their retention (`sweepClosedRequests`) and sends one reminder of the open requests that are overdue, due within 7 days, or never notified (`remindDueRequests`). It sends nothing when there are none.

## 7. Limit abuse in front of the route

Add a Cloudflare rate-limiting rule for `POST /privacy/requests`. The handler limits confirmation emails per address and in all, but it stores no IP address, so it cannot limit per client itself.

## 8. Check it

```sh
pnpm run deploy
consent-kit audit https://www.example.com --page / --page /about.html
```

The audit's "Subject requests reachable" check finds the form on a page or one link away, and checks that it posts to the site's own origin. It then submits it from `audit-…@consent-kit.invalid` and expects the 303. Against the live site this files one real request, and the controller's mailbox receives its notification. Close it as an audit test. The handler sends no confirmation email to `.invalid` addresses.

## Answering a request

The notification carries the checklist. When you are done, close the request:

```ts
await store.close(id, { outcome: 'data sent', at: new Date().toISOString() });
```

For a complex request, record an extension and tell the requester why, within the first month:

```ts
await store.extend(id, { dueAt: addMonths(new Date(request.receivedAt), 3).toISOString(), reason: 'Many systems to search.' });
```

`findReceipts(receiptStore, request.subjectHash)` lists the consent receipts of the browser that sent the request. `eraseReceipts` deletes them, once you have decided that you no longer need them as proof of consent.
