#
API Reference
Complete reference documentation for the microsandbox server HTTP API.
#
Base URL
The microsandbox server runs on http://127.0.0.1:5555
by default. All API endpoints are prefixed with /api/v1
.
#
Authentication
The API uses Bearer token authentication. Include your API key in the Authorization header:
Authorization: Bearer YOUR_API_KEY
Generate an API key using the CLI:
msb server keygen
#
REST Endpoints
Check if the server is running and healthy.
Endpoint: GET /api/v1/health
Response:
{
"message": "Service is healthy"
}
Status Codes:
200 OK
- Server is healthy
#
JSON-RPC API
The main API uses JSON-RPC 2.0 over HTTP POST. All requests should be sent to /api/v1/rpc
.
Content-Type: application/json
#
Request Format
{
"jsonrpc": "2.0",
"method": "method_name",
"params": { ... },
"id": "unique_request_id"
}
#
Response Format
Success:
{
"jsonrpc": "2.0",
"result": { ... },
"id": "unique_request_id"
}
Error:
{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "Error description",
"data": null
},
"id": "unique_request_id"
}
#
Sandbox Management
sandbox.start
Start a new sandbox with specified configuration.
Parameters:
Configuration Object:
Example Request:
{
"jsonrpc": "2.0",
"method": "sandbox.start",
"params": {
"sandbox": "my-python-env",
"namespace": "default",
"config": {
"image": "microsandbox/python",
"memory": 1024,
"cpus": 2,
"envs": ["DEBUG=true"],
"workdir": "/workspace"
}
},
"id": "1"
}
Response:
{
"jsonrpc": "2.0",
"result": "Sandbox my-python-env started successfully",
"id": "1"
}
Error Codes:
-32602
- Invalid parameters-32603
- Sandbox start failed
sandbox.stop
Stop a running sandbox and clean up its resources.
Parameters:
Example Request:
{
"jsonrpc": "2.0",
"method": "sandbox.stop",
"params": {
"sandbox": "my-python-env",
"namespace": "default"
},
"id": "2"
}
Response:
{
"jsonrpc": "2.0",
"result": "Sandbox my-python-env stopped successfully",
"id": "2"
}
Error Codes:
-32602
- Invalid parameters-32603
- Sandbox stop failed
sandbox.metrics.get
Get metrics and status for sandboxes including CPU usage, memory consumption, and running state.
Parameters:
Example Request:
{
"jsonrpc": "2.0",
"method": "sandbox.metrics.get",
"params": {
"namespace": "default",
"sandbox": "my-python-env"
},
"id": "3"
}
Response:
{
"jsonrpc": "2.0",
"result": {
"sandboxes": [
{
"namespace": "default",
"name": "my-python-env",
"running": true,
"cpu_usage": 15.5,
"memory_usage": 256,
"disk_usage": 1048576
}
]
},
"id": "3"
}
Response Fields:
Error Codes:
-32602
- Invalid parameters-32603
- Failed to get metrics
#
Code Execution
sandbox.repl.run
Execute code in a running sandbox. This method is forwarded to the sandbox's portal service.
Prerequisites: The target sandbox must be started first using sandbox.start
.
Parameters:
Example Request:
{
"jsonrpc": "2.0",
"method": "sandbox.repl.run",
"params": {
"sandbox": "my-python-env",
"namespace": "default",
"language": "python",
"code": "print('Hello, World!')"
},
"id": "4"
}
Response:
{
"jsonrpc": "2.0",
"result": {
"status": "completed",
"language": "python",
"output": "Hello, World!\n",
"error": "",
"has_error": false
},
"id": "4"
}
Response Fields:
Error Codes:
-32602
- Invalid parameters-32603
- Execution failed
sandbox.command.run
Execute a shell command in a running sandbox. This method is forwarded to the sandbox's portal service.
Prerequisites: The target sandbox must be started first using sandbox.start
.
Parameters:
Example Request:
{
"jsonrpc": "2.0",
"method": "sandbox.command.run",
"params": {
"sandbox": "my-python-env",
"namespace": "default",
"command": "ls",
"args": ["-la", "/workspace"]
},
"id": "5"
}
Response:
{
"jsonrpc": "2.0",
"result": {
"command": "ls",
"args": ["-la", "/workspace"],
"exit_code": 0,
"success": true,
"output": "total 4\ndrwxr-xr-x 2 root root 4096 Jan 1 12:00 .\n",
"error": ""
},
"id": "5"
}
Response Fields:
Error Codes:
-32602
- Invalid parameters-32603
- Command execution failed
#
MCP (Model Context Protocol) Support
The microsandbox server also implements the Model Context Protocol, making it compatible with AI tools like Claude.
The server supports the following MCP methods:
MCP Tools Available:
sandbox_start
- Start a new sandboxsandbox_stop
- Stop a running sandboxsandbox_run_code
- Execute code in a sandboxsandbox_run_command
- Execute commands in a sandboxsandbox_get_metrics
- Get sandbox metrics
MCP Prompts Available:
create_python_sandbox
- Template for creating Python sandboxescreate_node_sandbox
- Template for creating Node.js sandboxes
#
Error Handling
#
Standard JSON-RPC Error Codes
#
Custom Error Codes
#
Common Error Scenarios
Sandbox Not Found:
{
"jsonrpc": "2.0",
"error": {
"code": -32603,
"message": "Sandbox 'nonexistent' not found in configuration"
},
"id": "1"
}
Invalid Namespace:
{
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Namespace cannot be empty"
},
"id": "1"
}
Authentication Required:
{
"jsonrpc": "2.0",
"error": {
"code": -32002,
"message": "Authentication required"
},
"id": "1"
}
#
Rate Limiting
The API does not currently implement rate limiting, but it's recommended to:
- Limit concurrent sandbox starts to avoid resource exhaustion
- Use reasonable timeouts for long-running operations
- Monitor resource usage through the metrics endpoint
#
Best Practices
Recommended Practices
- Always stop sandboxes when done to prevent resource leaks
- Use meaningful names for sandboxes and namespaces
- Set appropriate timeouts for operations
- Monitor metrics regularly to track resource usage
- Handle errors gracefully with proper retry logic
- Use namespaces to organize sandboxes by project or team
Common Pitfalls
- Starting sandboxes without stopping them (resource leaks)
- Using invalid characters in sandbox/namespace names
- Not handling timeout errors properly
- Attempting operations on non-existent sandboxes
- Forgetting to include authentication headers
#
Examples
#
Complete Workflow Example
1. Start the server:
msb server start --dev
2. Generate API key:
msb server keygen
3. Use the API:
const apiKey = "your-api-key";
const baseUrl = "http://127.0.0.1:5555/api/v1/rpc";
4. Start a sandbox:
const startResponse = await fetch(baseUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
jsonrpc: "2.0",
method: "sandbox.start",
params: {
sandbox: "my-env",
namespace: "default",
config: {
image: "microsandbox/python",
memory: 512
}
},
id: "1"
})
});
5. Execute code:
const runResponse = await fetch(baseUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
jsonrpc: "2.0",
method: "sandbox.repl.run",
params: {
sandbox: "my-env",
namespace: "default",
language: "python",
code: "print('Hello from API!')"
},
id: "2"
})
});
6. Stop the sandbox:
const stopResponse = await fetch(baseUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
jsonrpc: "2.0",
method: "sandbox.stop",
params: {
sandbox: "my-env",
namespace: "default"
},
"id": "3"
})
});