Counter API

A small counter API. Built to be dependable.

Create counters, read their current values, and keep a durable record of each change.

Scoped keys for tenant accessIdempotent changes safe to retryOperation history records each mutation

API reference

POST

Create Tenant

POST {base_url}/v2/tenants

Request headers

KeyValueDescription
X-API-Key{{ADMIN_API_KEY}}Administrator key required.
Content-Typeapplication/jsonRequest body is JSON.

Request body

KeyValueDescription
label"production"Required and unique. 1–255 characters; letters, numbers, spaces, hyphens, underscores, and periods.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request POST "$BASE_URL/v2/tenants" --header "X-API-Key: $ADMIN_API_KEY" --header "Content-Type: application/json" --data-raw '{"label":"production"}'

Sample response

{
  "tenant_id": "01912345-6789-7000-8000-000000000001",
  "label": "production",
  "created_at": "2026-09-25T08:00:00Z",
  "updated_at": "2026-09-25T08:00:00Z"
}

Creates a new tenant with a unique label. Tenants act as isolated workspaces that contain counters.

Idempotency: Optional Idempotency-Key header containing a UUID. Reuse

the key with the same administrator credential, URL, and request to replay

the original tenant response for 24 hours. Replays return

Idempotency-Replayed: true; changing request data with the same key returns

409 IDEMPOTENCY_KEY_REUSED.

Authentication: Requires API key via X-Api-Key header

Rate Limit: 10 requests per minute (configurable via RATE_LIMIT_REQUESTS)

Errors

  • 400 INVALID_JSON - Invalid JSON in request body
  • 400 INVALID_PARAMETER - Label validation failed
  • 409 TENANT_LABEL_EXISTS - Label already in use
  • 401 UNAUTHORIZED - Missing or invalid API key
  • 429 RATE_LIMIT_EXCEEDED - Too many requests
GET

Get Tenant

GET {base_url}/v2/tenants/{tenant_id}

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires tenant:read for this tenant or an administrator key.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request GET "$BASE_URL/v2/tenants/$TENANT_ID" --header "X-API-Key: $TENANT_API_KEY"

Sample response

{
  "tenant_id": "01912345-6789-7000-8000-000000000001",
  "label": "production",
  "created_at": "2026-09-25T08:00:00Z",
  "updated_at": "2026-09-25T08:00:00Z"
}

Retrieves tenant information by tenant ID.

Authentication: Requires an administrator key or a tenant key with tenant:read.

Rate Limit: 30 requests per minute (configurable via RATE_LIMIT_GET_MULTIPLIER)

Response (200 OK)

Errors

  • 400 INVALID_UUID - Invalid tenant_id format
  • 404 TENANT_NOT_FOUND - Tenant does not exist
  • 429 RATE_LIMIT_EXCEEDED - Too many requests
POST

Create Credential

POST {base_url}/v2/tenants/{tenant_id}/credentials

Request headers

KeyValueDescription
X-API-Key{{ADMIN_API_KEY}}Administrator key required to manage credentials.
Content-Typeapplication/jsonRequest body is JSON.

Request body

KeyValueDescription
scopes["counter:read", "counter:increment", "counter:history"]Optional; supports tenant:read, counter:read, counter:list, counter:create, counter:increment, counter:adjust, counter:history, metadata:read, and metadata:write. Defaults to read, increment, and history; listing is not granted by default.
expires_at"2026-12-31T23:59:59Z"Optional expiration timestamp; omitted for a non-expiring credential.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request POST "$BASE_URL/v2/tenants/$TENANT_ID/credentials" --header "X-API-Key: $ADMIN_API_KEY" --header "Content-Type: application/json" --data-raw '{"scopes":["counter:read","counter:increment","counter:history"]}'

Sample response

{
  "credential_id": "01912345-6789-7000-8000-000000000010",
  "tenant_id": "01912345-6789-7000-8000-000000000001",
  "api_key": "ck_01912345-6789-7000-8000-000000000010.example-secret",
  "scopes": [
    "counter:read",
    "counter:increment",
    "counter:history"
  ]
}

Creates a tenant-scoped key. Requires the administrator API key. The secret is returned once; store it securely. Supported scopes: tenant:read, counter:read, counter:list, counter:create, counter:increment, counter:adjust, counter:history, metadata:read, and metadata:write. By default, keys receive read, increment, and history scopes; listing, counter creation, adjustment, and metadata access must be granted explicitly.

POST

Create Counter

POST {base_url}/v2/tenants/{tenant_id}/counters

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Administrator key or tenant key with counter:create.
Content-Typeapplication/jsonRequest body is JSON.

Request body

