Skip to main content
The write_file() method allows you to create or overwrite files inside your sandbox’s isolated filesystem.
It supports both text and binary content, making it flexible for handling scripts, configuration files, and assets such as images, JSON files, or compiled binaries.
All file paths must begin with the absolute sandbox path /home/damner.

async write_file(path: str, content: Union[str, bytes]) -> None

Description:
Writes a file to the sandbox filesystem. Supports both text (str) and binary (bytes) content.
Automatically creates directories if they don’t exist.

Parameters:
ParameterTypeRequiredDescription
pathstrYesFile path (must start with /home/damner)
contentstr or bytesYesFile content (text string or binary data)

Returns:
None : Resolves when the file write operation is complete.

Examples
import asyncio
import json
from fermion_sandbox import Sandbox

async def main():
    sandbox = Sandbox(api_key="your-api-key")
    await sandbox.create(should_backup_filesystem=False)

    # Write a simple Python file
    await sandbox.write_file(
        path="/home/damner/script.py",
        content='print("Hello World")'
    )

    # Multi line content
    config = {
        "name": "my-app",
        "version": "1.0.0"
    }

    await sandbox.write_file(
        path="/home/damner/config.json",
        content=json.dumps(config, indent=2)
    )

asyncio.run(main())
Path Handling
# Valid paths (must start with /home/damner)

await sandbox.write_file(path="/home/damner/file.txt", content="data")
await sandbox.write_file(path="/home/damner/code/file.txt", content="data")
await sandbox.write_file(path="/home/damner/nested/dir/file.txt", content="data")

# Invalid paths
await sandbox.write_file(path="/tmp/file.txt", content="data")  # Error
await sandbox.write_file(path="relative/file.txt", content="data")  # Error
await sandbox.write_file(path="./file.txt", content="data")  # Error