If you're building an AI agent, coding assistant, or educational platform, you'll eventually need to execute user-submitted code. And if you've ever tried to run eval() on untrusted input, you know that's a one-way ticket to getting pwned.
The safe way? Run code in an isolated sandbox — no filesystem access, no network, strict timeouts, resource limits. But setting up Docker containers, gVisor, or Firecracker VMs is a pain.
In this post, I'll show you how to execute Python, JavaScript, TypeScript, and Bash code safely using a free sandbox API — zero infrastructure, one HTTP call.
Why You Need a Code Sandbox
Common use cases:
- AI agents that write and test code (LLM tool use)
- Online code editors and playgrounds
- Coding challenges and interview platforms
- Education platforms where students run exercises
- CI/CD pipelines that need quick code validation
The risk of running code directly:
# Never do this
exec(user_input) # RCE in 3 characters
// Also never do this
eval(userCode); // Full access to your Node.js process
The API: One Endpoint, Four Languages
Here's the simplest possible code execution:
curl -X POST https://agent-gateway-kappa.vercel.app/v1/agent-coderunner/execute \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"language": "python",
"code": "print(sum(range(1, 101)))"
}'
Response:
{
"id": "eb44ad44-b024-4a90-a1dd-1b6adc3f9f93",
"language": "python",
"exitCode": 0,
"stdout": "5050",
"stderr": "",
"duration": 13,
"timedOut": false
}
Get a free API key (200 credits, no signup):
curl -X POST https://agent-gateway-kappa.vercel.app/api/keys/create
Running Python
Python 3.12, standard library included:
const response = await fetch('https://agent-gateway-kappa.vercel.app/v1/agent-coderunner/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
language: 'python',
code: `
import json
import math
data = [math.sqrt(i) for i in range(1, 11)]
print(json.dumps({"roots": [round(x, 3) for x in data]}))
`
})
});
const result = await response.json();
console.log(result.stdout);
// {"roots": [1.0, 1.414, 1.732, 2.0, 2.236, 2.449, 2.646, 2.828, 3.0, 3.162]}
Running JavaScript (Node.js 20)
import requests
result = requests.post(
'https://agent-gateway-kappa.vercel.app/v1/agent-coderunner/execute',
headers={'Authorization': 'Bearer YOUR_API_KEY'},
json={
'language': 'javascript',
'code': '''
const crypto = require("crypto");
const hash = crypto.createHash("sha256").update("hello world").digest("hex");
console.log(JSON.stringify({ input: "hello world", sha256: hash }));
'''
}
).json()
print(f"Exit code: {result['exitCode']}")
print(f"Output: {result['stdout']}")
Running Bash
For quick scripting and system-level checks:
curl -X POST https://agent-gateway-kappa.vercel.app/v1/agent-coderunner/execute \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"language": "bash",
"code": "echo \"CPU cores: $(nproc)\"\necho \"Memory: $(free -h | awk '/Mem:/ {print $2}')\"\necho \"Uptime: $(uptime -p)\""
}'
Building an AI Agent Tool
The killer use case: give your LLM the ability to write code and test it. Here's an OpenAI function-calling integration:
const tools = [{
type: 'function',
function: {
name: 'execute_code',
description: 'Execute Python or JavaScript code in a secure sandbox',
parameters: {
type: 'object',
properties: {
language: { type: 'string', enum: ['python', 'javascript'] },
code: { type: 'string', description: 'Code to execute' }
},
required: ['language', 'code']
}
}
}];
async function executeCode(language, code) {
const res = await fetch('https://agent-gateway-kappa.vercel.app/v1/agent-coderunner/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.GATEWAY_API_KEY}`
},
body: JSON.stringify({ language, code })
});
const result = await res.json();
if (result.exitCode !== 0) {
return `Error (exit ${result.exitCode}):\n${result.stderr}`;
}
return result.stdout;
}
Now your AI agent can:
- Write code to solve a problem
- Execute it in a sandbox
- Read the output
- Fix errors and retry
No Docker, no VMs, no infrastructure.
Handling Errors Gracefully
The API returns structured error info you can act on:
async function safeExecute(language, code) {
const result = await executeCode(language, code);
if (result.timedOut) {
return { error: 'Code execution timed out (10s limit)' };
}
if (result.exitCode !== 0) {
return {
error: 'Runtime error',
stderr: result.stderr,
exitCode: result.exitCode
};
}
return {
output: result.stdout,
duration: `${result.duration}ms`
};
}
What the Sandbox Blocks
Each execution runs in an isolated environment:
| Feature | Status |
|---|---|
| Filesystem read/write | Restricted |
| Network access | Blocked |
| Process spawning | Limited |
| Execution time | 10s timeout |
| Memory | Capped |
| Stdout | Truncated at 64KB |
This means malicious code like import os; os.system("rm -rf /") or while True: pass won't harm anything — it'll either be blocked or time out.
Comparing Approaches
| Approach | Setup Time | Cost | Isolation | Latency |
|---|---|---|---|---|
eval() / exec()
|
0 min | Free | None (dangerous) | <1ms |
| Docker containers | Hours | Server costs | Good | 500ms+ cold start |
| AWS Lambda | 30 min | Pay per invoke | Great | 100-500ms |
| Firecracker/gVisor | Days | Server costs | Excellent | 50-200ms |
| Sandbox API | 1 min | Free (200 credits) | Good | ~15ms |
Try It Now
Get your API key and run your first sandboxed code:
# Get a free key
API_KEY=$(curl -s -X POST https://agent-gateway-kappa.vercel.app/api/keys/create | jq -r '.key')
# Run Python
curl -s -X POST https://agent-gateway-kappa.vercel.app/v1/agent-coderunner/execute \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{"language":"python","code":"print(\"It works!\")"}' | jq .
# Run JavaScript
curl -s -X POST https://agent-gateway-kappa.vercel.app/v1/agent-coderunner/execute \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{"language":"javascript","code":"console.log(Array.from({length:10}, (_,i) => i*i))"}' | jq .
200 free credits, no signup required. The API also gives you 40+ other services (crypto prices, IP geolocation, screenshots, DNS lookup) through the same key.
Top comments (0)