> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ppio.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Download files from a sandbox

> Copy a file from a sandbox to the local filesystem with files.read() as bytes, or generate a short-lived pre-signed download URL for a browser.

`files.read()` returns file content from the sandbox, which local code writes to disk. A second path hands the download to a browser through a pre-signed URL, for clients that hold no API key.

> [!NOTE]
>
> Install the SDK, or the CLI for `ppio` commands, and set the `PPIO_API_KEY` and `PPIO_DOMAIN` environment variables before running these examples. See [Quickstart](https://docs.ppio.ai/sandbox/quickstart.md) and [Install and authenticate](https://docs.ppio.ai/sandbox/cli/install.md).

## Download a file

Read the file as bytes, then write those bytes to the local filesystem.

```python Python theme={null}
from ppio_sandbox import PPIO

ppio = PPIO()
sandbox = ppio.code_interpreter.create()

# Create a file in the sandbox for testing
file_path_in_sandbox = '/tmp/test-file'
sandbox.files.write(file_path_in_sandbox, 'test-file-content')

# Read the file from the sandbox as bytes
content = sandbox.files.read(file_path_in_sandbox, format='bytes')

# Write the file to the local filesystem
local_file_path = './local-test-file'
with open(local_file_path, 'wb') as file:
    file.write(content)

sandbox.kill()
```

```javascript Node.js theme={null}
// index.mts
import fs from 'fs'
import { PPIO } from 'ppio-sandbox'

const ppio = new PPIO()
const sandbox = await ppio.codeInterpreter.create()

// Create a file in the sandbox for testing
const filePathInSandbox = '/tmp/test-file'
await sandbox.files.write(filePathInSandbox, 'test-file-content')

// Read the file from the sandbox as bytes
const content = await sandbox.files.read(filePathInSandbox, { format: 'bytes' })

// Write the file to the local filesystem
const localFilePath = './local-test-file'
fs.writeFileSync(localFilePath, content)

await sandbox.kill()
```

## Download with a pre-signed URL

Pre-signed download URLs let users download files securely from environments that hold no API key, such as web browsers. Create the sandbox with the `secure: true` option, then use the SDK on the trusted backend to generate the download URL.

```mermaid theme={null}
sequenceDiagram
  participant B as Backend SDK
  participant S as Sandbox
  participant U as Browser
  B->>S: create sandbox (secure: true)
  B->>B: generate download URL
  B-->>U: URL (authorized users only)
  U->>S: GET the URL
```

> [!WARNING]
>
> Return the URL only to users authorized by the application, and set a short expiration time. Anyone who holds the URL can use it until it expires.

```python Python theme={null}
from pathlib import Path

import requests
from ppio_sandbox import PPIO

ppio = PPIO()
sandbox = ppio.code_interpreter.create(secure=True)

# Create a test file in the sandbox (requires SDK authentication)
file_path_in_sandbox = '/tmp/test-file'
sandbox.files.write(file_path_in_sandbox, 'test-file-content')

# Generate pre signed download URL (valid for 120 seconds, optional)
signed_url = sandbox.download_url(
    path=file_path_in_sandbox,
    use_signature_expiration=120,  # optional, in seconds
)

# Simulate "browser/unauthorized environment": without API key, directly GET download
response = requests.get(signed_url, timeout=60)
response.raise_for_status()
content = response.content

# Write to local file
local_file_path = Path('./local-test-file')
local_file_path.write_bytes(content)
print(content)

sandbox.kill()
```

```javascript Node.js theme={null}
// index.mts
import fs from 'fs'
import { PPIO } from 'ppio-sandbox'

const ppio = new PPIO()
const sandbox = await ppio.codeInterpreter.create({ secure: true })

// Create a test file in the sandbox (requires SDK authentication)
const filePathInSandbox = '/tmp/test-file'
await sandbox.files.write(filePathInSandbox, 'test-file-content')

// Generate pre signed download URL (valid for 120 seconds, optional)
const publicDownloadUrl = await sandbox.downloadUrl(filePathInSandbox, {
  useSignatureExpiration: 120, // optional, in seconds
})

// Simulate "browser/unauthorized environment": without API key, directly GET download
const res = await fetch(publicDownloadUrl)
if (!res.ok) {
  throw new Error(`Download failed: ${res.status} ${await res.text()}`)
}
const content = Buffer.from(await res.arrayBuffer())

// Write to local file
const localFilePath = './local-test-file'
fs.writeFileSync(localFilePath, content)
console.log(content.toString())

await sandbox.kill()
```


## Related topics

- [Work with files in a sandbox](https://docs.ppio.ai/sandbox/filesystem/overview.md)
- [Upload files to a sandbox](https://docs.ppio.ai/sandbox/filesystem/upload.md)
- [Secure access to sandbox services](https://docs.ppio.ai/sandbox/secured-access.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.