# Shako SDK

> The Shako API in Python and JavaScript: a method for each endpoint, with an API key.

The reference of each endpoint, its fields and its answer: https://shako.dev/api.md.

## Install

Python 3.9 or newer, with no other packages. PyPI: https://pypi.org/project/shako-sdk/

```sh
pip install shako-sdk
```

JavaScript and TypeScript, Node.js 18 or newer, with no other packages. npm: https://www.npmjs.com/package/shako-sdk

```sh
npm install shako-sdk
```

## Quick start

```python Python
from shako_sdk import Shako

shako = Shako()  # the key from SHAKO_API_KEY

for vm in shako.vms.list():
    print(vm["name"], vm["state"])

shako.vms.create(name="web1", size="small")
print(shako.ports.create("web1", port=8000)["url"])
shako.vms.sleep("web1")
```

```javascript JavaScript
import { Shako } from 'shako-sdk';

const shako = new Shako(); // the key from SHAKO_API_KEY

for (const vm of await shako.vms.list()) {
  console.log(vm.name, vm.state);
}

await shako.vms.create({ name: 'web1', size: 'small' });
console.log((await shako.ports.create('web1', { port: 8000 })).url);
await shako.vms.sleep('web1');
```

## Rules

| Rule | Value |
|---|---|
| Methods | shako.<resource>.<endpoint> is the page /api/<resource>/<endpoint> of the API reference. |
| Args | The args of the path come first, in their order. Then the fields: keyword args in Python, one object in JavaScript. A field that you do not give is not sent. |
| Answers | A list returns its items. Each other answer is the object of the endpoint page. |
| Key | SHAKO_API_KEY, or api_key= in Python and { apiKey } in JavaScript. |
| Tenant | A key for one tenant needs none. A key for an account gives tenant= in Python and { tenant } in JavaScript, to the client or to one call. |
| Base URL | SHAKO_API, then https://api.shako.dev. |

## Methods

