Cabinhub News API v1.0.0

Docs / Rate limits and quotas

Rate limits and quotas

One quota per database key per UTC month, plus an optional per-minute request limiter. Both answer 429; they are told apart by the body and the headers.

429 is two different things

The monthly billable-item quota and the optional per-minute request limiter both answer 429, with different bodies and different headers. Read detail.error: quota_exceeded or rate_limited.

Header names in this contract

HeaderValue seen in the contract
content-typeapplication/json
retry-after2254140
x-ratelimit-limit60
x-ratelimit-remaining0
x-ratelimit-reset2026-11-05T12:01:00.000000Z
x-ratelimit-window60

Extracted from the response and request examples in docs/API.md §4 on every build.

The quota rule, the bodies and the guidance

Source: docs/API.md §4.

4.1 The quota rule

A database key is billed per distinct item received in a UTC month. Re-polling the same snapshot does not bill the same item twice, and a 304 bills no items (it still counts as one request in /v1/usage). The quota check is:

Text
if used > quota: 429

It is > and not >= on purpose: an item is billed when it is served, so refusing the request that would serve the quota's last item would make a metered contract unusable.

Measured on this build: a key with quota=1 made one /v1/items?limit=2 call (200, 2 items served); the next metered call returned 429.

4.2 429 body and headers (verified)

Text
HTTP/1.1 429 Too Many Requests
retry-after: 2254140
cache-control: no-store
content-type: application/json
JSON
{"detail":{"error":"quota_exceeded","detail":"monthly billable-item quota exhausted","quota":1,"used":2,"resets_at":"2026-12-01T00:00:00.000000Z"}}

Retry-After is seconds until the first instant of the next UTC month (at least 1).

Ordering. Authentication, then the per-key request limiter (§4.3, when enabled), then the quota check all run inside the authentication dependency — i.e. before query-parameter validation and before conditional-request evaluation. An exhausted key receives 429 for every metered route, including requests that would otherwise be 400 for a bad limit and including a poll that would have been answered 304. Do not treat a 429 where you expected a 400 as a second bug; check the quota (and the request limiter) first. A 401 is decided before either 429, so a bad bearer never spends the key's request window.

4.3 Per-key request limiter (off by default)

A fixed 60 s request limiter is wired into the authentication dependency, but it is off unless the operator sets NEWS_RATE_LIMIT_PER_MINUTE to a positive integer (the default 0, and any unreadable/negative value, leaves it disabled). It applies to database-issued keys only; legacy NEWS_API_KEYS bearers and dev mode are never limited, because they have no customer identity to meter. The counter is one SQLite row per key per window, incremented under BEGIN IMMEDIATE, so the limit is shared across uvicorn workers rather than multiplied by them.

When enabled and the key has spent its window, the request is refused before the handler runs (before query validation, before the quota check, before any item is metered). The refusal is a second 429 shape, distinct from the monthly quota:

Text
HTTP/1.1 429 Too Many Requests
retry-after: 42
cache-control: no-store
x-ratelimit-limit: 60
x-ratelimit-remaining: 0
x-ratelimit-window: 60
x-ratelimit-reset: 2026-11-05T12:01:00.000000Z
content-type: application/json
JSON
{"detail":{"error":"rate_limited","detail":"too many requests for this api key","limit":60,
 "window_seconds":60,"retry_after":42,"resets_at":"2026-11-05T12:01:00.000000Z"}}

Retry-After is seconds until the window resets and is always at least 1. An auth failure (401) is evaluated before the limiter, so junk requests cannot spend a real key's window. Item metering is unchanged: a 304 is zero billable items, and a 4xx/5xx is not metered at all.

4.4 Rate-limit headers on allowed responses

The headers below describe the per-key request budget of §4.3 only. They exist so a client can pace itself instead of discovering its budget by being refused.

Two spellings, one verdict. The standard fields from draft-ietf-httpapi-ratelimit-headers-11 (the IETF HTTPAPI Working Group draft, May 2026 — datatracker entry doc/draft-ietf-httpapi-ratelimit-headers) are sent first; the X-RateLimit-* names this API has published since 2026-11-05 are sent alongside them from the same dict, so the two can never quote different numbers. Both are produced by backend/app/api/rate_limit.py::rate_limit_headers.

