# Connect a Python program

> Let your agent do it: Connect a Python program to Hermitbox. Install the `hermitbox` package from PyPI in the program's Python environment. On this machine `Hermitbox()` reads the saved credentials automatically; on another machine, set `HERMITBOX_API_URL` and `HERMITBOX_API_KEY` in that process's private environment, never in a file or this chat. Run the page's quickstart and agent-code examples with the same interpreter and show me their output. Follow the exact steps in https://docs.hermitbox.dev/v0.3.2/get-started/python.md.

Give your Python program sandboxes: create one, run a command in it and delete it with the
Hermitbox SDK.

Install the SDK from PyPI, on Python 3.12 or later:

```sh
pip install hermitbox
```

## Quickstart

```python
from hermitbox import Hermitbox

with Hermitbox() as hb:
    box = hb.sandbox()
    print(box.run('echo hello').stdout.strip())
    box.delete()
```

On the machine where you installed Hermitbox, `Hermitbox()` connects automatically. From another
machine, set `HERMITBOX_API_URL` and `HERMITBOX_API_KEY` in the process's private environment.

## Run code an agent wrote

Run code a model wrote in a sandbox instead of your own process. Write the code and its inputs
into the sandbox, run it there, read the result back and delete the sandbox:

```python
from hermitbox import Hermitbox

code = """
import json, sys
rows = json.load(open(sys.argv[1]))
total = sum(row['amount'] for row in rows)
open(sys.argv[2], 'w').write(f'total={total}\\n')
"""

with Hermitbox() as hb:
    box = hb.sandbox()
    try:
        box.write('orders.json', '[{"amount": 12}, {"amount": 30}]')
        box.write('analyze.py', code)
        result = box.run('python3 ./analyze.py "$PWD/orders.json" "$PWD/summary.txt"')
        if result.code != 0:
            raise RuntimeError(result.stderr)
        print(box.read('summary.txt').strip())
    finally:
        box.delete()
```

Output:

```txt
total=42
```

See [tier limits](https://docs.hermitbox.dev/v0.3.2/reference/tiers.md) and the
[sync and async SDK reference](https://docs.hermitbox.dev/v0.3.2/sdk/python.md).
