All Products
Search
Document Center

Function Compute:Upload and Download Files

Last Updated:Jul 14, 2026

Upload and download URLs are suitable for browser-direct uploads, handing result files to environments that do not hold SDK credentials, or passing files to downstream systems. For small files, binary content, streams, and agent-generated code, prefer sandbox.files.write() and sandbox.files.read().

Note

If you want to upload or download files by using URLs returned by sandbox.downloadUrl() and sandbox.uploadUrl(), make sure that you explicitly set secure=false when creating the sandbox. Otherwise, uploading and downloading files through these URLs still requires the caller to send X-Access-Token.

Python example:

sandbox = Sandbox.create(..., secure=False)

TypeScript example:

const sandbox = await Sandbox.create("code-interpreter-v1", {
  ...,
  secure: false,
});

Generate a download URL

When a file has already been generated inside the sandbox, call sandbox.downloadUrl(path) to get a download URL, and then hand the file to the user or a downstream system from your application.

Python example:

sandbox.files.write("/tmp/result.txt", "analysis result\n")

download_url = sandbox.download_url("/tmp/result.txt")
print(download_url)

TypeScript example:

await sandbox.files.write("/tmp/result.txt", "analysis result\n");

const downloadUrl = await sandbox.downloadUrl("/tmp/result.txt");
console.log(downloadUrl);

Generate an upload URL

When you need to upload a file into the sandbox from a browser or another environment without SDK credentials, first call sandbox.uploadUrl(path) to get an upload URL, and then post the file to that URL as multipart/form-data.

Python example:

upload_url = sandbox.upload_url("/tmp/input.csv")
print(upload_url)

TypeScript example:

const uploadUrl = await sandbox.uploadUrl("/tmp/input.csv");
console.log(uploadUrl);

After the browser or third-party system gets the upload URL, it must send the file as multipart/form-data:

Python example:

import requests

with open("input.csv", "rb") as f:
    requests.post(upload_url, files={"file": f}).raise_for_status()

TypeScript example:

const form = new FormData();
form.append("file", file);

await fetch(uploadUrl, {
  method: "POST",
  body: form,
});

After the upload finishes, you can read or process the file directly inside the sandbox:

Python example:

result = sandbox.commands.run(
    "python3 - <<'PY'\nfrom pathlib import Path\nprint(Path('/tmp/input.csv').exists())\nPY"
)
print(result.stdout.strip())

TypeScript example:

const result = await sandbox.commands.run("python3 - <<'PY'\nfrom pathlib import Path\nprint(Path('/tmp/input.csv').exists())\nPY");
console.log(result.stdout.trim());

Notes

  • When handing URLs returned by downloadUrl(path) and uploadUrl(path) directly to environments without SDK credentials, use a sandbox created with secure=false.

  • secure=false reduces the access protection on sandbox endpoints. Control the sandbox lifetime, file paths, and data sensitivity accordingly.

  • downloadUrl(path) and uploadUrl(path) generate access URLs for the specified path, so confirm that the path matches your application expectations before use.

  • Do not write upload or download URLs that expose sensitive data into logs or share them with unrelated users.

  • After a large upload, use sandbox.files.exists() or a command check to verify that the file is readable.

  • Files are available only during the lifetime of the current sandbox. For long-term retention, write them to external storage.

Selection guidance

  • Small files, text files, binary content, streams, and agent-generated code: use sandbox.files.write() and sandbox.files.read().

  • Browser-direct uploads, downloading result files, or handing files to environments without SDK credentials: use sandbox.uploadUrl() and sandbox.downloadUrl(), and create the sandbox with secure=false.

  • Directory traversal, renaming, and deletion: use the Filesystem API.