Skip to content

JavaScript API

The JavaScript Execution API allows you to execute pure JavaScript code securely in a sandboxed environment.

Authentication

All JavaScript API endpoints require authentication using an API key. Include your API key in the X-API-KEY header of your requests.

curl -X POST "https://magic-meal-kits-xxxxx.run.app/api/v1/javascript/execute" \
     -H "X-API-KEY: your-api-key" \
     -H "Content-Type: application/json" \
     -d '{
       "code": "return 2 + 2;"
     }'

Video

Endpoints

Execute JavaScript

Executes pure JavaScript code in a secure, sandboxed environment and returns the result.

POST /api/v1/javascript/execute

Request Body:

{
  "code": "const result = 2 + 2; return result;",
  "timeout": 5000,
  "args": ["arg1", "arg2"]
}
Field Type Required Description
code String Yes JavaScript code to execute
timeout Integer No Execution timeout in milliseconds (default: 5000)
args String[] No Arguments to pass to the script

Example Response:

{
  "success": true,
  "result": "Hello, World!",
  "execution_time": 12
}

Code Execution Environment

The JavaScript execution environment is a secure, sandboxed environment with the following characteristics:

Pure JavaScript Only

The API only supports pure JavaScript execution. You must write self-contained code that doesn't rely on any external libraries.

Return Values

Your code must use a return statement to provide the result. The returned value must be JSON-serializable (objects, arrays, strings, numbers, booleans, or null).

Limitations

The JavaScript execution environment has the following limitations:

  • Execution Time: Maximum execution time is 10 seconds
  • Memory: Maximum memory usage is 256 MB
  • Code Length: Maximum code length is 100 KB
  • Result Size: Maximum result size is 5 MB
  • Network Access: No network access
  • File System: No access to the file system
  • Process: No access to process information or child processes

Error Handling

If an error occurs during execution, the API returns an error response:

{
  "success": false,
  "error": "Error message",
  "error_type": "SyntaxError",
  "stack_trace": "SyntaxError: Unexpected token )\n    at ..."
}

Common error types:

  • SyntaxError: Invalid JavaScript syntax
  • ReferenceError: Reference to undefined variable
  • TypeError: Operation on incompatible type
  • RangeError: Value outside of allowed range
  • TimeoutError: Execution exceeded timeout limit
  • MemoryLimitError: Execution exceeded memory limit

Security Considerations

The JavaScript Execution API implements several security measures:

  1. Sandboxed Environment: Code runs in a secure, isolated environment
  2. Resource Limits: Strict limits on CPU, memory, and execution time
  3. No Network Access: No ability to make network requests
  4. No File System Access: No access to the server's file system
  5. No Process Access: No ability to spawn processes or access system information