# Draft Otter publishing API: contract for the receiving site

This document specifies the HTTP endpoint a website must provide so that Draft Otter (https://draftotter.com) can publish articles and white papers to it. It is written to be implemented from directly, by a developer or an AI coding agent. Everything Draft Otter sends and everything it expects back is stated here; nothing else is required.

Human setup instructions: https://draftotter.com/publishing-api

## Summary

- You build **two routes** under one base address, the **endpoint**: `GET <endpoint>/<slug>` and `PUT <endpoint>/<slug>`.
- You issue one secret, the **publish key**. Draft Otter sends it on every request.
- The site owner enters the endpoint and the key in Draft Otter, under Publishing, Custom API.

Example endpoint: `https://api.example.com/content`. Draft Otter then calls `https://api.example.com/content/<slug>`.

## Requirements on the endpoint address

Draft Otter refuses to call an endpoint unless all of these hold:

- The scheme is `https`.
- The host resolves to a public IP address (not private, loopback or link-local).
- The address has no port number and no username or password in it.

Draft Otter does not follow redirects. Answer on the endpoint address itself.

## Authentication

Every request carries:

```
Authorization: Bearer <publish key>
```

Rules for the publish key. The receiving site MUST:

1. Generate the key at random. Never choose it by hand. `openssl rand -hex 32` produces a suitable 64-character key.
2. Require the key to be **at least 32 characters**. If the stored key is missing or shorter than 32 characters, refuse every request with `401`, so that an empty or weak secret cannot open the API.
3. Compare the presented key with the stored key in constant time (for example `crypto.timingSafeEqual` over SHA-256 hashes of both values).
4. Answer a missing or wrong key with `401` and a body that reveals nothing more than that, for example `{"error":"bad-key"}`.
5. Store the key as a secret (a secrets manager or environment secret), never in source code.

## Route 1: connection check

When the site owner connects, Draft Otter tests the key with one request for a post that does not exist:

```
GET <endpoint>/draft-otter-connection-check
Authorization: Bearer <publish key>
Accept: application/json
```

| Your answer | Draft Otter concludes |
|---|---|
| `404` (or `200`) | The key works. The connection is saved |
| `401` or `403` | The key was refused. The connection is not saved |
| Anything else | The endpoint is not working. The connection is not saved |

So: check the key first, and only then look the slug up. A valid key with an unknown slug must give `404`, never `401`.

## Route 2: publish

```
PUT <endpoint>/<slug>
Authorization: Bearer <publish key>
Content-Type: application/json
Accept: application/json
```

`<slug>` is 1 to 80 characters of lowercase letters `a-z`, digits `0-9` and hyphens. It never starts or ends with a hyphen. It is the identity of the post: a `PUT` to a slug that exists **replaces** that post; a `PUT` to a new slug creates one. The same request sent twice must leave one post, not two.

### Request body

```json
{
  "type": "blog",
  "title": "How to store coffee beans so they stay fresh",
  "html": "<h2>Why freshness fades</h2><p>Roasted coffee loses…</p>",
  "summary": "Where to keep beans, what to keep them in, and how long they last.",
  "tags": ["coffee", "storage"],
  "status": "published"
}
```

| Field | Type | Always sent | Meaning |
|---|---|---|---|
| `type` | string | yes | `"blog"` for an article, `"whitepaper"` for a white paper. Put the two in separate sections of the site if you can |
| `title` | string | yes | Up to 160 characters. Plain text. Use it as the page's `<h1>` |
| `html` | string | yes | The body only, without `<html>`, `<head>` or `<body>`. A white paper is about 3,500 words; allow at least 300 KB |
| `summary` | string | yes | Up to 200 characters, plain text. May be an empty string. Suitable for a listing and the meta description |
| `tags` | array of strings | yes | 0 to 8 tags, each up to 30 characters. Always empty for a white paper |
| `status` | string | yes | `"published"`: make the post public now. `"draft"`: store it without a public page |

Ignore any field you do not recognise; more may be added.

### About the HTML

The body is generated from Markdown and already has scripts and unsafe links removed. Treat it as untrusted input anyway and clean it to an allowlist before storing or displaying it. It uses only these elements:

`h1` `h2` `h3` `h4` `p` `a` `ul` `ol` `li` `blockquote` `pre` `code` `img` `table` `thead` `tbody` `tr` `th` `td` `strong` `em` `del` `hr` `br`

- The title is not repeated inside `html`. Headings in the body normally start at `h2`; demote any `h1` to `h2`.
- `<a href>` values are `http`, `https`, `mailto`, or a relative path.
- `<img>` may appear, pointing at another site. You may keep, re-host or remove images.
- No `class`, `id` or `style` attributes are sent (table cells may carry `align`). Apply your own typography.

### Success response

Any `2xx` status, with a JSON body containing the public address of the post:

```json
{ "url": "https://www.example.com/blog/how-to-store-coffee-beans-so-they-stay-fresh" }
```

- For `"status": "published"`, `url` MUST be the full `https` address where the post can be read.
- For `"status": "draft"`, `url` may be `null` or absent.
- Other fields in the response are ignored.

Draft Otter shows this address to the author as "See it on your site".

### Error response

Any status outside `2xx` means the publish failed. Return JSON with a short reason:

```json
{ "error": "bad-title", "detail": "The title is longer than 160 characters." }
```

Draft Otter shows the author `detail` if present, otherwise `error`, otherwise the status code, cut to 200 characters. Write `detail` for a non-technical reader. Suggested statuses:

| Status | When |
|---|---|
| `401` | Missing or wrong publish key |
| `409` | The post was edited on your site and you do not want it overwritten. Draft Otter never forces an overwrite |
| `413` | `html` is larger than you accept |
| `422` | A field broke a rule. Say which in `detail` |
| `5xx` | Storing or publishing failed. Draft Otter does not retry by itself; the author can publish again, so a repeated `PUT` must be safe |

## Behaviour to expect from Draft Otter

- **When it calls:** when the author presses Publish, and, if the author has switched on automatic publishing, each time a new article (daily) or white paper (monthly) is written.
- **Drafts then publishing:** the same slug may arrive first with `"status": "draft"` and later with `"status": "published"`. The second request replaces the first.
- **Timeout:** each request is abandoned after 15 seconds. Answer within that, and do slow work (cache invalidation, rebuilds) after responding or quickly.
- **User agent:** `DraftOtterBot/1.0`.
- **Volume:** a few requests a day per site.
- **Not called:** Draft Otter never deletes or unpublishes a post. Removing one is done on your site.

## Test it yourself

Replace the endpoint and key, then check these four outcomes before connecting Draft Otter:

```bash
ENDPOINT=https://api.example.com/content
KEY=your-publish-key

# 1. Wrong key is refused: expect 401
curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer wrong" "$ENDPOINT/draft-otter-connection-check"

# 2. Right key, unknown slug: expect 404
curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $KEY" "$ENDPOINT/draft-otter-connection-check"

# 3. Publish a draft: expect 2xx and a JSON body
curl -s -X PUT "$ENDPOINT/draft-otter-test-post" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"type":"blog","title":"Test post","html":"<h2>Hello</h2><p>A test.</p>","summary":"A test.","tags":["test"],"status":"draft"}'

# 4. Publish it: expect 2xx and {"url":"https://…"}; sending it twice must not create two posts
curl -s -X PUT "$ENDPOINT/draft-otter-test-post" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{"type":"blog","title":"Test post","html":"<h2>Hello</h2><p>A test.</p>","summary":"A test.","tags":["test"],"status":"published"}'
```

## Implementation checklist

- [ ] `GET <endpoint>/<slug>` and `PUT <endpoint>/<slug>` exist, over `https`, on a public address, without a port.
- [ ] The publish key is random, at least 32 characters, stored as a secret, and compared in constant time.
- [ ] A missing, wrong or too-short key gives `401` on both routes.
- [ ] A valid key with an unknown slug gives `404` on `GET`.
- [ ] `PUT` creates or replaces by slug, and is safe to repeat.
- [ ] `type` `"blog"` and `"whitepaper"` are both accepted.
- [ ] `status` `"draft"` stores without a public page; `"published"` makes the page public.
- [ ] `html` is cleaned to an allowlist before it is stored or shown.
- [ ] Success returns `2xx` with `{"url": "<public https address>"}` for a published post.
- [ ] Failures return a non-`2xx` status with `{"error": "...", "detail": "..."}`.
- [ ] Responses arrive within 15 seconds.
