SDKs
SDKs
Clients for TypeScript, Python and Elixir wrap the API the same way: one client per API key, errors with type, request_id and doc_url, retries for network errors, 429, 500 and 502–504, an automatic Idempotency-Key on every command, and an event follower that resumes from your cursor.
import { OneKey } from "@1key/api";
const onekey = new OneKey({ apiKey: process.env.ONEKEY_API_KEY });
const { data: hubs } = await onekey.hubs.list();
await onekey.devices.command(hubs[0].id, 7, { key: "lock", value: false });
for await (const event of onekey.events.follow({ after: savedCursor })) {
handle(event);
saveCursor(event.cursor);
}
from onekey import OneKey
onekey = OneKey(api_key=os.environ["ONEKEY_API_KEY"])
hubs = onekey.hubs.list()["data"]
onekey.devices.command(hubs[0]["id"], 7, key="lock", value=False)
for event in onekey.events.follow(after=saved_cursor):
handle(event)
save_cursor(event["cursor"])
client = Onekey.new(api_key: System.fetch_env!("ONEKEY_API_KEY"))
{:ok, %{"data" => [hub | _]}} = Onekey.Hubs.list(client)
{:ok, _} = Onekey.Devices.command(client, hub["id"], 7, key: "lock", value: false)
client
|> Onekey.Events.follow(after: saved_cursor)
|> Stream.each(fn event -> handle(event); save_cursor(event["cursor"]) end)
|> Stream.run()
| Language | Package | Install |
|---|---|---|
| TypeScript / JavaScript (Node 18+) | @1key/api |
npm install @1key/api |
| Python 3.9+ | onekey-api |
pip install onekey-api |
| Elixir | onekey |
{:onekey, "~> 1.0"} in mix.exs |
What every SDK does
| Errors | An error carries the API's type, message, request_id and doc_url. Branch on type |
| Retries | Network errors, 429 (after Retry-After), 500 and 502–504, up to three times. A POST is retried only when it carries an Idempotency-Key or got 429, so nothing is created twice |
| Commands | Sent with an Idempotency-Key, a new UUID unless you pass one, so a retried command runs once |
| Paging | Iterate every site or hub without handling starting_after |
| Events | Follow your organization's event log (or one hub's) from a cursor, holding each request open; an expired cursor raises cursor_expired: read current state, then follow from the newest event |