Python API Client
koios-client is a typed Python SDK for the Koios API. It gives you three
layers, and you can mix them freely in one script:
- a resource API of
Device,Tag, andModelobjects with methods like.enable(),.update(), and.delete() - a fully typed GraphQL client at
client.gql, with editor autocomplete for every query and mutation - file helpers for import and export, model files, backups, trend data, and log downloads
Installation
Choosing a version
The client's minor tracks the server's. Install the line that matches the Koios you run:
The client is generated from the server's API schema, so a newer client against an older server offers operations that server does not have.
Credentials
You authenticate with an API client ID and secret, created under System. See API Clients for how to create one and grant it permissions. The secret is shown once, when the client is created.
Getting started
Or as a context manager, which closes the connection for you:
Working with resources
Every resource is fetched by ID or by slug. Exactly one is required.
To find something by name, filter the list:
A returned list supports iteration, indexing, len(), and truth testing.
len() counts the current page; total_count is everything matching on the
server.
Creating, updating, deleting
update returns a fresh object with every field refreshed, so use the return
value rather than the object you called it on.
Following relationships
Acting on many at once
Live values
Current values come from the live data cache rather than the configuration database, so they are cheap to poll:
What you can reach
Devices, tags, models, scan groups, device sets, protocols, and API clients each expose the same shape: a singular getter, a plural list with filters and pagination, a create, bulk enable and disable, and a bulk delete. Protocols are read-only apart from visibility, and are not paginated.
Device sets group devices for failover. Members are added through
client.gql, and the active member is chosen by the member record's ID, not
the device's:
See Device Sets.
The GraphQL layer
Everything the API offers is available on client.gql, typed end to end.
A mutation returns either the entity or a structured error. check_operation
unwraps it, raising if the server rejected the request:
Every API enum is importable, so you never compare against a bare string:
Files, imports, and exports
Import and export
Imports preview by default, so client.import_devices("devices.csv") shows
you what would change and applies nothing until you pass dry_run=False. See
Importing & Exporting Devices.
Model files
See Managing Model Files.
Backups
Backups and restores run in the background, so poll the task until it reports completion. See Backup & Restore.
Trend exports
client.estimate_trend_export(...) reports the approximate size before you
commit to a large range, along with the size limit. The server refuses an export
estimated over that limit, or a raw export whose size it cannot estimate at the
time, so client.create_trend_export(...) raises an error for that request
instead of returning a task. See Settings & Export.
Other uploads and downloads
Connection options
Set verify_ssl=False only against a server using a self-signed certificate.
Installing your own certificate is the better answer for anything long-lived —
see Certificates.
Handling errors
KoiosError is the base class, so catch it when you only need to know that
something failed. OperationError carries the server's own validation
messages, including which field each one is about.
