# REST API and webhooks

> The public REST API v1 with project tokens, its limits, and signed webhooks for internal tooling.

**Spec:** `GET /api/v1/openapi.json` (OpenAPI 3.1, public).

## Authentication

`Authorization: Bearer adp_…` — the same project-scoped tokens agents use (Project → Connect agent).

- **Service accounts** (Org → Service accounts, Pro): tokens that don't belong to a person — for CI and shared agents. They need an explicit project grant, always expire (≤ 365 days) and show up as `token (service account <name>)` in the audit log.
- A token works for **one project**. Another project's id returns 404.
- **Read-only** tokens can only `GET`.
- **Approving designs is human-only** and isn't part of the API.

## Limits

- `RATE_LIMIT_MCP_PER_MIN` requests per minute per token (default 300).
- The plan's daily agent-request allowance, shared with MCP.
- The limits return `429`. A plan limit returns `403` with `code: plan_limit` and an upgrade hint.

## Endpoints

| Method | Path | |
| --- | --- | --- |
| GET | `/api/v1/projects/:projectId` | Project |
| GET | `/api/v1/projects/:projectId/design-tokens` | Tokens, local and from libraries |
| GET | `/api/v1/projects/:projectId/components` | Components with variants and CSS |
| GET | `/api/v1/projects/:projectId/screens` | Screens |
| POST | `/api/v1/projects/:projectId/screens` | Save a version (read/write token) |
| GET | `/api/v1/projects/:projectId/screens/:screen` | Screen + versions (id or key) |
| GET | `/api/v1/projects/:projectId/screens/:screen/versions/:which` | `latest`, `approved` or a number |
| POST | `/api/v1/projects/:projectId/screens/:screen/implemented` | `{ ref, version?, notes? }` |
| GET | `/api/v1/projects/:projectId/comments` | Threads (`?screen=&status=`) |
| POST | `/api/v1/projects/:projectId/screens/:screen/comments` | `{ versionId, anchor: { path, xPct, yPct }, body }` |

## Webhooks

**Setup:** Project → Settings → Webhooks. This needs `manage_settings` and the Pro plan.

**Events:**
- `version.created`
- `screen.approved`
- `screen.implemented`
- `library.released` (sent to the library project and every project that uses it)
- `comment.created`
- `ping` (the "Send test" button)

**Each delivery** is a POST with a JSON body: `{ id, type, createdAt, projectId, data }`. It carries these headers:
- `Specimo-Event`
- `Specimo-Delivery`
- `Specimo-Signature: t=<unix>,v1=<hex>`, where `v1` is the HMAC-SHA256 of `"<t>.<raw body>"` using the endpoint's secret (`whsec_…`, shown once).

Verify the signature and reject anything older than 5 minutes:

```js
const [t, v1] = header.split(',').map((p) => p.split('=')[1]);
const ok = crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(crypto.createHmac('sha256', secret).update(`${t}.${rawBody}`).digest('hex')));
```

**Retries:** anything other than a 2xx response is retried with backoff: 6 tries over about 2.5 minutes. The delivery log (last 50 deliveries) shows each delivery's status, tries and the last response.

**Allowed URLs:** endpoints must be `https` on a public address; private, loopback and link-local addresses are refused. Redirects aren't followed.

## Repository webhooks (incoming)

Connect GitHub or GitLab in **Project → Settings → Code repositories**. Each connection gives you a URL, `POST /api/integrations/repo/:id`, and a secret, which is shown once.

- **GitHub:** the `X-Hub-Signature-256` HMAC is checked.
- **GitLab:** the `X-Gitlab-Token` is checked.

Pull or merge requests whose title or body mentions a screen are linked to it. A mention is `design:<screen-key>`, `design:<screen-key>@<version>`, or a link to `/projects/<id>/screens/<screenId>[?version=<versionId>]`.

- **Merge:** marks the version implemented, with the PR URL and the merge commit. Without an explicit version, the approved one is used.
- **Merged revert:** a PR whose body says `Reverts owner/repo#N` (GitLab: `This reverts merge request !N`) unmarks it.
