Create a context
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
# Run code inside the context — state persists between calls
gravixlayer runtime run "$RT" --code 'counter = 1' --context-id "$CTX"
gravixlayer runtime run "$RT" --code 'counter += 1; print(counter)' --context-id "$CTX"
# → 2
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 |
Check a context
Read the language and working directory of a context you already created.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)
context = client.runtime.get_context(sandbox.runtime_id, ctx.context_id)
print(context.context_id)
print(context.language) # python
print(context.cwd) # /workspace
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();
const context = await sandbox.getContext(ctx.contextId);
console.log(context.contextId);
console.log(context.language); // python
console.log(context.cwd); // /workspace
await sandbox.kill();
CTX=$(gravixlayer runtime code-context create "$RT" --language python --output json | jq -r '.context_id')
gravixlayer runtime code-context get "$RT" "$CTX" --output json | jq .
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"}' | jq -r '.id // .context_id')
curl -sS "https://api.gravixlayer.ai/v1/agents/runtime/$RT/code/contexts/$CTX" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" | jq .
Delete a context
Delete a context when you want the next run to start clean. Later runs with that id fail.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)
response = client.runtime.delete_context(sandbox.runtime_id, ctx.context_id)
print(response.message)
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();
const response = await sandbox.deleteContext(ctx.contextId);
console.log(response.message);
await sandbox.kill();
CTX=$(gravixlayer runtime code-context create "$RT" --language python --output json | jq -r '.context_id')
gravixlayer runtime code-context delete "$RT" "$CTX" --output json | jq .
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"}' | jq -r '.id // .context_id')
curl -sS -X DELETE "https://api.gravixlayer.ai/v1/agents/runtime/$RT/code/contexts/$CTX" \
-H "Authorization: Bearer $GRAVIXLAYER_API_KEY" | jq .
Endpoints
| Action | Method and path |
|---|---|
| Create a context | POST /v1/agents/runtime/{runtime_id}/code/contexts |
| Check a context | GET /v1/agents/runtime/{runtime_id}/code/contexts/{context_id} |
| Delete a context | DELETE /v1/agents/runtime/{runtime_id}/code/contexts/{context_id} |