unsandbox.com
Anonymous remote code, compile, & execution API for humans & machine learning agents.
Docs 📚 View Pricing →The Java examples below require the following 3rd party libraries:
org.json:json
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 (Java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String url = "https://api.unsandbox.com/execute";
String payload = new JSONObject()
.put("language", "python")
.put("code", "print('Hello from unsandbox!')")
.toString();
String apiKey = System.getenv().getOrDefault("UNSANDBOX_API_KEY", "unsb-sk-test0-vault-unlck-12345");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONObject result = new JSONObject(response.body());
int exitCode = result.optInt("exit_code", 1);
if (exitCode == 0) {
System.out.println("Output: " + result.optString("stdout", ""));
} else {
System.out.println("Error: " + result.optString("stderr", ""));
}
}
}
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 (Java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
// Submit job
String url = "https://api.unsandbox.com/execute/async";
String payload = new JSONObject()
.put("language", "python")
.put("code", "print('Computing...')")
.put("ttl", 300)
.toString();
String apiKey = System.getenv().getOrDefault("UNSANDBOX_API_KEY", "unsb-sk-test0-vault-unlck-12345");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200 && response.statusCode() != 202) {
System.err.println("Failed to submit job: " + response.statusCode());
System.exit(1);
}
JSONObject job = new JSONObject(response.body());
if (!job.has("job_id")) {
System.err.println("Response missing job_id field");
System.exit(1);
}
String jobId = job.getString("job_id");
System.out.println("Job submitted: " + jobId);
// Wait briefly for job to be created
Thread.sleep(2000);
// Poll for results
String statusUrl = "https://api.unsandbox.com/jobs/" + jobId;
int maxAttempts = 20; // Max 20 seconds of polling
int attempts = 0;
while (attempts < maxAttempts) {
attempts++;
HttpRequest statusRequest = HttpRequest.newBuilder()
.uri(URI.create(statusUrl))
.header("Authorization", "Bearer " + apiKey)
.GET()
.build();
HttpResponse<String> statusResponse = client.send(statusRequest, HttpResponse.BodyHandlers.ofString());
if (statusResponse.statusCode() != 200) {
// Job might not be ready yet, continue polling
Thread.sleep(1000);
continue;
}
try {
JSONObject status = new JSONObject(statusResponse.body());
if (!status.has("status")) {
System.err.println("Response missing status field");
System.exit(1);
}
String jobStatus = status.getString("status");
if (jobStatus.equals("completed")) {
int exitCode = status.optInt("exit_code", 1);
if (exitCode == 0) {
System.out.println("Output: " + status.optString("stdout", ""));
} else {
System.out.println("Error: " + status.optString("stderr", ""));
}
break;
} else if (jobStatus.equals("timeout") || jobStatus.equals("cancelled")) {
System.out.println("Job " + jobStatus);
break;
} else if (jobStatus.equals("failed")) {
System.err.println("Job failed");
System.exit(1);
}
// Job is queued or running, continue polling
} catch (org.json.JSONException e) {
// Invalid JSON response, continue polling
Thread.sleep(1000);
continue;
}
Thread.sleep(1000); // Wait 1 second before polling again
}
if (attempts >= maxAttempts) {
System.err.println("Timeout: Job did not complete within " + maxAttempts + " seconds");
System.exit(1);
}
}
}
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 (Java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
// First create a job to query
String createUrl = "https://api.unsandbox.com/execute/async";
String createPayload = new JSONObject()
.put("language", "python")
.put("code", "print('test')")
.put("ttl", 60)
.toString();
String apiKey = System.getenv().getOrDefault("UNSANDBOX_API_KEY", "unsb-sk-test0-vault-unlck-12345");
HttpRequest createRequest = HttpRequest.newBuilder()
.uri(URI.create(createUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(createPayload))
.build();
HttpResponse<String> createResponse = client.send(createRequest, HttpResponse.BodyHandlers.ofString());
if (createResponse.statusCode() != 200 && createResponse.statusCode() != 202) {
System.err.println("Failed to create job: " + createResponse.statusCode());
System.exit(1);
}
JSONObject createdJob = new JSONObject(createResponse.body());
if (!createdJob.has("job_id")) {
System.err.println("Response missing job_id field");
System.exit(1);
}
String jobId = createdJob.getString("job_id");
System.out.println("Created job: " + jobId);
Thread.sleep(3000); // Wait for job to potentially complete
// Now get its status
String getUrl = "https://api.unsandbox.com/jobs/" + jobId;
HttpRequest getRequest = HttpRequest.newBuilder()
.uri(URI.create(getUrl))
.header("Authorization", "Bearer " + apiKey)
.GET()
.build();
HttpResponse<String> getResponse = client.send(getRequest, HttpResponse.BodyHandlers.ofString());
if (getResponse.statusCode() == 200) {
try {
JSONObject job = new JSONObject(getResponse.body());
if (!job.has("status")) {
System.out.println("Response missing status field");
return;
}
System.out.println("Status: " + job.getString("status"));
if (job.getString("status").equals("completed")) {
int exitCode = job.optInt("exit_code", 1);
if (exitCode == 0) {
System.out.println("Output: " + job.optString("stdout", ""));
}
}
} catch (org.json.JSONException e) {
System.out.println("Failed to parse job response");
}
} else {
System.out.println("Get job returned status: " + getResponse.statusCode());
}
}
}
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 (Java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String url = "https://api.unsandbox.com/jobs";
String apiKey = System.getenv().getOrDefault("UNSANDBOX_API_KEY", "unsb-sk-test0-vault-unlck-12345");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.GET()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONArray jobs = new JSONArray(response.body());
System.out.println("Found " + jobs.length() + " jobs:");
for (int i = 0; i < jobs.length(); i++) {
JSONObject job = jobs.getJSONObject(i);
System.out.println(job.getString("job_id") + ": " +
job.getString("status") + " (" +
job.getString("language") + ")");
}
}
}
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 (Java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
// First create a job to delete
String createUrl = "https://api.unsandbox.com/execute/async";
String createPayload = new JSONObject()
.put("language", "python")
.put("code", "print('test')")
.put("ttl", 60)
.toString();
String apiKey = System.getenv().getOrDefault("UNSANDBOX_API_KEY", "unsb-sk-test0-vault-unlck-12345");
HttpRequest createRequest = HttpRequest.newBuilder()
.uri(URI.create(createUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(createPayload))
.build();
HttpResponse<String> createResponse = client.send(createRequest, HttpResponse.BodyHandlers.ofString());
JSONObject job = new JSONObject(createResponse.body());
String jobId = job.getString("job_id");
System.out.println("Created job: " + jobId);
// Now delete it
String deleteUrl = "https://api.unsandbox.com/jobs/" + jobId;
HttpRequest deleteRequest = HttpRequest.newBuilder()
.uri(URI.create(deleteUrl))
.header("Authorization", "Bearer " + apiKey)
.DELETE()
.build();
HttpResponse<String> deleteResponse = client.send(deleteRequest, HttpResponse.BodyHandlers.ofString());
if (deleteResponse.statusCode() == 200) {
JSONObject result = new JSONObject(deleteResponse.body());
System.out.println("Deleted: " + result.optString("message", "Job deleted"));
} else {
System.out.println("Delete failed with status: " + deleteResponse.statusCode());
}
}
}
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 (Java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String url = "https://api.unsandbox.com/run";
String code = "#!/usr/bin/env python3\nprint(\"Language auto-detected!\")";
String apiKey = System.getenv().getOrDefault("UNSANDBOX_API_KEY", "unsb-sk-test0-vault-unlck-12345");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "text/plain")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(code))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONObject result = new JSONObject(response.body());
System.out.println("Detected: " + result.optString("detected_language", "unknown"));
int exitCode = result.optInt("exit_code", 1);
if (exitCode == 0) {
System.out.println("Output: " + result.optString("stdout", ""));
} else {
System.out.println("Error: " + result.optString("stderr", ""));
}
}
}
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 (Java)
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String url = "https://api.unsandbox.com/run/async";
String code = "#!/usr/bin/env ruby\nputs \"Running async with auto-detect!\"";
// Submit job
String apiKey = System.getenv().getOrDefault("UNSANDBOX_API_KEY", "unsb-sk-test0-vault-unlck-12345");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "text/plain")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(code))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200 && response.statusCode() != 202) {
System.err.println("Failed to submit job: " + response.statusCode());
System.exit(1);
}
JSONObject job = new JSONObject(response.body());
if (!job.has("job_id")) {
System.err.println("Response missing job_id field");
System.exit(1);
}
String jobId = job.getString("job_id");
System.out.println("Job submitted: " + jobId);
// Wait briefly for job to be created
Thread.sleep(2000);
// Poll for results
String statusUrl = "https://api.unsandbox.com/jobs/" + jobId;
int maxAttempts = 20; // Max 20 seconds of polling
int attempts = 0;
while (attempts < maxAttempts) {
attempts++;
HttpRequest statusRequest = HttpRequest.newBuilder()
.uri(URI.create(statusUrl))
.header("Authorization", "Bearer " + apiKey)
.GET()
.build();
HttpResponse<String> statusResponse = client.send(statusRequest, HttpResponse.BodyHandlers.ofString());
if (statusResponse.statusCode() != 200) {
// Job might not be ready yet, continue polling
Thread.sleep(1000);
continue;
}
try {
JSONObject status = new JSONObject(statusResponse.body());
if (!status.has("status")) {
System.err.println("Response missing status field");
System.exit(1);
}
String jobStatus = status.getString("status");
if (jobStatus.equals("completed")) {
System.out.println("Detected: " + status.optString("detected_language", "unknown"));
int exitCode = status.optInt("exit_code", 1);
if (exitCode == 0) {
System.out.println("Output: " + status.optString("stdout", ""));
}
break;
} else if (jobStatus.equals("timeout") || jobStatus.equals("cancelled")) {
System.out.println("Job " + jobStatus);
break;
} else if (jobStatus.equals("failed")) {
System.err.println("Job failed");
System.exit(1);
}
// Job is queued or running, continue polling
} catch (org.json.JSONException e) {
// Invalid JSON response, continue polling
Thread.sleep(1000);
continue;
}
Thread.sleep(1000); // Wait 1 second before polling again
}
if (attempts >= maxAttempts) {
System.err.println("Timeout: Job did not complete within " + maxAttempts + " seconds");
System.exit(1);
}
}
}
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
}
}