# SDKs for JavaScript and Python

The SDKs are thin, typed clients for the [public API](/docs/api-reference). They return the API's own JSON, follow cursors for you, retry safely, raise typed errors and verify [webhook](/docs/webhooks) signatures. Neither has any dependencies. You need an [API key](/docs/api-keys).

| | JavaScript | Python |
| --- | --- | --- |
| Package | `@apidly/sdk` | `apidly` |
| Needs | Node 18 or newer, or any runtime with `fetch` | Python 3.9 or newer |
| Install | `npm install ./apidly-sdk-0.1.35.tgz` | `pip install ./apidly-0.1.35-py3-none-any.whl` |

## Install

The packages are not on npm or PyPI yet. Download them from apidly.com, check them against the checksum list and install the downloaded files. The check catches a damaged download, not a compromised site, because the files and the list come from the same place. Packages on npm and PyPI are planned.

```bash
curl -fsSLO https://apidly.com/downloads/SHA256SUMS
curl -fsSLO https://apidly.com/downloads/apidly-sdk-0.1.35.tgz
curl -fsSLO https://apidly.com/downloads/apidly-0.1.35-py3-none-any.whl
grep -F -e apidly-sdk-0.1.35.tgz -e apidly-0.1.35-py3-none-any.whl SHA256SUMS | sha256sum -c -    # macOS: shasum -a 256 -c -
npm install ./apidly-sdk-0.1.35.tgz
pip install ./apidly-0.1.35-py3-none-any.whl
```

pip can also install straight from the address. So can npm 11 and older; npm 12 refuses remote addresses unless you add `--allow-remote=root`. The npm address always serves the newest release, while the Python wheel is named with its version.

```bash
npm install --allow-remote=root https://apidly.com/downloads/apidly-sdk.tgz
pip install https://apidly.com/downloads/apidly-0.1.35-py3-none-any.whl
```

## JavaScript

```js
import { Apidly } from "@apidly/sdk";

const apidly = new Apidly({ apiKey: process.env.APIDLY_API_KEY });

const { plans } = await apidly.plans.list({ limit: 20 });
const { templates } = await apidly.templates.list();

const { server, deployment } = await apidly.servers.create({
  planId: plans[0].id,
  templateId: templates[0].id,
  label: "training",
  diskGb: 80,
  sshKeyIds: ["ssh_abc123"],
});
```

`server` is `null` while the request waits for approval; check `deployment.status`.

## Python

```python
import os
from apidly import Client

client = Client(api_key=os.environ["APIDLY_API_KEY"])

plans = client.plans.list(limit=20)["plans"]
templates = client.templates.list()["templates"]

result = client.servers.create(
    plan_id=plans[0]["id"],
    template_id=templates[0]["id"],
    label="training",
    disk_gb=80,
    ssh_key_ids=["ssh_abc123"],
)
```

`result["server"]` is `None` while the request waits for approval.

## What is covered

Both SDKs have a method for every operation in the [OpenAPI document](/api/v1/openapi.json), grouped by resource: `me`, `team`, `plans`, `gpus`, `regions`, `templates`, SSH keys, `servers` (including `exec` and `logs`), `secrets`, `deployments`, `billing` (balance, ledger, invoices, usage, top-up), `webhooks` and `groups`. SSH keys can be renamed, servers can be tagged and filtered by tag (`servers.tag` with `set`, `add` or `remove`, and the `tag` list filter), and an interruptible server's maximum price read and raised (`servers.getMaxPrice` and `servers.raiseMaxPrice` in JavaScript, `servers.get_max_price` and `servers.raise_max_price` in Python). A test in each package fails when the API gains a path or method the SDK does not have.

Also covered: `servers.recreate` (DESTRUCTIVE; `servers.recycle` is its deprecated alias), `servers.tagMany` (add or remove tags on up to 100 servers in one call), `servers.setNote` (your own note on a server), `servers.clone` (launch a copy of a server), `deployments.launchAgain` (a new request like an earlier one), `organization.defaults` and `organization.setDefaults` (the organization's launch defaults) and `profile.get` and `profile.update` (display name, language and time zone). In Python they are `tag_many`, `set_note`, `launch_again`, `set_defaults`, and so on.

Python uses snake_case (`ssh_keys`, `list_events`, `include_destroyed`); the data the API returns keeps its own names.

## Pages

Every list takes `limit` and `cursor` and returns `nextCursor`. The `iter` methods walk every page:

```js
for await (const server of apidly.servers.iter()) {
  console.log(server.id, server.status);
}
```

```python
for server in client.servers.iter():
    print(server["id"], server["status"])
```

## Retries and idempotency

Creating a server and starting a top-up send an `Idempotency-Key` (a random one unless you pass your own), so a retry after a timeout cannot create two servers or two checkouts. See [idempotency](/docs/api-reference#idempotency). The SDKs retry GET requests and idempotent calls up to twice on `429`, `502`, `503`, `504` and network errors, waiting as long as `retry-after` says. A plain action such as `start` is not retried.

## Errors

Every error extends one base class (`ApidlyError`). An API failure carries `status`, `code`, `message`, `requestId` and `retryAfter`; branch on `code`, not on the message. There is a class for each kind: validation (400, 422), authentication (401), insufficient balance (402), permission (403), not found (404), conflict (409), rate limit (429) and server (5xx, including `no_capacity`), plus a connection error when there is no answer at all.

## Verify a webhook

Pass the raw request body, the `Apidly-Signature` header and the endpoint's signing secret. The result is true or false; deliveries more than five minutes old are rejected unless you change the tolerance.

```js
import { verifyWebhook } from "@apidly/sdk";

const ok = verifyWebhook(rawBody, req.get("Apidly-Signature") ?? "", process.env.APIDLY_WEBHOOK_SECRET, 300);
```

```python
from apidly import verify_webhook

ok = verify_webhook(request.get_data(), request.headers.get("Apidly-Signature", ""), SECRET, 300)
```

It accepts either signature while a secret is being rotated. See [webhooks](/docs/webhooks) for the payload and delivery rules.

## Settings

`APIDLY_API_KEY` is the default key. The address of the service comes from the `baseUrl` option (`base_url` in Python), then `APIDLY_API_URL`, then the built-in default.

For a shell, see the [command-line tool](/docs/cli).