HeaderUnitMeaning
RateLimit-PolicyRFC 8941 ListThe policy: "perkey";q=<limit>;w=<window>. q is the quota in requests (the draft's default quota unit), w the fixed window in seconds. Describes the policy, not the count, so it is stable between responses.
RateLimitRFC 8941 ListThe service limit: "perkey";r=<remaining>;t=<seconds>. r is the quota left after this request; t is the draft's effective window — seconds from this response until the count returns. r=0 means the next request in this window is refused.
X-RateLimit-LimitrequestsLegacy spelling of q.
X-RateLimit-RemainingrequestsLegacy spelling of r.
X-RateLimit-WindowsecondsLength of the fixed window, 60. Constant; sent so a client never hard-codes it.
X-RateLimit-ResetRFC3339 UTC instantWhen the current window ends (e.g. 2026-11-05T12:01:00.000000Z). Absent if the limiter has no reset instant to report.

Worked example — the first request of a 60-requests-per-minute window, 10 s in:

Text
RateLimit-Policy: "perkey";q=60;w=60
RateLimit: "perkey";r=59;t=50
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
X-RateLimit-Window: 60
X-RateLimit-Reset: 2026-11-05T12:01:00.000000Z

Why t and not an absolute instant: the draft (§4.1.2) defines t as a delay in seconds so a client with a skewed clock is never told to wait past the reset; X-RateLimit-Reset keeps the absolute instant for clients that prefer it. On a refusal, t carries the same number as Retry-After — a client given r=0;t=17 and Retry-After: 42 would be right to distrust both. The draft also says Retry-After takes precedence when both are present (§7), so an existing RFC 9110 client keeps working unchanged.

They appear on allowed responses as well as on the limiter's 429 — a 200, a 206, a 304, and a 400 for a bad query parameter, because every one of those spent a request. They are attached by backend/app/api/rate_headers.py, which copies them from the same helper the refusal uses, so an allowed response and a refusal can never quote different numbers. A response served from the read cache carries the count for that request, never the count stored with the cached bytes.

They are not sent when there is nothing to meter or nothing to say:

A 429 with "error": "quota_exceeded" (§4.2) also carries them: the request limiter allowed that request before the monthly quota refused it, so the window was spent. That refusal's Retry-After is the wait to the next UTC month, not t — the rate-limit fields keep describing the 60 s window they always describe.

The public mirror /v1/site/* publishes the same six headers from its own per-IP limiter (§5a), so a browser rendering the reader site can pace itself the same way a server-side customer does.

These are not the monthly quota. The billable allowance a plan is priced on is counted in unique items delivered per UTC month (§4.1) and is reported by GET /v1/usage (billable_items, quota, remaining, resets_at). RateLimit: "perkey";r=3;t=48 means "three more requests in the next 48 seconds"; it says nothing about how many items remain in the month. Do not build an item budget out of these numbers. No X-Quota-* header exists on this build: the monthly figures live only in the /v1/usage body.

Browser clients. Both CORS layers now expose every one of these names: app/api/cors.py publishes expose_headers containing RateLimit, RateLimit-Policy, the four X-RateLimit-* names and Retry-After (see §5b for the exact value). Before 2026-11-12 the credentialed layer listed only ETag, Last-Modified, Cache-Control, Retry-After, so a browser received X-RateLimit-* and its JavaScript could not read them; backend/tests/test_rate_headers_standard.py::test_both_cors_layers_expose_every_rate_limit_header now fails if either list falls behind the producer.

The document side matches the wire: every metered operation declares these headers on its 200/304/400 and on its 429, so a generated SDK binds a typed accessor instead of discovering the numbers by hand.

Guidance that follows from the cache headers and the pipeline schedule:

4.5 Request correlation: X-Request-Id

Every response carries an X-Request-Id. It is the one value to quote in a support ticket: it appears on our access log line for that request, so one id joins your report to the hop that served it.

Text
HTTP/1.1 304 Not Modified
x-request-id: 6f1d0a2c4b8e4d5f9a3c7e2b1d0f4a6c
etag: "9b2f0c1d4e6a8b0c2d4e6f8a0b2c4d6e"

Rules (implemented in backend/app/api/request_id.py, pinned by backend/tests/test_request_id.py and backend/tests/test_request_id_mount.py; the module is mounted by one line in create_production_app):

Cross-origin browser code cannot read it until X-Request-Id is added to the CORS expose_headers (and to allow_headers if the browser also sends one); the current list is the one printed in §5. Server-side clients are unaffected.