# Connect Python

> Let your agent do it: Connect a Python program to Hermitbox. Install the `hermitbox` SDK from the release URL the page gives, in the program's Python environment, and 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 example with the same interpreter: it uses the install's default tier, adds an image only for containers, prints hello and deletes its sandbox. Show me the output. Follow the exact steps in https://docs.hermitbox.dev/v0.2.5/get-started/python.md.

Create a sandbox, run a command, and delete it from Python with the Hermitbox SDK.

1. Install the SDK from the release in your application's Python environment, on Python 3.12 or
   later: `pip install https://github.com/hermitbox/hermitbox/releases/download/v0.2.5/hermitbox-0.2.5-py3-none-any.whl`.
2. Set `HERMITBOX_API_URL` and `HERMITBOX_API_KEY` in that process's private environment.
3. Run this with the same Python interpreter.

```python
from hermitbox import Hermitbox

# Connect with HERMITBOX_API_URL and HERMITBOX_API_KEY from the environment.
with Hermitbox() as hb:
    # Use the install's default tier. A container also needs an image.
    isolation = hb.project()['defaults']['isolation']
    image = {'image': 'alpine:3.22'} if isolation == 'container' else {}
    box = hb.sandbox(isolation=isolation, **image)

    try:
        # Run a command and stop if it failed.
        result = box.run('echo hello')
        if result.code != 0:
            raise RuntimeError(result.stderr)
        print(result.stdout)  # hello
    finally:
        # Delete the sandbox, even when something failed.
        box.delete()
```

This names the reported default tier and adds an image only for containers. An unavailable
named tier is refused. See [tier limits](https://docs.hermitbox.dev/v0.2.5/reference/tiers.md) and the
[sync and async SDK reference](https://docs.hermitbox.dev/v0.2.5/sdk/python.md).
