Cosmo Reader webhook reference
Cosmo Reader can send every highlight and note to an HTTPS endpoint you run. This page describes what arrives, so you can write the receiving side.
At a glance
- One
POSTper change, body is JSON (Content-Type: application/json). - Three events:
annotation.created,annotation.updated,annotation.deleted. - Delivery is at least once. Store by
id(upsert); never append blindly. - Events for different annotations can arrive in any order. For one annotation, the app only ever sends its latest state.
- Answer with any
2xxstatus within 30 seconds. The body is ignored. - The URL must be
https://. Plainhttp://is accepted forlocalhostonly.
Events
annotation.created and annotation.updated
Both carry the full annotation. Treat them the same way: insert or replace the record with this id.
{
"event": "annotation.created",
"id": "c12af4d7-f12c-4274-9768-32cad313c975",
"type": "highlight",
"book": {
"title": "Cosmo Test Book",
"author": "Ada Example",
"identifier": "urn:uuid:5e1f0c2a-7d2b-4c64-9a53-1f6f6f1c0a01"
},
"quote": "wrote",
"note": "A first note",
"location": { "chapter": "The First Chapter", "progress": 0.1667, "page": null },
"color": "pink",
"created_at": "2026-11-28T07:48:54Z",
"updated_at": "2026-11-28T07:49:04Z"
}
| Field | Type | Notes |
|---|---|---|
event | string | See above. |
id | string | Lowercase UUID. Stable for the life of the annotation. |
type | string | highlight today. margin_note and handwriting are reserved. |
book.title | string | |
book.author | string or null | Several authors are joined into one string. |
book.identifier | string or null | The identifier the publication declares (ISBN, UUID …). PDFs have none. |
quote | string or null | The highlighted text. |
note | string or null | The reader's note, plain text. |
location.chapter | string or null | Chapter title, or the outline entry for a PDF. |
location.progress | number or null | Position in the whole book, 0 to 1. Use it to sort. |
location.page | number or null | Page number, starting at 1. PDFs only. |
color | string or null | yellow, green, blue, pink or purple. |
created_at, updated_at | string | ISO 8601, UTC. |
Fields without a value are sent as null, not left out. Keys are sorted alphabetically in the body. New fields may be added later; ignore what you do not know.
annotation.deleted
{
"event": "annotation.deleted",
"id": "22d5759b-498f-4af0-b386-6d2109d86325",
"book": { "title": "Cosmo Test Book", "author": "Ada Example", "identifier": "urn:uuid:…" }
}
Remove the record with this id. book is missing when the whole book was deleted in the app. A deletion for an id you do not have is not an error: answer 2xx.
You receive a deletion when the reader
- deletes the annotation,
- marks it private (private annotations are never sent),
- switches this book off for your endpoint, or
- deletes the book.
If one of these is undone, the annotation comes back as annotation.created.
connection.test
Sent when the reader taps "Send Test Request" while setting up the connection. It carries no annotation.
{ "event": "connection.test", "sent_at": "2026-11-28T07:53:23Z" }
Answer 2xx if the authentication is right. Do not store anything.
Headers
| Header | When | Value |
|---|---|---|
Content-Type | always | application/json |
Idempotency-Key | annotation events | <id>-<16 hex characters> for created and updated, <id>-deleted for deletions. A retry of the same change repeats the key; the next change has a new one. |
Authorization | bearer authentication | Bearer <token> |
X-Cosmo-Timestamp | HMAC authentication | Unix time in seconds, as a decimal string. |
X-Cosmo-Signature | HMAC authentication | sha256=<hex> |
Authentication
The reader picks one of two methods per connection.
Bearer token
The app sends the token you gave the reader in Authorization. Compare it in constant time and answer 401 if it does not match.
HMAC signature
The app signs <timestamp>.<body> with HMAC-SHA256, using the shared secret as the key (its UTF-8 bytes), and sends the lowercase hex digest.
To verify:
- Read the raw request body, before any JSON parsing.
- Compute
HMAC-SHA256(secret, timestamp + "." + rawBody). - Compare with the value after
sha256=in constant time. - Reject timestamps more than a few minutes from your clock, so a captured request cannot be replayed later.
Test vector: secret key, timestamp 1, body body gives 91b5374b153842ad05b2c4eab9349b8321b14703165bd3fb8b034dfb8be98ae5.
Node.js:
import crypto from "node:crypto";
function verify(rawBody, headers, secret) {
const timestamp = headers["x-cosmo-timestamp"] ?? "";
const given = (headers["x-cosmo-signature"] ?? "").replace(/^sha256=/, "");
const expected = crypto
.createHmac("sha256", secret)
.update(`${timestamp}.`)
.update(rawBody)
.digest("hex");
const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) < 300;
return (
fresh &&
given.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(given), Buffer.from(expected))
);
}
Python:
import hashlib, hmac, time
def verify(raw_body: bytes, headers, secret: str) -> bool:
timestamp = headers.get("X-Cosmo-Timestamp", "")
given = headers.get("X-Cosmo-Signature", "").removeprefix("sha256=")
expected = hmac.new(
secret.encode(), timestamp.encode() + b"." + raw_body, hashlib.sha256
).hexdigest()
fresh = timestamp.isdigit() and abs(time.time() - int(timestamp)) < 300
return fresh and hmac.compare_digest(given, expected)
Responses and retries
| Your answer | What the app does |
|---|---|
2xx | Marks the change as delivered. |
408, 425, 429, 5xx, no answer, network error | Retries later: after 1 minute, then doubling up to 6 hours, 8 attempts in all. After that the change waits until the reader retries it by hand or edits the annotation again. |
Any other 4xx | Stops at once and shows the status to the reader. Use 401 for bad credentials. |
The app syncs when it opens, when it goes to the background, on a "Sync Now" tap, and in the background at an interval the reader chooses. iOS decides when background runs actually happen, so do not expect changes within seconds.
Several edits made between two syncs arrive as one event with the final state. An annotation that was created and deleted between two syncs is never sent.
A minimal receiver
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
TOKEN = "change-me"
store = {}
class Hook(BaseHTTPRequestHandler):
def do_POST(self):
if self.headers.get("Authorization") != f"Bearer {TOKEN}":
self.send_response(401); self.end_headers(); return
body = self.rfile.read(int(self.headers.get("Content-Length", 0)))
event = json.loads(body)
if event["event"] == "annotation.deleted":
store.pop(event["id"], None)
elif event["event"].startswith("annotation."):
store[event["id"]] = event
self.send_response(204); self.end_headers()
HTTPServer(("127.0.0.1", 8787), Hook).serve_forever()