# REST API & Security

The full OpenAPI 3.1 specification is published live at:

```text
https://api.blokforms.com/api/v1/openapi.json
```

## Authentication

Pass a scoped Personal Access Token created in **Workspace settings -> Developers & API**:

```bash
curl https://api.blokforms.com/api/v1/forms \
  -H "Authorization: Bearer bf_pat_YOUR_TOKEN"
```

## Key Management Endpoints (`https://api.blokforms.com`)

| Method & Path | Purpose |
| --- | --- |
| `GET /api/v1/templates` | List all 16 starter templates |
| `GET/POST /api/v1/projects` | List or create projects |
| `GET/POST /api/v1/projects/:projectId/forms` | List or create forms |
| `GET/PATCH/DELETE /api/v1/forms/:formId` | Read, update, or delete a form |
| `GET /api/v1/forms/:formId/snippets` | Get embed snippets (auto-uses verified custom domain) |
| `GET /api/v1/forms/:formId/submissions` | List or FTS5 search submissions (`?q=...`) |
| `GET /api/v1/forms/:formId/submissions/export` | Export as `?format=xlsx|csv|json` |
| `POST /api/v1/submissions/triage` | Bulk status update, assign, or delete (up to 200) |
| `GET /api/v1/forms/:formId/analytics` | Cookie-free conversion, funnel, UTM & device metrics |
| `GET /api/v1/forms/:formId/report` | Anonymized field distribution report |
| `GET /api/v1/forms/:formId/approvals` | List approval workflow runs |
| `POST /api/v1/approvals/:stepId/decide` | Approve or deny a pending workflow step |
| `GET /api/v1/forms/:formId/bookings` | List appointment bookings |
| `POST /api/v1/bookings/:bookingId/cancel` | Cancel a booking and reopen the slot |
| `GET/POST /api/v1/integrations` | List or create encrypted native integrations |
| `POST /api/v1/ai/generate-form` | Generate a validated form blueprint from a prompt |
| `POST /api/v1/forms/:formId/ai/ask` | Ask the AI Data Assistant (PII scrubbed by default) |
| `GET /api/v1/organization/audit-logs` | Inspect workspace audit log entries |
| `GET/POST /api/v1/organization/domains` | Manage agency custom domains & DoH TXT verification |
| `POST /api/v1/organization/gdpr/erase` | Preview (`dryRun: true`) or erase all data for an email |

## Public Edge Endpoints (`https://cdn.blokforms.com`)

| Method & Path | Purpose |
| --- | --- |
| `GET /v1/forms/:id/schema` | Edge-cached public form schema (`?locale=en|nl|de|fr`) |
| `POST /f/:id` | Submit JSON, multipart, or URL-encoded form data |
| `POST /f/:id/uploads` | Two-step private EU R2 file or signature upload |
| `GET /f/:id/slots?field=...&date=YYYY-MM-DD` | Live appointment slot availability |
| `GET /f/:id/bookings/:bookingId.ics` | Download RFC 5545 calendar invite |
| `POST /f/:id/unlock` | Verify password and issue a 12-hour HMAC unlock token |
| `POST /f/:id/drafts` & `GET /f/:id/drafts/:token` | Save & Continue Later drafts |
| `GET/POST /approve/:token(/decide)` | Hosted approval review and 1-click decision |
| `GET /sign/:token` | Hosted BlokForms Sign e-signature ceremony |

## Password-Protected Forms

When `settings.security.passwordEnabled` is `true`, `POST /f/:id` returns HTTP `401 PASSWORD_REQUIRED` unless unlocked. Call `POST /f/:id/unlock` with `{ "password": "..." }` to receive `{ "ok": true, "accessToken": "..." }`, then pass `X-BlokForms-Access: <accessToken>` (or `_bf_access` in JSON) on submission.

## Verifying Webhook Signatures (HMAC-SHA256)

Every outbound webhook includes:
- `X-BlokForms-Event`
- `X-BlokForms-Delivery`
- `X-BlokForms-Timestamp` (Unix seconds)
- `X-BlokForms-Signature: t=<timestamp>,v1=<hex_hmac>`

Verify in Node.js or Cloudflare Workers:

```ts
import crypto from "node:crypto";

export function verifyBlokFormsWebhook(rawBody: string, header: string, secret: string): boolean {
  const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
  if (!parts.t || !parts.v1) return false;
  const expected = crypto
    .createHmac("sha256", secret)
    .update(`${parts.t}.${rawBody}`)
    .digest("hex");
  return crypto.timingSafeEqual(Buffer.from(parts.v1), Buffer.from(expected));
}
```
