> ## Documentation Index
> Fetch the complete documentation index at: https://www.agent37.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK

> A one-file Python client that creates sandboxes and runs commands in them.

The Agent37 Python SDK is one file built on `requests`, with no package to install. Save it as `agent37.py` next to your code and set `AGENT37_API_KEY` to a key from the [dashboard](https://www.agent37.com/dashboard/cloud/api-keys).

```python agent37.py theme={null}
import os

import requests

API = "https://api.agent37.com/v1"


class Sandbox:
    def __init__(self, id, session):
        self.id = id
        self._session = session

    @classmethod
    def create(cls, api_key=None, **params):
        session = requests.Session()
        session.headers["Authorization"] = f"Bearer {api_key or os.environ['AGENT37_API_KEY']}"
        response = session.post(f"{API}/instances", json=params or None, timeout=120)
        response.raise_for_status()
        return cls(response.json()["id"], session)

    def exec(self, command, user=None):
        body = {"command": command}
        if user:
            body["user"] = user
        response = self._session.post(f"{API}/instances/{self.id}/exec", json=body, timeout=900)
        response.raise_for_status()
        return response.json()

    def delete(self):
        self._session.delete(f"{API}/instances/{self.id}", timeout=120).raise_for_status()

    def __enter__(self):
        return self

    def __exit__(self, *exc):
        self.delete()
```

## Run code in a sandbox

```python theme={null}
from agent37 import Sandbox

with Sandbox.create() as sandbox:
    sandbox.exec("pip install requests")
    result = sandbox.exec("python3 -c 'import requests; print(requests.__version__)'")
    print(result["stdout"])
```

`Sandbox.create()` starts a sandbox from the default template, a Linux machine with Python, Node, git and build tools. Keyword arguments become the [create request](https://www.agent37.com/docs/agents-api/instances/create.md) body, such as `Sandbox.create(template="agent37-ubuntu", type="vm")`.

`exec` runs a shell command inside the sandbox and returns `exit_code`, `stdout`, `stderr` and `truncated`. Pass `user="root"` to run as root. Files persist between calls, so install packages in one call and use them in the next. See [Run commands](https://www.agent37.com/docs/agents-api/exec.md) for limits and background processes.

Leaving the `with` block deletes the sandbox, which removes its data and ends billing. Call `sandbox.delete()` yourself when you create one outside a `with` block.

The SDK covers the calls most scripts need. Every other operation is a plain HTTPS request in the [API reference](https://www.agent37.com/docs/agents-api/overview.md), sent with the same `Authorization: Bearer` header.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.