Python SDK
UnpublishedPure stdlib, zero dependencies, arbitrary precision via native int.
Not yet published
Initialize
The API key is the first positional argument. base_url defaults to the production host.
import os
from counters import CountersClient
client = CountersClient(os.environ["COUNTERS_API_KEY"])Count
add() and subtract() are buffered and coalesced per counter (flushed as one POST /batch); add_now()/subtract_now() write immediately and return the counter. amount accepts an int or a decimal string — Python's native int is already arbitrary precision.
reg = client.counter("registrations")
reg.add(1) # buffered
reg.add(5) # coalesced
client.flush() # one POST /batch
c = reg.add_now(1) # immediate; returns a dict
print(c["value"]) # "42" — a stringRead
Read the current value, or a per-bucket time series.
value = reg.value()["value"]
series = reg.series(**{
"from": "2026-01-01T00:00:00Z",
"to": "2026-01-08T00:00:00Z",
"bucket": "1d",
})from is a Python keyword
Manage
clear() resets to zero (new epoch, history retained); delete() tombstones the counter; list() pages the organization's counters.
reg.clear()
reg.delete()
page = client.list(limit=50)Lifecycle
Buffered writes flush on a background thread (on an interval and at max_batch_size). Call close() before exit to flush and stop the thread.
client.close() # flush + stop the background threadErrors
Invalid keys or amounts raise CountersValidationError before any request. Failed requests raise CountersError, carrying .status and the RFC 9457 .problem body.
from counters import CountersError, CountersValidationError
try:
reg.add_now(1)
except CountersValidationError:
raise # bad input
except CountersError as e:
print(e.status, e.problem)