---
title: Python client
description: Use pols from Python with the typed client, generated from the same OpenAPI spec as the API, with helpers for waiting, streaming, detached processes and webhooks.
---

`polsso` is a typed Python client for the pols API, imported as `pols`. Its types are generated from the API's [OpenAPI spec](/docs/api/), and its tests check that it wraps every operation there; on top of that it adds helpers for waiting, streaming, detached processes, file watches and checking webhook signatures. It needs Python 3.10 or later and depends on `httpx` and `typing-extensions`.

## Install

The client is not published on PyPI yet; once it is, it installs with `pip install polsso`. Until then it is installed from a checkout of the pols source repository, which is not public:

```sh
pip install ./clients/python
```

If you need it before it is published, write to team@peweo.com. (The PyPI name `pols` cannot be registered, as PyPI treats it as too similar to an existing project; the package is `polsso` and the module `pols`.)

## Create, run, clean up

Create an API key on the [Agents page](https://my.pols.so/account/agents), or let your agent get one by email (below).

```python
from pols import Pols

with Pols(api_key="pols_...") as pols:
    sb = pols.sandboxes.create(size="small", ttl_seconds=3600)
    sb = pols.sandboxes.wait_for(sb)  # until status == desired_state
    result = pols.exec(sb["id"], ["bash", "-lc", "uname -a"])
    print(result["exit_code"], result["stdout"])
    pols.sandboxes.delete(sb["id"])
```

Responses are plain dicts typed with the `TypedDict`s in `pols` (`Sandbox`, `ExecResult`, ...), so type checkers know their keys. Wherever a call takes a sandbox or template, its ID or name works.

`Pols(api_key, base_url="https://api.pols.so")` refuses a plain `http://` base URL except to this machine, so the key does not travel in cleartext; pass `allow_insecure_http=True` to allow it. Pass `http_client=httpx.Client(...)` for proxies or tests.

### Getting a key by email

An agent without a key gets one with its person's approval, as with [`pols signup --email`](/docs/account/orgs/#signing-up-from-the-cli-or-an-agent):

```python
from pols import AgentLogins, Pols

with AgentLogins() as logins:
    login = logins.start("ada@example.com", client_name="my agent")
    print("Approve the email from pols.so; it shows the code", login["user_code"])
    key = logins.wait(login)  # polls until approved, for up to 15 minutes
pols = Pols(api_key=key["api_key"])
```

## Waiting for lifecycle changes

Create, stop, resume, fork, resize, rollback and delete return at once, and pols then drives the sandbox to its desired state (see [Lifecycle](/docs/sandboxes/lifecycle/)). `sandboxes.wait_for` polls until it gets there, and raises `PolsError` with code `sandbox_error` if the sandbox ends in status `error`, `resize_failed` or `rollback_failed` if a resize or [rollback](/docs/sandboxes/checkpoints/) could not be done, or `timeout` after `timeout` seconds (default 300):

```python
sb = pols.sandboxes.wait_for(pols.sandboxes.stop("web"), timeout=120, interval=0.5)
```

`sandboxes.create`, `sandboxes.fork`, `templates.create` and `templates.build` take `idempotency_key`, so a retry after a lost answer does not make a second one; see [Idempotency keys](/docs/sandboxes/idempotency/).

## Commands and processes

`exec` waits for the command and returns its collected output, at most 4 MiB per stream. `exec_stream` yields events as the command produces output, ending with an `exit` or `error` event:

```python
for event in pols.exec_stream("web", ["bash", "-lc", "make test"], timeout_seconds=1800):
    if event["type"] in ("stdout", "stderr"):
        print(event["data"], end="")
    elif event["type"] == "exit":
        print("exit code", event["exit_code"])
```

Leaving the loop early ends the call, which kills the command. A non-zero exit code is not an exception. For commands that should outlive the call, start a [detached process](/docs/sandboxes/processes/) with `processes.start` and read it with `processes.output`.

## Files, computer use and more

```python
pols.files.write("web", "/home/user/app.py", "print('hi')\n", mode="0755")
data = pols.files.read("web", "/home/user/app.py")  # bytes
entries = pols.files.list("web", "/home/user")["entries"]
pols.files.track("web", "/home/user")  # later: what changed since
changes = pols.files.changes("web", "/home/user")["changes"]

png = pols.computer.screenshot("web")  # 1920x1080 PNG
pols.computer.click("web", 960, 540)
pols.computer.type("web", "hello")
cdp = pols.browser.cdp("web")  # Playwright: chromium.connect_over_cdp(cdp["websocket_url"])
```

## What the client covers

| Area | Calls |
| --- | --- |
| Sandboxes | `pols.sandboxes.list`, `.get`, `.create`, `.fork`, `.rename`, `.resize`, `.set_egress`, `.stop`, `.resume`, `.rollback`, `.delete`, `.stats`, `.events`, `.event_thumbnail`, `.set_timeline_capture`, `.wait_for` |
| Commands | `pols.exec`, `pols.exec_stream`; `pols.processes.start`, `.list`, `.output`, `.signal` |
| Files | `pols.files.read`, `.write`, `.list`, `.watch`, `.track`, `.changes`, `.untrack` |
| Users | `pols.users.list`, `.add`, `.remove` |
| Checkpoints | `pols.checkpoints.list`, `.create`, `.delete` |
| Templates | `pols.templates.list`, `.get`, `.create`, `.build`, `.delete` |
| Databases | `pols.databases.list`, `.get`, `.create`, `.delete`, `.backups`, `.link`, `.unlink`, `.rotate` |
| Computer use | `pols.computer.screenshot`, `.click`, `.double_click`, `.drag`, `.type`, `.key`, `.scroll`, `.action`, `.control`; `pols.browser.cdp` |
| Sites | `pols.sites.list`, `.get`, `.create`, `.update`, `.delete`, `.publish`, `.versions`, `.restore`, `.rotate_link`, `.redirects`, `.set_redirects`, `.domains` |
| Access | `pols.ports.list`, `.publish`, `.unpublish`, `.link`, `.domains`; `pols.desktop.link`; `pols.ssh_keys.list`, `.create`, `.delete`; `pols.ssh` |
| Org | `pols.org`, `pols.orgs`, `pols.org_stats`, `pols.usage`, `pols.balance`, `pols.vault.list`, `pols.api_keys.list`, `pols.webhooks.list`, `.deliveries`, `pols.billing` |

The exec, process, file write, file list/watch/track, computer-use, desktop, CDP and SSH calls take `user=` to act as a [sandbox user](/docs/reference/cli/users/). For anything without a helper, `pols.request(method, path, ...)` sends a request with the API key.

## Errors

A non-2xx answer raises `PolsError` with the HTTP `status`, the API's stable `code` and its `message`. See [Errors](/docs/reference/errors/) for what each code means.

```python
from pols import PolsError

try:
    pols.sandboxes.create()
except PolsError as e:
    if e.code == "quota_exceeded":
        ...
```

## Verifying webhooks

`webhooks.verify`, from the `pols` package, checks a [webhook](/docs/account/webhooks/) delivery's `Pols-Signature` in constant time, refuses timestamps more than 5 minutes from your clock, and only then parses the body. Give it the raw body bytes as received:

```python
from pols import webhooks

def handle(body: bytes, headers) -> int:
    try:
        event = webhooks.verify(body, headers.get(webhooks.SIGNATURE_HEADER), SECRET)
    except webhooks.WebhookVerificationError:
        return 400
    print(event["type"], event["data"]["sandbox"]["id"])
    return 200
```

With Flask the raw body is `request.get_data()`, with FastAPI or Starlette `await request.body()`, with Django `request.body`. `webhooks.sign(body, secret, timestamp)` returns the header value pols would send, for testing your receiver.
