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:
{
"items": [{ "id": "c4Rt8NwZ1bYs", "displayName": "Sam" }],
"nextCursor": "WyJzYW0iLCJjNFJ0OE53WjFiWXMiXQ"
}- Send
nextCursorback as thecursorquery parameter for the next page. On the last page,nextCursoris null. limitsets 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
VALIDATIONwith the issue pathcursor. - 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.
// 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, whoselimitis 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}/:
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, reasonidempotency_key_reused. - Sending it again while the first request is still running gets 409, reason
idempotency_in_progress, withRetry-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:
- POST
/Invite a customer, or a trainer to a gymv1/ orgs/ {orgId}/ invitations - POST
/Create a library programmev1/ orgs/ {orgId}/ programmes - POST
/Duplicate a library programmev1/ orgs/ {orgId}/ programmes/ {programmeId}/ duplicate - POST
/Create an exercisev1/ orgs/ {orgId}/ exercises - POST
/Create a machinev1/ orgs/ {orgId}/ machines - POST
/Assign a programme to customersv1/ orgs/ {orgId}/ assignments - POST
/Add a webhook endpointv1/ orgs/ {orgId}/ webhooks - POST
/Send a test eventv1/ orgs/ {orgId}/ webhooks/ {endpointId}/ test
The rules follow the IETF draft draft-ietf-httpapi-idempotency-key-header-09.