SDKs

Two clients wrap the API with the same behaviour: typed operations generated from the OpenAPI document, an Idempotency-Key on every write, retries after Retry-After, cursor paging, resumable events, typed errors and the webhook signature check. Both are Apache-2.0 and versioned with the server.

Install

Both clients live in the OpenCanvas repository and are not yet published to npm or crates.io.

ClientWhereRuns on
TypeScript @opencanvas/client[web/packages/client](../../../web/packages/client)Node 20+, browsers, Deno, edge runtimes. No runtime dependency beyond fetch.
Rust opencanvas-client (library opencanvas_client)[crates/oc-client](../../../crates/oc-client)Tokio, reqwest and serde.
Add the client
# TypeScript: build the package from a checkout, then depend on the folder.
git clone https://github.com/CasualOffice/opencanvas
cd opencanvas/web && pnpm install && pnpm --filter @opencanvas/client build
cd /path/to/your/app && npm install /path/to/opencanvas/web/packages/client

# Rust: a git dependency.
cargo add opencanvas-client --git https://github.com/CasualOffice/opencanvas

Create a client

Host key or user token
# curl: send the headers yourself (see Authentication).
curl -s "https://canvas.example.com/api/v1/files/doc_1" \
  -H "X-OpenCanvas-Host: acme" -H "X-OpenCanvas-Key: $OC_API_KEY"

What both do

FeatureBehaviour
Typed operationsOne method per operation, with the types of its parameters, body and answer, generated from openapi.json (TypeScript scripts/gen.mjs, Rust tests/models.rs; CI fails when they drift). The generic call(operationId, params, options) answers status, headers, etag, replayed and rateLimit too.
CredentialsThe host key (with optional author and callback token) or a user token; a token callback runs before every request.
IdempotencyEvery write carries an Idempotency-Key: a random one per call, the same on each retry of that call. Pass your own to survive a process restart, or none.
Retries429, 503 and 409 IDEMPOTENCY_IN_PROGRESS, after Retry-After (else 500 ms doubling, at most 30 s), 3 times by default. Network errors are retried for reads and keyed writes only. The wait is injectable, so tests never sleep.
ErrorsProblem details as typed errors, by code first, then status: stale block (412, with fresh), rate limited (retryAfterSecs), key reused, key in progress, cursor expired, bad request, unauthorized, forbidden, not found, conflict, gone, precondition failed, too large, unprocessable, unavailable, server.
Pagingpaginate follows next_cursor through listBlocks and queryRows, item by item.
EventsThe SSE stream of a file. A dropped stream (or close code 4008) reconnects with Last-Event-ID and gets what it missed, or a resync event; 4001 and 4003 end it with an error.
WebhooksverifyWebhook / verify_webhook: the v2 check, in constant time, with a 5-minute window.

Typed errors

Handle a stale write
# 412 with "code": "STALE_BLOCK": re-read, apply your change again, send it with the new rev.
curl -s -o answer.json -w '%{http_code}\n' -X POST \
  "https://canvas.example.com/api/v1/files/doc_1/blocks:batch" \
  -H "X-OpenCanvas-Host: acme" -H "X-OpenCanvas-Key: $OC_API_KEY" \
  -H "Content-Type: application/json" -H 'If-Match: "c465d27bcd7e"' \
  -d '{"ops": [{"op": "append", "markdown": "Shipped."}]}'
jq -r .code answer.json

Paging, events and webhooks

See pagination, events and webhooks: each page has the client samples beside curl and Python.

The conformance suite

crates/oc-server/api/conformance.json is one list of scenarios that both clients run against a real server: a read, a block batch with If-Match, idempotent replay, paging, a rate limit, an events resume and a webhook signature, plus offline webhook signature vectors. The server pins its clock, so no scenario sleeps. A third-party client can run the same scenarios; the format is in the integration guide.

Block SDK

To add your own block types to the editor rather than call the API, use @opencanvas/block-sdk. It is a different package: blocks run sandboxed inside the canvas, not on your server. See the Block SDK guide.

Last changed .