Skip to content
Developer docsPagination and idempotency

Conventions

Pagination and idempotency

Lists come in pages that you walk with a cursor. Requests that create or change things accept an Idempotency-Key, so a retry cannot do the work twice.

Pagination

A list returns a page of items and a cursor for the next page:

JSON
{
  "items": [{ "id": "c4Rt8NwZ1bYs", "displayName": "Sam" }],
  "nextCursor": "WyJzYW0iLCJjNFJ0OE53WjFiWXMiXQ"
}
  • Send nextCursor back as the cursor query parameter for the next page. On the last page, nextCursor is null.
  • limit sets the page size: 1 to 50, and 20 when you leave it out. Each reference entry gives its own range.
  • Cursors are opaque: do not build, change or keep them. A cursor the API cannot read gets 422 VALIDATION with the issue path cursor.
  • Every list has a fixed order, given in its reference entry, such as by name or newest first. Each page carries on from the last item of the one before, not from a page number.
JavaScript
// Reads every page of a list.
const auth = { Authorization: `Bearer ${process.env.WW_API_KEY}` };

async function listAll(path) {
  const items = [];
  let cursor = null;
  do {
    const url = new URL(path, 'https://api.wristworkout.com');
    url.searchParams.set('limit', '50');
    if (cursor) url.searchParams.set('cursor', cursor);
    const res = await fetch(url, { headers: auth });
    if (!res.ok) throw new Error(`Wrist Workout API: ${res.status}`);
    const page = await res.json();
    items.push(...page.items);
    cursor = page.nextCursor;
  } while (cursor);
  return items;
}

const customers = await listAll('/v1/orgs/org_id/customers');

A few lists come in one response, without a cursor:

  • webhook endpoints, GET /v1/orgs/{orgId}/webhooks;
  • a customer’s assignments, inside GET /v1/orgs/{orgId}/customers/{customerId};
  • your coaching links and assignments, GET /v1/me/coaching;
  • progress on an exercise, GET /v1/exercises/{id}/progress, whose limit is the number of workouts: 1 to 200, and 100 when you leave it out.

Idempotency

A retry after a timeout must not assign a programme twice or create two invitations. Send an Idempotency-Key header with any POST under /v1/orgs/{orgId}/:

Shell
IDEMPOTENCY_KEY=$(uuidgen) # send the same key again if you retry
curl -X POST https://api.wristworkout.com/v1/orgs/org_id/invitations \
  -H "Authorization: Bearer $WW_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
  -d '{ "kind": "customer", "label": "Tuesday small group" }'
  • The value is 1 to 64 letters, digits, _ and -. A random UUID works well. It may also be sent as a quoted string, "…".
  • A successful response is kept for 24 hours, for your key and that value. Send the identical request again (the same method, path and body, byte for byte) and you get the same status and body back, with the header Idempotent-Replayed: true. Nothing happens twice.
  • The same value with a different request gets 422 VALIDATION, reason idempotency_key_reused.
  • Sending it again while the first request is still running gets 409, reason idempotency_in_progress, with Retry-After: 1. If the 409 lasts more than a minute, the first request did not finish, and the next retry with the same value runs the request.
  • Errors are not kept. After a 4xx nothing changed: fix the cause and send the request again, with the same value or a new one. After a 5xx, a retry runs the request again.
  • The header is optional. Without it, every request runs.

These operations accept it:

The rules follow the IETF draft draft-ietf-httpapi-idempotency-key-header-09.