KeyValueDescription
label"daily_steps"Optional; unique within this tenant, 1–255 characters.
initial_value0Optional int64; defaults to 0.
max_delta50Optional positive int64; defaults to 50 and must be at least 1.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request POST "$BASE_URL/v2/tenants/$TENANT_ID/counters" --header "X-API-Key: $TENANT_API_KEY" --header "Content-Type: application/json" --data-raw '{"label":"daily_steps","initial_value":0,"max_delta":50}'

Sample response

{
  "counter_id": "01912345-6789-7000-8000-000000000002",
  "tenant_id": "01912345-6789-7000-8000-000000000001",
  "label": "daily_steps",
  "value": 0,
  "max_delta": 50,
  "created_at": "2026-09-25T08:00:00Z",
  "updated_at": "2026-09-25T08:00:00Z"
}

Creates a new counter under the specified tenant.

Idempotency: Optional Idempotency-Key header containing a UUID. Reuse

the key with the same caller, URL, and request to replay the original counter

response for 24 hours. Replays return Idempotency-Replayed: true; changing

request data with the same key returns 409 IDEMPOTENCY_KEY_REUSED.

Authentication: Requires an administrator key or tenant-scoped key with

the counter:create scope.

Rate Limit: 10 requests per minute (configurable via RATE_LIMIT_REQUESTS)

Errors

  • 400 INVALID_JSON - Invalid JSON in request body
  • 400 INVALID_UUID - Invalid tenant_id format
  • 400 INVALID_PARAMETER - Parameter validation failed
  • 404 TENANT_NOT_FOUND - Tenant does not exist
  • 409 COUNTER_LABEL_EXISTS - Label already exists for this tenant
  • 401 UNAUTHORIZED - Missing or invalid API key
  • 429 RATE_LIMIT_EXCEEDED - Too many requests
GET

List Counters

GET {base_url}/v2/tenants/{tenant_id}/counters

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires counter:list for this tenant or an administrator key.

Query parameters

KeyValueDescription
limit20Optional page size, 1–100.
cursorOptional opaque next_cursor from the preceding response.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request GET "$BASE_URL/v2/tenants/$TENANT_ID/counters?limit=20" --header "X-API-Key: $TENANT_API_KEY"

Sample response

{
  "counters": [],
  "next_cursor": null
}

Lists all counters belonging to a tenant with cursor-based pagination.

Authentication: Requires an administrator key or a tenant key with the counter:list scope.

Rate Limit: 30 requests per minute (configurable via RATE_LIMIT_GET_MULTIPLIER)

Response (200 OK)

Pagination

Use limit to control page size and pass the returned next_cursor as the cursor query parameter to retrieve the next page. The cursor is an opaque base64-encoded token — do not construct or modify it.

Errors

  • 400 INVALID_UUID - Invalid tenant_id format
  • 400 INVALID_PARAMETER - limit is not between 1 and 100
  • 400 INVALID_CURSOR - Cursor is malformed
  • 404 TENANT_NOT_FOUND - Tenant does not exist
  • 401 UNAUTHORIZED - Missing or invalid API key
  • 429 RATE_LIMIT_EXCEEDED - Too many requests
GET

Get Counter

GET {base_url}/v2/tenants/{tenant_id}/counters/{counter_id}

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Tenant credentials require counter:read; administrator keys are also accepted.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request GET "$BASE_URL/v2/tenants/$TENANT_ID/counters/$COUNTER_ID" --header "X-API-Key: $TENANT_API_KEY"

Sample response

{
  "counter_id": "01912345-6789-7000-8000-000000000002",
  "tenant_id": "01912345-6789-7000-8000-000000000001",
  "label": "daily_steps",
  "value": 8421,
  "max_delta": 100000,
  "created_at": "2026-09-25T08:00:00Z",
  "updated_at": "2026-09-25T12:01:00Z"
}

Retrieves the current value and details of a specific counter.

Authentication: Requires a key with counter:read (administrator keys

are also accepted).

Reads use the configured bounded read-through cache. See the deployment

guide for cache configuration and consistency behavior.

Response (200 OK)

Errors

  • 400 INVALID_UUID - Invalid tenant_id or counter_id format
  • 404 COUNTER_NOT_FOUND - Counter does not exist
  • 429 RATE_LIMIT_EXCEEDED - Too many requests
POST

Increment Counter

POST {base_url}/v2/tenants/{tenant_id}/counters/{counter_id}/inc

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires counter:increment.
Idempotency-Key{{UUID_IDEMPOTENCY_KEY}}Required UUID; reuse only for retries of this same mutation.

Query parameters

KeyValueDescription
delta5Optional positive int64 increment; defaults to 1 and cannot exceed max_delta.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request POST "$BASE_URL/v2/tenants/$TENANT_ID/counters/$COUNTER_ID/inc?delta=5" --header "X-API-Key: $TENANT_API_KEY" --header "Idempotency-Key: $IDEMPOTENCY_KEY"