| Resource | Python | JavaScript | Endpoint |
|---|---|---|---|
| VMs | `shako.vms.create` | `shako.vms.create` | [Create a VM](https://shako.dev/api/vms/create.md) |
| VMs | `shako.vms.list` | `shako.vms.list` | [List VMs](https://shako.dev/api/vms/list.md) |
| VMs | `shako.vms.get` | `shako.vms.get` | [Get a VM](https://shako.dev/api/vms/get.md) |
| VMs | `shako.vms.update` | `shako.vms.update` | [Update a VM](https://shako.dev/api/vms/update.md) |
| VMs | `shako.vms.delete` | `shako.vms.delete` | [Delete a VM](https://shako.dev/api/vms/delete.md) |
| VMs | `shako.vms.sleep` | `shako.vms.sleep` | [Put a VM to sleep](https://shako.dev/api/vms/sleep.md) |
| VMs | `shako.vms.wake` | `shako.vms.wake` | [Wake a VM](https://shako.dev/api/vms/wake.md) |
| VMs | `shako.vms.restart` | `shako.vms.restart` | [Restart a VM](https://shako.dev/api/vms/restart.md) |
| VMs | `shako.vms.resize` | `shako.vms.resize` | [Resize a VM](https://shako.dev/api/vms/resize.md) |
| VMs | `shako.vms.rename` | `shako.vms.rename` | [Rename a VM](https://shako.dev/api/vms/rename.md) |
| VMs | `shako.vms.copy` | `shako.vms.copy` | [Copy a VM](https://shako.dev/api/vms/copy.md) |
| VMs | `shako.vms.logs` | `shako.vms.logs` | [Read the console](https://shako.dev/api/vms/logs.md) |
| VMs | `shako.vms.crons` | `shako.vms.crons` | [List the crons](https://shako.dev/api/vms/crons.md) |
| Ports | `shako.ports.list` | `shako.ports.list` | [List ports](https://shako.dev/api/ports/list.md) |
| Ports | `shako.ports.create` | `shako.ports.create` | [Share a port](https://shako.dev/api/ports/create.md) |
| Ports | `shako.ports.delete` | `shako.ports.delete` | [Stop sharing a port](https://shako.dev/api/ports/delete.md) |
| Backups | `shako.backups.list` | `shako.backups.list` | [List backups](https://shako.dev/api/backups/list.md) |
| Backups | `shako.backups.create` | `shako.backups.create` | [Back up now](https://shako.dev/api/backups/create.md) |
| Backups | `shako.backups.restore` | `shako.backups.restore` | [Restore a backup](https://shako.dev/api/backups/restore.md) |
| Backups | `shako.backups.delete` | `shako.backups.delete` | [Erase the backups](https://shako.dev/api/backups/delete.md) |
| Screen links | `shako.screen_links.create` | `shako.screenLinks.create` | [Create a screen link](https://shako.dev/api/screen-links/create.md) |
| Screen links | `shako.screen_links.delete` | `shako.screenLinks.delete` | [Stop the screen links](https://shako.dev/api/screen-links/delete.md) |
| Secrets | `shako.secrets.list` | `shako.secrets.list` | [List secrets](https://shako.dev/api/secrets/list.md) |
| Secrets | `shako.secrets.set` | `shako.secrets.set` | [Set a secret](https://shako.dev/api/secrets/set.md) |
| Secrets | `shako.secrets.delete` | `shako.secrets.delete` | [Delete a secret](https://shako.dev/api/secrets/delete.md) |
| Devices | `shako.devices.list` | `shako.devices.list` | [List devices](https://shako.dev/api/devices/list.md) |
| Devices | `shako.devices.create` | `shako.devices.create` | [Add a device](https://shako.dev/api/devices/create.md) |
| Devices | `shako.devices.delete` | `shako.devices.delete` | [Remove a device](https://shako.dev/api/devices/delete.md) |
| Agent memory | `shako.memory.get` | `shako.memory.get` | [Get the agent memory](https://shako.dev/api/memory/get.md) |
| Agent memory | `shako.memory.update` | `shako.memory.update` | [Update the agent memory](https://shako.dev/api/memory/update.md) |
| Billing | `shako.billing.usage` | `shako.billing.usage` | [Read the usage](https://shako.dev/api/billing/usage.md) |
| Billing | `shako.billing.credit` | `shako.billing.credit` | [Read the credit](https://shako.dev/api/billing/credit.md) |
| Billing | `shako.billing.top_up` | `shako.billing.topUp` | [Top up the credit](https://shako.dev/api/billing/top-up.md) |
| Billing | `shako.billing.invoice` | `shako.billing.invoice` | [Get an invoice](https://shako.dev/api/billing/invoice.md) |
| Access | `shako.access.list` | `shako.access.list` | [List access](https://shako.dev/api/access/list.md) |
| Access | `shako.access.revoke_vm` | `shako.access.revokeVm` | [Take a VM's access away](https://shako.dev/api/access/revoke-vm.md) |
| Tenants | `shako.tenants.list` | `shako.tenants.list` | [List tenants](https://shako.dev/api/tenants/list.md) |
| Tenants | `shako.tenants.create` | `shako.tenants.create` | [Create a tenant](https://shako.dev/api/tenants/create.md) |
| Tenants | `shako.tenants.delete` | `shako.tenants.delete` | [Delete a tenant](https://shako.dev/api/tenants/delete.md) |
| Caller | `shako.caller.whoami` | `shako.caller.whoami` | [Who am I](https://shako.dev/api/caller/whoami.md) |
| Caller | `shako.caller.ssh_keys` | `shako.caller.sshKeys` | [List SSH keys](https://shako.dev/api/caller/ssh-keys.md) |

## Errors

```python Python
from shako_sdk import ShakoError

try:
    shako.billing.top_up(amount=10)
except ShakoError as err:
    print(err.code, err.status, err.message)  # forbidden 403 API key ci needs the role owner on acme ...
```

```javascript JavaScript
import { ShakoError } from 'shako-sdk';

try {
  await shako.billing.topUp({ amount: 10 });
} catch (err) {
  if (!(err instanceof ShakoError)) throw err;
  console.log(err.code, err.status, err.message); // forbidden 403 API key ci needs the role owner on acme ...
}
```

An error answer raises ShakoError, with its code, its HTTP status and its message. No answer is the code unreachable, with the status 0. The codes: https://shako.dev/api/errors.md.
