Skip to main content
Grok Build is xAI’s coding agent and CLI. E2B provides a pre-built grok template with Grok Build already installed.

CLI

Create a sandbox with the E2B CLI.
e2b sbx create grok
Once inside the sandbox, start Grok Build.
grok

Run headless

Use -p for non-interactive mode and --always-approve to auto-approve all tool calls (safe inside E2B sandboxes). Grok Build authenticates with an API key from the xAI console via the XAI_API_KEY environment variable.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('grok', {
  envs: { XAI_API_KEY: process.env.XAI_API_KEY },
})

const result = await sandbox.commands.run(
  `grok --always-approve -p "Create a hello world HTTP server in Go"`
)

console.log(result.stdout)
await sandbox.kill()
import os
from e2b import Sandbox

sandbox = Sandbox.create("grok", envs={
    "XAI_API_KEY": os.environ["XAI_API_KEY"],
})

result = sandbox.commands.run(
    'grok --always-approve -p "Create a hello world HTTP server in Go"',
)

print(result.stdout)
sandbox.kill()

Example: work on a cloned repository

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('grok', {
  envs: { XAI_API_KEY: process.env.XAI_API_KEY },
  timeoutMs: 600_000,
})

await sandbox.git.clone('https://github.com/your-org/your-repo.git', {
  path: '/home/user/repo',
  username: 'x-access-token',
  password: process.env.GITHUB_TOKEN,
  depth: 1,
})

const result = await sandbox.commands.run(
  `cd /home/user/repo && grok --always-approve -p "Add error handling to all API endpoints"`,
  { onStdout: (data) => process.stdout.write(data) }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)

await sandbox.kill()
import os
from e2b import Sandbox

sandbox = Sandbox.create("grok", envs={
    "XAI_API_KEY": os.environ["XAI_API_KEY"],
}, timeout=600)

sandbox.git.clone("https://github.com/your-org/your-repo.git",
    path="/home/user/repo",
    username="x-access-token",
    password=os.environ["GITHUB_TOKEN"],
    depth=1,
)

result = sandbox.commands.run(
    'cd /home/user/repo && grok --always-approve -p "Add error handling to all API endpoints"',
    on_stdout=lambda data: print(data, end=""),
)

diff = sandbox.commands.run("cd /home/user/repo && git diff")
print(diff.stdout)

sandbox.kill()

Build a custom template

If you need to customize the environment (e.g. pre-install dependencies, add config files), build your own template on top of the pre-built grok template.
// template.ts
import { Template } from 'e2b'

export const template = Template()
  .fromTemplate('grok')
# template.py
from e2b import Template

template = (
    Template()
    .from_template("grok")
)
// build.ts
import { Template, defaultBuildLogger } from 'e2b'
import { template as grokTemplate } from './template'

await Template.build(grokTemplate, 'my-grok', {
  cpuCount: 2,
  memoryMB: 2048,
  onBuildLogs: defaultBuildLogger(),
})
# build.py
from e2b import Template, default_build_logger
from template import template as grok_template

Template.build(grok_template, "my-grok",
    cpu_count=2,
    memory_mb=2048,
    on_build_logs=default_build_logger(),
)
Run the build script to create the template.
npx tsx build.ts
python build.py

Sandbox persistence

Auto-pause, resume, and manage sandbox lifecycle

Git integration

Clone repos, manage branches, and push changes

SSH access

Connect to the sandbox via SSH for interactive sessions