Sample response

{
  "operation_id": "01912345-6789-7000-8000-000000000003",
  "counter_id": "01912345-6789-7000-8000-000000000002",
  "delta": 5,
  "value": 47,
  "replayed": false,
  "updated_at": "2026-09-25T12:01:00Z"
}

Increments a counter value by a specified delta amount. Useful for tracking events like page views, clicks, or any countable metric.

Authentication: Requires an administrator key or a tenant key with

counter:increment. A UUID idempotency key is required.

Rate Limit: Controlled by the configured API rate limits.

Response (200 OK)

Errors

  • 400 INVALID_UUID - Invalid tenant_id or counter_id format
  • 400 INVALID_DELTA - Delta parameter is not a positive integer
  • 404 COUNTER_NOT_FOUND - Counter does not exist
  • 400 COUNTER_OVERFLOW - Increment would cause integer overflow
  • 409 IDEMPOTENCY_KEY_REUSED - Idempotency key was reused with different request data
  • 409 OPERATION_IN_PROGRESS - The operation is already being completed
  • 429 RATE_LIMIT_EXCEEDED - Too many requests
POST

Set Counter

POST {base_url}/v2/tenants/{tenant_id}/counters/{counter_id}/set

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires the counter:adjust scope.
Idempotency-Key{{UUID_IDEMPOTENCY_KEY}}Required UUID; reuse only for retries of this same request.
Content-Typeapplication/jsonRequest body is JSON.

Request body

KeyValueDescription
value100Required int64 value to set.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request POST "$BASE_URL/v2/tenants/$TENANT_ID/counters/$COUNTER_ID/set" --header "X-API-Key: $TENANT_API_KEY" --header "Idempotency-Key: $IDEMPOTENCY_KEY" --header "Content-Type: application/json" --data-raw '{"value":100}'

Sample response

{
  "operation_id": "01912345-6789-7000-8000-000000000004",
  "counter_id": "01912345-6789-7000-8000-000000000002",
  "delta": 53,
  "value": 100,
  "replayed": false,
  "updated_at": "2026-09-25T12:03:00Z"
}

Sets the counter value with a required UUID Idempotency-Key and records

the adjustment in operation history.

Authentication: Requires an administrator key or a tenant key with counter:adjust.

Rate Limit: 10 requests per minute (configurable via RATE_LIMIT_REQUESTS)

Response (200 OK)

Errors

  • 400 INVALID_JSON - Invalid JSON in request body
  • 400 INVALID_UUID - Invalid tenant_id or counter_id format
  • 400 INVALID_VALUE - Value is out of valid range
  • 404 COUNTER_NOT_FOUND - Counter does not exist
  • 401 UNAUTHORIZED - Missing or invalid API key
  • 409 IDEMPOTENCY_KEY_REUSED - Idempotency key was reused with different request data
  • 409 OPERATION_IN_PROGRESS - The operation is already being completed
  • 429 RATE_LIMIT_EXCEEDED - Too many requests
GET

Operation History

GET {base_url}/v2/tenants/{tenant_id}/counters/{counter_id}/operations?limit=50

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires the counter:history scope.

Query parameters

KeyValueDescription
limit50Optional integer from 1–100; defaults to 50.
cursor{{CURSOR}}Optional opaque cursor; pass next_cursor unchanged.

Response headers

KeyValueDescription
Content-Typeapplication/jsonJSON response body.

Sample request

curl --silent --show-error --request GET "$BASE_URL/v2/tenants/$TENANT_ID/counters/$COUNTER_ID/operations?limit=50" --header "X-API-Key: $TENANT_API_KEY"

Sample response

{
  "operations": [
    {
      "operation_id": "01912345-6789-7000-8000-000000000003",
      "counter_id": "01912345-6789-7000-8000-000000000002",
      "kind": "increment",
      "delta": 5,
      "value_before": 42,
      "value_after": 47,
      "metadata": {},
      "created_at": "2026-09-25T12:01:00Z",
      "completed_at": "2026-09-25T12:01:00Z"
    }
  ],
  "next_cursor": null
}

Returns paginated, reverse-chronological counter operations. Requires a

tenant-scoped credential with counter:history.

Pass the opaque next_cursor from a response unchanged to fetch the next

GET

Get Metadata

GET {base_url}/v2/tenants/{tenant_id}/counters/{counter_id}/metadata

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires metadata:read or an administrator key.

Response headers

KeyValueDescription
Content-Typeapplication/jsonDocument envelope.
ETag"<version>"Opaque version; preserve quotes for If-Match.
Cache-Controlno-storeMetadata is never cached.

Sample request

