Skip to main content
Write content to a file in the sandbox. Creates the file if it doesn’t exist, or overwrites if it does. Only allowed paths can be written.

Endpoint

POST /files/write

Request

Headers

Authorization: Bearer YOUR_JWT_TOKEN
Content-Type: application/json

Body Parameters

ParameterTypeRequiredDescription
pathstringYesDestination file path
contentstringYesFile content to write

Example Request

curl -X POST https://sandbox_abc123xyz.hopx.dev/files/write \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "path": "/workspace/data.txt",
    "content": "Hello World\nLine 2\nLine 3"
  }'
Write JSON file:
curl -X POST https://sandbox_abc123xyz.hopx.dev/files/write \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "path": "/workspace/config.json",
    "content": "{\"api_key\": \"test\", \"timeout\": 30}"
  }'

Response

Success (200 OK)

{
  "path": "/workspace/data.txt",
  "size": 30,
  "message": "File written successfully"
}

Response Fields

FieldTypeDescription
pathstringFile path
sizeintegerFile size in bytes
messagestringConfirmation message

Status Codes

CodeDescription
200File written successfully
400Invalid request (missing path or content)
401Unauthorized
403Path not allowed
500Write failed

Errors

Path Not Allowed (403)

{
  "error": "Path not allowed: /etc/config",
  "code": "FORBIDDEN"
}
Cause: The path is outside allowed directories. Fix: Only write files to /workspace, /tmp, or other explicitly allowed paths.

Use Cases

Write Configuration

curl -X POST https://sandbox_abc123xyz.hopx.dev/files/write \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "path": "/workspace/app_config.json",
    "content": "{\"database\": \"postgres://localhost/mydb\", \"port\": 5000}"
  }'

Write Python Script

curl -X POST https://sandbox_abc123xyz.hopx.dev/files/write \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "path": "/workspace/script.py",
    "content": "import pandas as pd\n\ndf = pd.DataFrame({\"a\": [1,2,3]})\nprint(df)\n"
  }'

Write CSV Data

curl -X POST https://sandbox_abc123xyz.hopx.dev/files/write \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "path": "/workspace/data.csv",
    "content": "Name,Age,City\nAlice,25,NYC\nBob,30,LA\nCharlie,35,SF\n"
  }'

Write and Execute

#!/bin/bash
# Write script
curl -X POST https://sandbox_abc123xyz.hopx.dev/files/write \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "path": "/workspace/hello.py",
    "content": "print(\"Hello from HopX!\")"
  }'

# Execute it
curl -X POST https://sandbox_abc123xyz.hopx.dev/execute \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "exec(open(\"/workspace/hello.py\").read())",
    "language": "python"
  }'

Write from Python

import requests
import json

url = "https://sandbox_abc123xyz.hopx.dev/files/write"
headers = {"Authorization": "Bearer YOUR_JWT_TOKEN"}

# Prepare data
data = {
    "users": [
        {"name": "Alice", "age": 25},
        {"name": "Bob", "age": 30}
    ]
}

payload = {
    "path": "/workspace/users.json",
    "content": json.dumps(data, indent=2)
}

response = requests.post(url, headers=headers, json=payload)
print(f"Wrote {response.json()['size']} bytes")

Next Steps