|
| 1 | +--- |
| 2 | +title: "Grok Build" |
| 3 | +description: "Run Grok Build in a secure E2B sandbox with full filesystem, terminal, and git access." |
| 4 | +icon: "/images/icons/grok.svg" |
| 5 | +--- |
| 6 | + |
| 7 | +[Grok Build](https://x.ai/cli) is xAI's coding agent and CLI. E2B provides a pre-built `grok` template with Grok Build already installed. |
| 8 | + |
| 9 | +## CLI |
| 10 | + |
| 11 | +Create a sandbox with the [E2B CLI](/docs/cli). |
| 12 | + |
| 13 | +```bash |
| 14 | +e2b sbx create grok |
| 15 | +``` |
| 16 | + |
| 17 | +Once inside the sandbox, start Grok Build. |
| 18 | + |
| 19 | +```bash |
| 20 | +grok |
| 21 | +``` |
| 22 | + |
| 23 | +## Run headless |
| 24 | + |
| 25 | +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](https://console.x.ai) via the `XAI_API_KEY` environment variable. |
| 26 | + |
| 27 | +<CodeGroup> |
| 28 | +```typescript JavaScript & TypeScript |
| 29 | +import { Sandbox } from 'e2b' |
| 30 | + |
| 31 | +const sandbox = await Sandbox.create('grok', { |
| 32 | + envs: { XAI_API_KEY: process.env.XAI_API_KEY }, |
| 33 | +}) |
| 34 | + |
| 35 | +const result = await sandbox.commands.run( |
| 36 | + `grok --always-approve -p "Create a hello world HTTP server in Go"` |
| 37 | +) |
| 38 | + |
| 39 | +console.log(result.stdout) |
| 40 | +await sandbox.kill() |
| 41 | +``` |
| 42 | +```python Python |
| 43 | +import os |
| 44 | +from e2b import Sandbox |
| 45 | + |
| 46 | +sandbox = Sandbox.create("grok", envs={ |
| 47 | + "XAI_API_KEY": os.environ["XAI_API_KEY"], |
| 48 | +}) |
| 49 | + |
| 50 | +result = sandbox.commands.run( |
| 51 | + 'grok --always-approve -p "Create a hello world HTTP server in Go"', |
| 52 | +) |
| 53 | + |
| 54 | +print(result.stdout) |
| 55 | +sandbox.kill() |
| 56 | +``` |
| 57 | +</CodeGroup> |
| 58 | + |
| 59 | +### Example: work on a cloned repository |
| 60 | + |
| 61 | +<CodeGroup> |
| 62 | +```typescript JavaScript & TypeScript |
| 63 | +import { Sandbox } from 'e2b' |
| 64 | + |
| 65 | +const sandbox = await Sandbox.create('grok', { |
| 66 | + envs: { XAI_API_KEY: process.env.XAI_API_KEY }, |
| 67 | + timeoutMs: 600_000, |
| 68 | +}) |
| 69 | + |
| 70 | +await sandbox.git.clone('https://github.com/your-org/your-repo.git', { |
| 71 | + path: '/home/user/repo', |
| 72 | + username: 'x-access-token', |
| 73 | + password: process.env.GITHUB_TOKEN, |
| 74 | + depth: 1, |
| 75 | +}) |
| 76 | + |
| 77 | +const result = await sandbox.commands.run( |
| 78 | + `cd /home/user/repo && grok --always-approve -p "Add error handling to all API endpoints"`, |
| 79 | + { onStdout: (data) => process.stdout.write(data) } |
| 80 | +) |
| 81 | + |
| 82 | +const diff = await sandbox.commands.run('cd /home/user/repo && git diff') |
| 83 | +console.log(diff.stdout) |
| 84 | + |
| 85 | +await sandbox.kill() |
| 86 | +``` |
| 87 | +```python Python |
| 88 | +import os |
| 89 | +from e2b import Sandbox |
| 90 | + |
| 91 | +sandbox = Sandbox.create("grok", envs={ |
| 92 | + "XAI_API_KEY": os.environ["XAI_API_KEY"], |
| 93 | +}, timeout=600) |
| 94 | + |
| 95 | +sandbox.git.clone("https://github.com/your-org/your-repo.git", |
| 96 | + path="/home/user/repo", |
| 97 | + username="x-access-token", |
| 98 | + password=os.environ["GITHUB_TOKEN"], |
| 99 | + depth=1, |
| 100 | +) |
| 101 | + |
| 102 | +result = sandbox.commands.run( |
| 103 | + 'cd /home/user/repo && grok --always-approve -p "Add error handling to all API endpoints"', |
| 104 | + on_stdout=lambda data: print(data, end=""), |
| 105 | +) |
| 106 | + |
| 107 | +diff = sandbox.commands.run("cd /home/user/repo && git diff") |
| 108 | +print(diff.stdout) |
| 109 | + |
| 110 | +sandbox.kill() |
| 111 | +``` |
| 112 | +</CodeGroup> |
| 113 | + |
| 114 | +## Build a custom template |
| 115 | + |
| 116 | +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. |
| 117 | + |
| 118 | +<CodeGroup> |
| 119 | +```typescript JavaScript & TypeScript |
| 120 | +// template.ts |
| 121 | +import { Template } from 'e2b' |
| 122 | + |
| 123 | +export const template = Template() |
| 124 | + .fromTemplate('grok') |
| 125 | +``` |
| 126 | +```python Python |
| 127 | +# template.py |
| 128 | +from e2b import Template |
| 129 | + |
| 130 | +template = ( |
| 131 | + Template() |
| 132 | + .from_template("grok") |
| 133 | +) |
| 134 | +``` |
| 135 | +</CodeGroup> |
| 136 | + |
| 137 | +<CodeGroup> |
| 138 | +```typescript JavaScript & TypeScript |
| 139 | +// build.ts |
| 140 | +import { Template, defaultBuildLogger } from 'e2b' |
| 141 | +import { template as grokTemplate } from './template' |
| 142 | + |
| 143 | +await Template.build(grokTemplate, 'my-grok', { |
| 144 | + cpuCount: 2, |
| 145 | + memoryMB: 2048, |
| 146 | + onBuildLogs: defaultBuildLogger(), |
| 147 | +}) |
| 148 | +``` |
| 149 | +```python Python |
| 150 | +# build.py |
| 151 | +from e2b import Template, default_build_logger |
| 152 | +from template import template as grok_template |
| 153 | + |
| 154 | +Template.build(grok_template, "my-grok", |
| 155 | + cpu_count=2, |
| 156 | + memory_mb=2048, |
| 157 | + on_build_logs=default_build_logger(), |
| 158 | +) |
| 159 | +``` |
| 160 | +</CodeGroup> |
| 161 | + |
| 162 | +Run the build script to create the template. |
| 163 | + |
| 164 | +<CodeGroup> |
| 165 | +```bash JavaScript & TypeScript |
| 166 | +npx tsx build.ts |
| 167 | +``` |
| 168 | +```bash Python |
| 169 | +python build.py |
| 170 | +``` |
| 171 | +</CodeGroup> |
| 172 | + |
| 173 | +## Related guides |
| 174 | + |
| 175 | +<CardGroup cols={3}> |
| 176 | + <Card title="Sandbox persistence" icon="clock" href="/docs/sandbox/persistence"> |
| 177 | + Auto-pause, resume, and manage sandbox lifecycle |
| 178 | + </Card> |
| 179 | + <Card title="Git integration" icon="code-branch" href="/docs/sandbox/git-integration"> |
| 180 | + Clone repos, manage branches, and push changes |
| 181 | + </Card> |
| 182 | + <Card title="SSH access" icon="terminal" href="/docs/sandbox/ssh-access"> |
| 183 | + Connect to the sandbox via SSH for interactive sessions |
| 184 | + </Card> |
| 185 | +</CardGroup> |
0 commit comments