; goes through a shell. See Run a script.
Run a command
from gravixlayer import GravixLayer
client = GravixLayer() # defaults to cloud="aws", region="us-east-1"
sandbox = client.runtime.create() # defaults to template="base-small"
# Single-string form
result = sandbox.run_cmd(command="ls -la /workspace")
print(result.stdout)
print(result.exit_code)
# Equivalent command + args form
result = sandbox.run_cmd(command="ls", args=["-la", "/workspace"])
print(result.stdout)
sandbox.kill()
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
const sandbox = await client.runtime.create(); // defaults to template="base-small"
// Single-string form
const listed = await sandbox.runCmd('ls -la /workspace');
console.log(listed.stdout);
console.log(listed.exitCode);
const viaArgs = await sandbox.runCmd('ls', { args: ['-la', '/workspace'] });
console.log(viaArgs.stdout);
await sandbox.kill();
gravixlayer runtime exec "$RT" --workdir /workspace --timeout 300 -- ls -la /workspace
curl -sS -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/commands/run" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"command": "ls",
"args": ["-la", "/workspace"],
"working_dir": "/workspace",
"timeout": 300000
}'
CLI timeout is in seconds (
--timeout 300). The REST timeout field is in milliseconds (300000). The SDKs take seconds and convert for you.Install a package
Egress is deny-by-default, sopip install needs a network policy. This example uses allow_all for a short demo; prefer an allowlist for PyPI.
import uuid
from gravixlayer import GravixLayer
client = GravixLayer() # defaults to cloud="aws", region="us-east-1"
policy = client.network_policies.create(
name=f"pypi-{uuid.uuid4().hex[:8]}",
egress_mode="allow_all",
description="Temporary egress for pip",
)
sandbox = client.runtime.create(network_policy_ids=[policy.id]) # defaults to template="base-small"
sandbox.run_cmd(command="pip", args=["install", "pandas", "--quiet"])
result = sandbox.run_cmd(
command="python",
args=["-c", "import pandas; print(pandas.__version__)"],
)
print(result.stdout)
sandbox.kill()
client.network_policies.delete(policy.id)
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
const policy = await client.networkPolicies.create(`pypi-${Date.now()}`, {
egressMode: 'allow_all',
description: 'Temporary egress for pip',
});
const sandbox = await client.runtime.create({ networkPolicyIds: [policy.id] }); // defaults to template="base-small"
await sandbox.runCmd('pip', { args: ['install', 'pandas', '--quiet'] });
const result = await sandbox.runCmd('python', {
args: ['-c', 'import pandas; print(pandas.__version__)'],
});
console.log(result.stdout);
await sandbox.kill();
await client.networkPolicies.delete(policy.id);
gravixlayer runtime exec "$RT" -- pip install pandas --quiet
gravixlayer runtime exec "$RT" -- python -c 'import pandas; print(pandas.__version__)'
curl -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/commands/run" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"command": "pip",
"args": ["install", "pandas", "--quiet"]
}'
Run a script
from gravixlayer import GravixLayer
client = GravixLayer() # defaults to cloud="aws", region="us-east-1"
sandbox = client.runtime.create() # defaults to template="base-small"
# Single string — chained commands run in one shell invocation
result = sandbox.run_cmd(command="echo $HOME && python --version")
print(result.stdout)
# Equivalent with explicit shell invocation
result = sandbox.run_cmd(
command="sh",
args=["-lc", "echo $HOME && python --version"],
)
print(result.stdout)
sandbox.kill()
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
const sandbox = await client.runtime.create(); // defaults to template="base-small"
// Single string — chained commands run in one shell invocation
const chained = await sandbox.runCmd('echo $HOME && python --version');
console.log(chained.stdout);
const viaShell = await sandbox.runCmd('sh', { args: ['-lc', 'echo $HOME && python --version'] });
console.log(viaShell.stdout);
await sandbox.kill();
gravixlayer runtime exec "$RT" -- sh -lc 'echo $HOME && python --version'
curl -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/commands/run" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"command": "sh",
"args": ["-lc", "echo $HOME && python --version"]
}'
Set environment variables
Setenv_vars when you create the runtime so every command and code run can read them, or pass variables on one command:
from gravixlayer import GravixLayer
client = GravixLayer() # defaults to cloud="aws", region="us-east-1"
sandbox = client.runtime.create(env_vars={"APP_MODE": "production"}) # defaults to template="base-small"
result = sandbox.run_cmd(command="sh", args=["-lc", "echo APP_MODE=$APP_MODE"])
print(result.stdout) # APP_MODE=production
# Per-command environment (client API — not on the bound handle)
result = client.runtime.run_cmd(
sandbox.runtime_id,
command="sh",
args=["-lc", "echo $FOO"],
environment={"FOO": "bar"},
)
print(result.stdout)
sandbox.kill()
import { GravixLayer } from 'gravixlayer';
const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
const sandbox = await client.runtime.create({ envVars: { APP_MODE: 'production' } }); // defaults to template="base-small"
const result = await sandbox.runCmd('sh', { args: ['-lc', 'echo APP_MODE=$APP_MODE'] });
console.log(result.stdout); // APP_MODE=production
const perCommand = await sandbox.runCmd('sh', {
args: ['-lc', 'echo $FOO'],
environment: { FOO: 'bar' },
});
console.log(perCommand.stdout);
await sandbox.kill();
gravixlayer runtime exec "$RT" -e FOO=bar -- echo "$FOO"
curl -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/commands/run" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"command": "sh",
"args": ["-lc", "echo $FOO"],
"environment": {"FOO": "bar"}
}'
Stream the output
Passon_stdout or on_stderr to read output while the command runs. The full walkthrough is on Stream command output.
Run in the background
Setbackground to return as soon as the process is running, then wait or stop it with the pid. Attach, list, and stop are on Run in the background.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
command | string | Yes | Command to execute. May be a single shell string or a program name. |
args | list[string] | No | Additional arguments appended to command. |
working_dir | string | No | Working directory. Default /workspace. |
timeout | integer | No | Deadline. Seconds in the Python SDK and CLI, milliseconds on the REST API. Omitted or 0: 300 seconds for a foreground command, no deadline for a background command. |
environment | object | No | Per-command environment variables. |
background | boolean | No | Return as soon as the command is running. Default false. |
on_stdout | callable(str) | No | Per-chunk stdout callback. Enables streaming mode. |
on_stderr | callable(str) | No | Per-chunk stderr callback. Enables streaming mode. |
on_exit | callable(int) | No | Final exit-code callback. Enables streaming mode. |
on_error | callable(Exception) | No | With background, called if following the output fails. The command keeps running. |
Response
| Field | Type | Description |
|---|---|---|
stdout | string | Standard output (aggregated when streaming) |
stderr | string | Standard error (aggregated when streaming) |
exit_code | integer | Process exit code |
duration_ms | integer | Execution duration in milliseconds |
success | boolean | True when exit code is 0 |
timed_out | boolean | True when the deadline killed the command |
error | string | API-level error (if any) |
Endpoint
POST /v1/agents/runtime/{runtime_id}/commands/run
Add ?stream=true to stream output. Background attach and stop paths are on Run in the background.