unsandbox.com
Anonymous remote code, compile, & execution API for humans & machine learning agents.
Docs 📚 View Pricing →The OCaml examples below require the following 3rd party libraries:
lwtcohttp-lwt-unixyojson
Make sure these packages are installed in your environment before running the examples.
Execute code immediately and wait for results. Best for quick scripts and interactive use.
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
language |
string | ✓ | Programming language |
code |
string | ✓ | Source code to execute |
env |
object | Environment variables (key-value pairs) | |
network_mode |
string | "zerotrust" or "semitrusted" | |
ttl |
integer | Timeout 1-900s (default: 60) | |
return_artifact |
boolean | Return compiled binary | |
return_wasm_artifact |
boolean | Return WebAssembly binary |
Example (OCaml)
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
let body = {|{
"language": "ocaml",
"code": "print_endline \"Hello from unsandbox!\";;"
}|} in
let uri = Uri.of_string "https://api.unsandbox.com/execute" in
let headers = Header.init ()
|> fun h -> Header.add h "Content-Type" "application/json"
|> fun h -> Header.add h "Authorization" "Bearer (try Sys.getenv "UNSANDBOX_API_KEY" with Not_found -> "unsb-sk-test0-vault-unlck-12345")"
in
let req_body = Cohttp_lwt.Body.of_string body in
Lwt_main.run (
Client.post ~headers ~body:req_body uri >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
print_endline body
)
Response (Success)
{
"success": true,
"stdout": "Hello from unsandbox!\n",
"stderr": "",
"error": null,
"language": "python",
"exit_code": 0
}
Response (Error)
{
"success": false,
"error": "Syntax error in code",
"stderr": "SyntaxError: invalid syntax\n"
}
Submit code for execution and receive a job ID to poll for results. Best for long-running scripts or when you need to decouple submission from execution.
Request Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
language |
string | ✓ | Programming language |
code |
string | ✓ | Source code to execute |
env |
object | Environment variables (key-value pairs) | |
network_mode |
string | "zerotrust" or "semitrusted" | |
ttl |
integer | Timeout 1-900s (default: 60) | |
return_artifact |
boolean | Return compiled binary | |
return_wasm_artifact |
boolean | Return WebAssembly binary |
Example (OCaml)
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
let body = {|{
"language": "ocaml",
"code": "let rec factorial n = if n <= 1 then 1 else n * factorial (n - 1);;\nprint_endline \"Computing factorial...\" ;;\nPrintf.printf \"%d\\n\" (factorial 20);;",
"ttl": 300
}|} in
let uri = Uri.of_string "https://api.unsandbox.com/execute/async" in
let headers = Header.init ()
|> fun h -> Header.add h "Content-Type" "application/json"
|> fun h -> Header.add h "Authorization" "Bearer (try Sys.getenv "UNSANDBOX_API_KEY" with Not_found -> "unsb-sk-test0-vault-unlck-12345")"
in
let req_body = Cohttp_lwt.Body.of_string body in
Lwt_main.run (
Client.post ~headers ~body:req_body uri >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
print_endline body
)
Initial Response
{
"job_id": "job_1234567890_abc",
"status": "pending"
}
Check the status and results of an asynchronous job. Poll this endpoint after submitting a job via /execute/async or /run/async.
URL Parameters
| Parameter | Type | Description |
|---|---|---|
id |
string | Job ID returned from async endpoint |
Example (OCaml)
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
let uri = Uri.of_string "https://api.unsandbox.com/jobs/job_1234567890_abc" in
let headers = Header.init ()
|> fun h -> Header.add h "Authorization" "Bearer (try Sys.getenv "UNSANDBOX_API_KEY" with Not_found -> "unsb-sk-test0-vault-unlck-12345")"
in
Lwt_main.run (
Client.get ~headers uri >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
print_endline body
)
Response (Completed)
{
"job_id": "job_1234567890_abc",
"status": "completed",
"result": {
"success": true,
"stdout": "Hello from unsandbox!\n",
"stderr": "",
"error": null,
"language": "python",
"exit_code": 0
}
}
Possible Status Values
pending- Job queued, waiting to executerunning- Job currently executingcompleted- Job finished (check result field)timeout- Job exceeded TTL limitcancelled- Job was cancelled via DELETE
List all active (pending or running) jobs for your API key. Useful for monitoring multiple async executions.
Example (OCaml)
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
let uri = Uri.of_string "https://api.unsandbox.com/jobs" in
let headers = Header.init ()
|> fun h -> Header.add h "Authorization" "Bearer (try Sys.getenv "UNSANDBOX_API_KEY" with Not_found -> "unsb-sk-test0-vault-unlck-12345")"
in
Lwt_main.run (
Client.get ~headers uri >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
print_endline body
)
Response
{
"jobs": [
{
"job_id": "job_1234567890_abc",
"status": "running",
"language": "python",
"submitted_at": "2024-01-15T10:30:00Z"
},
{
"job_id": "job_0987654321_xyz",
"status": "pending",
"language": "go",
"submitted_at": "2024-01-15T10:29:45Z"
}
]
}
Cancel a pending or running job. If the job is already executing, it will be terminated and partial output returned.
URL Parameters
| Parameter | Type | Description |
|---|---|---|
id |
string | Job ID to cancel |
Example (OCaml)
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
let uri = Uri.of_string "https://api.unsandbox.com/jobs/job_1234567890_abc" in
let headers = Header.init ()
|> fun h -> Header.add h "Authorization" "Bearer (try Sys.getenv "UNSANDBOX_API_KEY" with Not_found -> "unsb-sk-test0-vault-unlck-12345")"
in
Lwt_main.run (
Client.delete ~headers uri >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
print_endline body
)
Response
{
"message": "Job cancelled",
"job_id": "job_1234567890_abc",
"result": {
"success": false,
"stdout": "Partial output before cancel...",
"stderr": "",
"error": "Job cancelled by user",
"exit_code": -1
}
}
Execute code with automatic language detection via shebang. Send raw code as the request body with Content-Type: text/plain.
Request
- Content-Type:
text/plain - Body: Raw code with shebang (e.g.,
#!/usr/bin/env python3)
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
env |
string | URL-encoded JSON object with environment variables | |
network_mode |
string | "zerotrust" or "semitrusted" | |
ttl |
integer | Timeout 1-900s (default: 60) | |
return_artifact |
boolean | Return compiled binary | |
return_wasm_artifact |
boolean | Return WebAssembly binary |
Example (OCaml)
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
let body = {|print_endline "Language auto-detected!";;|} in
let uri = Uri.of_string "https://api.unsandbox.com/run" in
let headers = Header.init ()
|> fun h -> Header.add h "Content-Type" "text/plain"
|> fun h -> Header.add h "Authorization" "Bearer (try Sys.getenv "UNSANDBOX_API_KEY" with Not_found -> "unsb-sk-test0-vault-unlck-12345")"
in
let req_body = Cohttp_lwt.Body.of_string body in
Lwt_main.run (
Client.post ~headers ~body:req_body uri >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
print_endline body
)
Response
{
"success": true,
"stdout": "Language auto-detected!\n",
"stderr": "",
"error": null,
"detected_language": "python",
"exit_code": 0
}
Submit code with automatic language detection and receive a job ID. Combines the convenience of /run with the flexibility of async execution.
Request
- Content-Type:
text/plain - Body: Raw code with shebang (e.g.,
#!/usr/bin/env ruby)
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
env |
string | URL-encoded JSON object with environment variables | |
network_mode |
string | "zerotrust" or "semitrusted" | |
ttl |
integer | Timeout 1-900s (default: 60) | |
return_artifact |
boolean | Return compiled binary | |
return_wasm_artifact |
boolean | Return WebAssembly binary |
Example (OCaml)
open Lwt
open Cohttp
open Cohttp_lwt_unix
let () =
let body = {|print_endline "Running async with auto-detect!";;
let sum = List.fold_left (+) 0 (List.init 1000000 (fun x -> x + 1));;
Printf.printf "%d\n" sum;;|} in
let uri = Uri.of_string "https://api.unsandbox.com/run/async" in
let headers = Header.init ()
|> fun h -> Header.add h "Content-Type" "text/plain"
|> fun h -> Header.add h "Authorization" "Bearer (try Sys.getenv "UNSANDBOX_API_KEY" with Not_found -> "unsb-sk-test0-vault-unlck-12345")"
in
let req_body = Cohttp_lwt.Body.of_string body in
Lwt_main.run (
Client.post ~headers ~body:req_body uri >>= fun (resp, body) ->
body |> Cohttp_lwt.Body.to_string >|= fun body ->
print_endline body
)
Initial Response
{
"job_id": "job_1234567890_abc",
"status": "pending"
}
Job Result (via GET /jobs/{id})
{
"job_id": "job_1234567890_abc",
"status": "completed",
"result": {
"success": true,
"stdout": "Running async with auto-detect!\n",
"stderr": "",
"error": null,
"detected_language": "ruby",
"exit_code": 0
}
}