from onecli import OneCLI
from vercel.sandbox import NetworkPolicy
from vercel.sandbox.sync import create_sandbox
with OneCLI() as onecli: # reads ONECLI_API_KEY
# 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="vercel",
ttl_seconds=3600,
) as agent:
# 2. The sandbox: the agent's settings, and a network to OneCLI only.
# It reads VERCEL_OIDC_TOKEN, or VERCEL_TOKEN, VERCEL_TEAM_ID and
# VERCEL_PROJECT_ID. Leaving the block stops and deletes it.
with create_sandbox(
env=agent.env,
network_policy=NetworkPolicy.custom(
allow={host: () for host in agent.allow_hosts}
),
execution_time_limit=45 * 60, # the most on the Hobby plan
) as sandbox:
# 3. Once: write the certificate files that agent.env points at.
# check=True raises when it fails.
sandbox.run_command(
"sh", ["-c", agent.setup_command], capture_output=True, check=True
)
# httpbin.org/bearer answers 401 with no key.
# 200 means the gateway added the Test key.
sandbox.run_command(
"curl",
[
"-s",
"-o",
"/dev/null",
"-w",
"%{http_code}\n",
"https://httpbin.org/bearer",
],
) # prints 200