Calling Tidegate from a service

A service calls Tidegate's HTTP API with an API key, sent as a bearer token in the Authorization header. Never put a key in a URL.

Your organization's endpoints are under:

https://tidegate.attune.inc/tenants/{organization}

where {organization} is your organization's id. The Keys page, in Settings, shows this address with your organization's id filled in. The examples below read it from $TIDEGATE_ORGANIZATION.

The full API is described by its OpenAPI document, at https://tidegate.attune.inc/openapi.json, which most languages can generate a client from.

Reserve before the call

curl -X POST https://tidegate.attune.inc/tenants/$TIDEGATE_ORGANIZATION/reservations \
  -H "Authorization: Bearer $TIDEGATE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "summary-8f14e45f",
    "quantities": { "openai-tokens": 4000 },
    "labels": { "member": "user_42", "job": "summary" }
  }'
  • id is yours to choose, unique per call. Retrying with the same id never charges twice.
  • quantities maps meter ids to estimated whole units. One reservation can charge several meters.
  • labels are what budgets narrow and split on. Send every label a budget splits by, or that budget won't count the call.

The request never names an app or an environment: the reservation is the app's that the key belongs to, in the key's environment.

A grant is a 200 with the reservation's id and when it was made. A denial is a 429 that names the budget that refused, its window, limit, spend so far, and what you asked for:

{
  "_tag": "Denied",
  "message": "budget summaries-per-member has 98500 of its limit of 100000 units spent in this window, so it refused 4000 more",
  "budget": "summaries-per-member",
  "split": { "member": "user_42" },
  "window": { "start": 1767225600000, "end": 1769904000000 },
  "unit": "units",
  "limit": 100000,
  "spent": 98500,
  "requested": 4000,
  "frozen": false
}

Times are epoch milliseconds. unit says what limit, spent, and requested count: the meter's units, or microdollars, for a budget in dollars, which adds up every line of the reservation on its meters. frozen is true when an admin has frozen the budget, which refuses everything it counts, whatever its spend, until it is unfrozen.

Settle after the call

curl -X POST https://tidegate.attune.inc/tenants/$TIDEGATE_ORGANIZATION/settlements \
  -H "Authorization: Bearer $TIDEGATE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "id": "summary-8f14e45f", "quantities": { "openai-tokens": 3127 } }'

Settle with the same meters you reserved. See Reservations and settlements for what settling changes.

When Tidegate is unreachable

Tidegate can't decide for you what a call should do if it can't reach Tidegate. Decide at each call site: fail closed for spend you must never exceed, or fail open where an outage costs more than the overspend.

Other errors

Every error is a JSON object like the denial above: _tag names the error, message says what happened in a sentence, and any fields of its own sit beside them. Branch on _tag and the status, and log message; its wording can change.

  • 400 (BadRequest): the request isn't one the API takes, and message says what and where, such as body.labels: key "bad key": "bad key" is not a label key: .... Fields the API doesn't know are refused too, as are label keys that break their rule, and numbers with a fraction where a whole number goes.
  • 415 (UnsupportedMediaType): the body wasn't sent with Content-Type: application/json.
  • 401: the key is missing, revoked, or not one of this organization's.
  • 403: the key's role doesn't allow the call.
  • 404: a settlement names a reservation that doesn't exist.
  • 409: the reservation id was used with other quantities or labels, or by another app's or environment's key, or the reservation was already settled with other quantities.
  • 422: a meter isn't defined, or a settlement names other meters than its reservation, or a budget in dollars counts a meter with no price now (UnpricedMeter), which an admin fixes by setting one.
  • 429 (TooManyRequests): too many of your key's calls were in flight at once for Tidegate to answer this one within a few seconds, so it turned it away without doing anything with it. A denial is a 429 too, so tell them apart by _tag. Each key, signed-in person, and agent has a few calls at a time, and spend reports one; the rest wait their turn, in the order they came, until it is clear they can't be answered in time. Wait as Retry-After says, then send it again, as for a 503; if it keeps coming, send fewer calls at once.
  • 503 (Unavailable): Tidegate is too busy to take the call just now (code is busy), or couldn't reach its database to answer it (database_unavailable). Wait as long as its Retry-After header says, in seconds (retryAfter in the body says the same), then send the same request again: a reservation or settlement sent again with the same id never counts twice. If it is still refused when the call can't wait any longer, treat it as unreachable.