Skip to main content

POST /api/execute

Execute code in a secure, isolated Docker container. This endpoint supports multiple programming languages and returns the execution output, exit code, and execution time.

Request Body

code
string
required
The source code to execute. Must be valid code for the specified language.
language
string
required
The programming language of the code. Supported values:
  • java - Java
  • python - Python
  • c - C
  • cpp - C++
  • js - JavaScript

Response

Returns an ApiResponse object containing the execution result.
success
boolean
required
Indicates whether the request was successful.
message
string
required
A descriptive message about the execution result.
data
object
The execution result data. Only present when success is true.
data.output
string
required
The standard output from the code execution.
data.exitCode
integer
required
The exit code from the execution process. 0 indicates successful execution.
data.executionTime
long
required
The execution time in milliseconds.

Examples

curl -X POST http://localhost:8081/api/execute \
  -H "Content-Type: application/json" \
  -d '{
    "code": "print(\"Hello, World!\")",
    "language": "python"
  }'

Success Response

{
  "success": true,
  "message": "Code Executed successfully",
  "data": {
    "output": "Hello, World!\n",
    "exitCode": 0,
    "executionTime": 1245
  }
}

Error Responses

Unsupported Language

{
  "success": false,
  "message": "Unsupported language: ruby",
  "data": null
}

Compilation Error (Java example)

{
  "success": true,
  "message": "Code Executed successfully",
  "data": {
    "output": "Main.java:1: error: ';' expected\nSystem.out.println(\"test\")\n                          ^\n1 error\n",
    "exitCode": 0,
    "executionTime": 856
  }
}

Runtime Error (Python example)

{
  "success": true,
  "message": "Code Executed successfully",
  "data": {
    "output": "Traceback (most recent call last):\n  File \"main.py\", line 1, in <module>\n    print(undefined_variable)\nNameError: name 'undefined_variable' is not defined\n",
    "exitCode": 0,
    "executionTime": 423
  }
}

Notes

Code execution happens in isolated Docker containers with resource limits applied. Each execution is ephemeral and containers are automatically cleaned up after execution.
The exit code is currently always 0 regardless of compilation or runtime errors. Check the output field for error details, which will contain compilation errors or runtime exceptions if your code fails.
Execution time is measured in milliseconds and includes the time to spin up the container, compile (if needed), and execute the code.

Build docs developers (and LLMs) love