Cosmo Reader
DE

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

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"
}
FieldTypeNotes
eventstringSee above.
idstringLowercase UUID. Stable for the life of the annotation.
typestringhighlight today. margin_note and handwriting are reserved.
book.titlestring
book.authorstring or nullSeveral authors are joined into one string.
book.identifierstring or nullThe identifier the publication declares (ISBN, UUID …). PDFs have none.
quotestring or nullThe highlighted text.
notestring or nullThe reader's note, plain text.
location.chapterstring or nullChapter title, or the outline entry for a PDF.
location.progressnumber or nullPosition in the whole book, 0 to 1. Use it to sort.
location.pagenumber or nullPage number, starting at 1. PDFs only.
colorstring or nullyellow, green, blue, pink or purple.
created_at, updated_atstringISO 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

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

HeaderWhenValue
Content-Typealwaysapplication/json
Idempotency-Keyannotation 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.
Authorizationbearer authenticationBearer <token>
X-Cosmo-TimestampHMAC authenticationUnix time in seconds, as a decimal string.
X-Cosmo-SignatureHMAC authenticationsha256=<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:

  1. Read the raw request body, before any JSON parsing.
  2. Compute HMAC-SHA256(secret, timestamp + "." + rawBody).
  3. Compare with the value after sha256= in constant time.
  4. 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 answerWhat the app does
2xxMarks the change as delivered.
408, 425, 429, 5xx, no answer, network errorRetries 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 4xxStops 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()