run_code calls.
POST /v1/agents/runtime/{runtime_id}/code/contexts
from gravixlayer import GravixLayer
client = GravixLayer() # defaults to cloud="aws", region="us-east-1"
sandbox = client.runtime.create() # defaults to template="base-small"
ctx = client.runtime.create_context(sandbox.runtime_id)
print(ctx.context_id)
print(ctx.language) # python
print(ctx.cwd) # /workspace
client.runtime.run_code(sandbox.runtime_id, code="counter = 1", context_id=ctx.context_id)
result = client.runtime.run_code(sandbox.runtime_id, code="counter += 1\nprint(counter)", context_id=ctx.context_id)
print(result.text) # 2
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"
const ctx = await sandbox.createContext();
console.log(ctx.contextId);
console.log(ctx.language); // python
console.log(ctx.cwd); // /workspace
await sandbox.runCode('counter = 1', { contextId: ctx.contextId });
const result = await sandbox.runCode('counter += 1\nprint(counter)', { contextId: ctx.contextId });
console.log(result.text); // 2
await sandbox.kill();
CTX=$(gravixlayer runtime code-context create "$RT" --language python --output json | jq -r '.context_id')
echo "$CTX"
# Optional working directory:
# gravixlayer runtime code-context create "$RT" --language python --cwd /workspace
# Context-bound run_code: use Python or cURL (runtime run has no --context-id).
CTX=$(curl -sS -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/code/contexts" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
-H "Content-Type: application/json" \
-d '{"language": "python", "cwd": "/workspace"}' | jq -r '.id // .context_id')
echo "$CTX"
curl -sS -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/code/run" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"code\": \"counter = 1\", \"language\": \"python\", \"context_id\": \"$CTX\"}" | jq .
curl -sS -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/code/run" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
-H "Content-Type: application/json" \
-d "{\"code\": \"counter += 1\\nprint(counter)\", \"language\": \"python\", \"context_id\": \"$CTX\"}" | jq .
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
runtime_id | string | Yes | Runtime identifier |
language | string | No | Context language. Default: python |
cwd | string | No | Working directory. Default: /workspace |
Response
| Field | Type | Description |
|---|---|---|
context_id | string | Context identifier (pass to run_code) |
language | string | Language of the context |
cwd | string | Working directory |