Skip to main content
The claude-code template supports ANTHROPIC_API_KEY.

Run Claude Code

Create a sandbox from the claude-code template, run Claude Code headlessly, and clean up. The SDK and CLI paths are equivalent — use the one that matches your workflow.
import os
from nullspace import Sandbox

with Sandbox.create(
    template="claude-code",
    envs={"ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"]},
    timeout=600,
) as sandbox:
    result = sandbox.commands.run(
        'claude --dangerously-skip-permissions -p "Create a hello world HTTP server in Go"',
        shell=True,
        cwd="/workspace",
        timeout=600,
    )
    print(result.stdout)

Interactive terminal

Open a PTY instead of a headless run to drive Claude Code interactively:
nullspace sandbox pty create sb_123 --cols 120 --rows 30
nullspace sandbox pty connect sb_123 --session-id ses_123
# then, inside the PTY:
claude
Use the session_id from sandbox pty create with --session-id.

Work on a cloned repository

import os
from nullspace import GitHttpsAuth, Sandbox

with Sandbox.create(
    template="claude-code",
    envs={"ANTHROPIC_API_KEY": os.environ["ANTHROPIC_API_KEY"]},
    timeout=600,
) as sandbox:
    sandbox.git.clone(
        "https://github.com/your-org/your-repo.git",
        path="/workspace/repo",
        auth=GitHttpsAuth(username="x-access-token", password=os.environ["GITHUB_TOKEN"]),
        depth=1,
    )
    sandbox.commands.run(
        'claude --dangerously-skip-permissions -p "Find and fix all TODO comments"',
        shell=True,
        cwd="/workspace/repo",
        timeout=600,
        on_stdout=lambda data: print(data, end=""),
    )
    print(sandbox.git.diff(path="/workspace/repo"))

Structured and streaming output

result = sandbox.commands.run(
    'claude --dangerously-skip-permissions --output-format json -p "Review this codebase and return JSON"',
    shell=True,
    cwd="/workspace/repo",
    timeout=600,
)
print(result.stdout)

sandbox.commands.run(
    'claude --dangerously-skip-permissions --output-format stream-json -p "Find risks"',
    shell=True,
    cwd="/workspace/repo",
    timeout=600,
    on_stdout=lambda data: print(data, end=""),
)

Resume a session

Claude Code persists conversations in its own CLI state inside the sandbox. Use the CLI’s session ID with --resume when you want follow-up work in the same sandbox session. Nullspace run IDs are not conversation session IDs. Framework session, thread, checkpoint, or flow state stays app-owned. For Claude Code, store session files in a volume or app-managed storage when they must survive sandbox cleanup. Use volumes, retained sandboxes, hibernate/auto-resume, or an external database for state that must outlive one disposable sandbox.