Skip to main content
Execute Python code with access to pre-installed data science and machine learning libraries.
curl -X POST https://api.gravixlayer.com/v1/agents/sandboxes/{sandbox_id}/code/run \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "import pandas as pd\ndf = pd.DataFrame({\"name\": [\"Alice\", \"Bob\"], \"age\": [25, 30]})\nprint(df)",
    "language": "python"
  }'

Request Parameters

ParameterTypeRequiredDescription
codestringYesPython code to execute
languagestringNoSet to “python” (default)
timeoutintegerNoTimeout in seconds (default: 30)

Response

{
  "execution_id": "exec_abc123",
  "results": {
    "stdout": [
      {
        "type": "stdout",
        "line": "   name  age",
        "timestamp": "2025-01-27T10:30:00Z"
      },
      {
        "type": "stdout",
        "line": "0  Alice   25",
        "timestamp": "2025-01-27T10:30:00Z"
      }
    ]
  },
  "error": null,
  "logs": {
    "stdout": ["   name  age", "0  Alice   25", "1    Bob   30"],
    "stderr": []
  }
}

Available Libraries

  • Data Science: NumPy, Pandas, Matplotlib, Seaborn
  • Machine Learning: Scikit-learn, TensorFlow, PyTorch
  • Web: Requests, BeautifulSoup4, FastAPI, Flask
  • Development: Jupyter, pytest, black

Example: Data Analysis

curl -X POST https://api.gravixlayer.com/v1/agents/sandboxes/{sandbox_id}/code/run \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "code": "import numpy as np\nimport matplotlib.pyplot as plt\n\n# Generate data\ndata = np.random.normal(0, 1, 1000)\n\n# Calculate statistics\nprint(f\"Mean: {np.mean(data):.3f}\")\nprint(f\"Std: {np.std(data):.3f}\")\n\n# Save plot\nplt.hist(data, bins=30)\nplt.savefig(\"/home/user/histogram.png\")\nprint(\"Plot saved to histogram.png\")",
    "language": "python"
  }'

Next Steps