from daytona import Daytona
from daytona.v0 import CreateSandboxFromSnapshotParams
from onecli import OneCLI
# OneCLI reads ONECLI_API_KEY, and Daytona reads DAYTONA_API_KEY.
with OneCLI() as onecli, Daytona() as daytona:
# 1. An agent on your Personal vault. It ends by itself after an hour,
# and leaving the block disconnects it.
with onecli.agents.create(
vault_name="Personal",
provider="daytona",
ttl_seconds=3600,
) as agent:
# 2. The sandbox: the agent's settings, and a network to OneCLI only.
sandbox = daytona.v0.create(
CreateSandboxFromSnapshotParams(
env_vars=agent.env,
domain_allow_list=",".join(agent.allow_hosts),
)
)
try:
# 3. Once: write the certificate files that agent.env points at.
setup = sandbox.process.exec(agent.setup_command)
if setup.exit_code != 0:
raise RuntimeError(f"setup failed: {setup.result}")
# httpbin.org/bearer answers 401 with no key.
# 200 means the gateway added the Test key.
call = sandbox.process.exec(
'curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/bearer'
)
print(call.result) # 200
finally:
sandbox.delete()