writeFile() method allows you to create or overwrite files inside your sandbox’s isolated filesystem. It supports both text and binary content, making it flexible enough for handling scripts, configuration files, and assets like images or compiled binaries. When writing a file, parent directories are created automatically if they don’t exist, ensuring that nested paths are handled gracefully without requiring manual directory setup.
All file paths must begin with either the tilde (~) shortcut or the absolute sandbox path /home/damner/code. Any attempt to write outside this directory will throw an error for security reasons. This method replaces existing files silently, so it’s best used with care when overwriting content.
writeFile() is commonly used to prepare your execution environment: for example, uploading scripts before running them, storing generated output, or programmatically managing project files within persistent sandboxes. Combined with getFile(), it provides a full read–write interface for your container’s filesystem.
writeFile(options)
Description:
Writes a file to the sandbox filesystem. Supports both text and binary content. Creates parent directories automatically if they don’t exist.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| path | string | Yes | File path (must start with ~ or /home/damner/code) |
| content | string | ArrayBuffer | Yes | File content (text string or binary data) |
Promise<void> - Resolves when write is complete
Example:
Error: "No container found"- Must callcreate()orfromSnippet()firstError: "Path must start with ~ or /home/damner/code"- Invalid path formatError: "Failed to set file"- Network error or permission issue
- Existing files are overwritten without warning
- No confirmation prompt
- Previous content is lost
- Use tilde
~for cleaner code - Create organized directory structures
- JSON.stringify for JSON files with proper indentation
- Check file was written with
getFile()if critical
