getFile() method retrieves a file from your sandbox’s filesystem and returns it as a standard Web Response object, allowing you to handle the data in multiple formats: text, JSON, binary, or Blob. This method gives you the flexibility to work with files in the same way you would when using the native fetch() API, making it intuitive and consistent for developers familiar with browser or Node.js environments.
All file paths must start with ~ or the absolute path /home/damner/code to ensure access is confined to the sandbox’s isolated workspace. If the requested file doesn’t exist or the path is invalid, an error is thrown.
getFile() is commonly used for reading script outputs, verifying file writes, or fetching configuration data generated during execution. Since the returned object supports .text(), .json(), .arrayBuffer(), and .blob() methods, it seamlessly supports diverse file types, from source code to media files. For large files, you can also use the response stream directly for efficient, chunked reading.
getFile(path)
Description:
Retrieves a file from the sandbox filesystem. Returns a standard Response object that you can read as text, binary, blob, etc.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| path | string | Yes | File path (must start with ~ or /home/damner/code) |
Promise<Response> - Standard fetch Response object with methods:
.text()- Read as string.arrayBuffer()- Read as binary.blob()- Read as Blob.json()- Parse as JSON
Error: "No container found"- Must callcreate()orfromSnippet()firstError: "File not found: <path>"- File doesn’t exist (404)Error: "Path must start with ~ or /home/damner/code"- Invalid pathError: "Failed to get file"- Network error or permission issue
- Always await both the getFile() call AND the .text()/.json() call
- Use try/catch for files that might not exist
- For large files, consider using streams
