Use this file to discover all available pages before exploring further.
CodeBox is BoxLite’s purpose-built sandbox for running untrusted Python code. It auto-installs missing packages, captures output, and keeps your host system completely safe. This tutorial walks through the core workflow.
CodeBox detects missing imports and installs them automatically. No manual pip install needed.
Python
Node.js
auto_install.py
import asyncioimport boxliteasync def main(): async with boxlite.CodeBox() as codebox: # requests is not pre-installed — CodeBox installs it for you result = await codebox.run("""import requestsresponse = requests.get('https://api.github.com/zen')print(response.text)""") print(result)if __name__ == "__main__": asyncio.run(main())
Auto-install is a Python SDK feature. In Node.js, use installPackage() before running code that needs third-party packages.
Use run_script() to execute a Python script from your host filesystem inside the box.
run_script() reads the script from your host machine and executes it inside the box. It does not look for files already inside the guest.
Python
Node.js
analysis.py
# This file lives on the HOST at ./analysis.pyimport jsondata = {"status": "success", "items": [1, 2, 3]}print(json.dumps(data, indent=2))
run_script.py
import asyncioimport boxliteasync def main(): async with boxlite.CodeBox() as codebox: # run_script() reads ./analysis.py from the HOST filesystem # and executes it inside the box result = await codebox.run_script("./analysis.py") print(result)if __name__ == "__main__": asyncio.run(main())
A single CodeBox can run many snippets sequentially. Filesystem state persists between calls — installed packages and written files remain available. However, in-memory state (variables, functions) does not persist.
Each run() call spawns a separate Python process. Variables and functions defined in one run() call are not available in the next. Use the filesystem to share data between calls.
Python
Node.js
multi_run.py
import asyncioimport boxliteasync def main(): async with boxlite.CodeBox() as codebox: # First run: compute results and write to a file await codebox.run("""import jsondata = {"a": 1, "b": 2, "c": 3}output = {k: v * 2 for k, v in data.items()}with open('/tmp/results.json', 'w') as f: json.dump(output, f)print("Saved results to /tmp/results.json")""") # Second run: read the file from the previous run result = await codebox.run("""import jsonwith open('/tmp/results.json') as f: data = json.load(f)print(json.dumps(data))""") print(result) # {"a": 2, "b": 4, "c": 6}if __name__ == "__main__": asyncio.run(main())
multi_run.js
import { CodeBox } from '@boxlite-ai/boxlite';async function main() { const codebox = new CodeBox({ memoryMib: 512 }); try { // First run: compute results and write to a file await codebox.run(`import jsondata = {"a": 1, "b": 2, "c": 3}output = {k: v * 2 for k, v in data.items()}with open('/tmp/results.json', 'w') as f: json.dump(output, f)print("Saved results to /tmp/results.json") `); // Second run: read the file from the previous run const result = await codebox.run(`import jsonwith open('/tmp/results.json') as f: data = json.load(f)print(json.dumps(data)) `); console.log(result); // {"a": 2, "b": 4, "c": 6} } finally { await codebox.stop(); }}main();