curl --silent --show-error --include "$BASE_URL/v2/tenants/$TENANT_ID/counters/$COUNTER_ID/metadata" --header "X-API-Key: $TENANT_API_KEY"

Sample response

{
  "tenant_id": "01912345-6789-7000-8000-000000000001",
  "counter_id": "01912345-6789-7000-8000-000000000002",
  "data": {
    "color": "blue"
  },
  "version": "b858fc63-a09a-4ec2-922e-4e91629122dd",
  "created_at": "2026-10-19T10:00:00Z",
  "updated_at": "2026-10-19T10:00:00Z"
}

Reads a counter's optional independent JSON document. Requires metadata:read

or an administrator key. Counter read permissions do not grant metadata access.

Returns a document envelope with data, version, tenant/counter IDs, and

timestamps. Response headers include a quoted opaque ETag and

Cache-Control: no-store. Save the ETag for a later edit.

Any JSON value, including null, can be stored. Missing metadata returns

404 METADATA_NOT_FOUND; a missing counter returns 404 COUNTER_NOT_FOUND.

Metadata is not included in regular counter responses or operation history.

PUT

Put Metadata

PUT {base_url}/v2/tenants/{tenant_id}/counters/{counter_id}/metadata

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires metadata:write or an administrator key.
Content-Typeapplication/jsonRaw JSON value.
If-None-Match*Required to create. For replacement, send If-Match with the quoted current ETag instead.
Idempotency-Key{{UUID_IDEMPOTENCY_KEY}}Optional UUID. Retain the body and conditions for retries.

Request body

KeyValueDescription
(raw value){"color":"blue"}Any JSON value, at most 64 KiB and 64 nesting levels; JSONB limits apply.

Response headers

KeyValueDescription
Content-Typeapplication/jsonDocument envelope.
ETag"<version>"Opaque version; preserve quotes for If-Match.
Cache-Controlno-storeMetadata is never cached.

Sample request

curl --silent --show-error --include --request PUT "$BASE_URL/v2/tenants/$TENANT_ID/counters/$COUNTER_ID/metadata" --header "X-API-Key: $TENANT_API_KEY" --header "Content-Type: application/json" --header 'If-None-Match: *' --header "Idempotency-Key: $IDEMPOTENCY_KEY" --data-raw '{"color":"blue"}'

Sample response

{
  "tenant_id": "01912345-6789-7000-8000-000000000001",
  "counter_id": "01912345-6789-7000-8000-000000000002",
  "data": {
    "color": "blue"
  },
  "version": "b858fc63-a09a-4ec2-922e-4e91629122dd",
  "created_at": "2026-10-19T10:00:00Z",
  "updated_at": "2026-10-19T10:00:00Z"
}

Creates or fully replaces a document. Requires metadata:write or an

administrator key. Send raw JSON: object, array, scalar, or null, up to

64 KiB and 64 nesting levels, subject to PostgreSQL JSONB limits.

To create, send If-None-Match: * for a 201 response. To replace, remove

that header and send If-Match with the exact quoted ETag from GET for 200.

Both responses contain the document envelope and a new ETag. Changes do not

affect the counter value. Properties omitted from a replacement are removed.

Missing preconditions return 428, malformed conditions 400, stale ETags

or creation against an existing document 412. Invalid JSON returns 400;

oversized documents return 413.

Idempotency-Key is optional. Reuse a UUID with the same body and conditional

headers to recover the original response/ETag for 24 hours. Changed headers

with the same key return 409. Replay returns the original version even if

later edits occurred; GET before starting a new edit.

DELETE

Delete Metadata

DELETE {base_url}/v2/tenants/{tenant_id}/counters/{counter_id}/metadata

Request headers

KeyValueDescription
X-API-Key{{TENANT_API_KEY}}Requires metadata:write or an administrator key.
If-Match"<version>"Required exact quoted ETag from GET.
Idempotency-Key{{UUID_IDEMPOTENCY_KEY}}Optional UUID; retry with the same conditional header.

Response headers

KeyValueDescription
Cache-Controlno-store204 response has no body.

Sample request

curl --silent --show-error --include --request DELETE "$BASE_URL/v2/tenants/$TENANT_ID/counters/$COUNTER_ID/metadata" --header "X-API-Key: $TENANT_API_KEY" --header "If-Match: $METADATA_ETAG" --header "Idempotency-Key: $IDEMPOTENCY_KEY"

Removes the document without changing the counter. Requires metadata:write

or an administrator key and the current quoted ETag in If-Match.

Success returns 204 with no body. Missing conditions return 428, malformed

conditions 400, and stale or absent documents 412.

Idempotency-Key is optional. Keyed retries replay the original 204 for

24 hours. Deleting then recreating metadata generates a fresh ETag.