JavaScript
JavaScript tools run on a V8 engine. Each execution gets a fresh JavaScript VM — there is no state shared between calls.
Handler signature
Your main file must be named cortexone_function.js and must define a function called cortexone_handler:
function cortexone_handler(event, context) { var result = { message: "Hello, World!" }; return { statusCode: 200, body: result };}| Parameter | Type | Description |
|---|---|---|
event | object | The input payload sent by the caller |
context | object | Execution metadata (not populated for JavaScript) |
The return value should be an object with statusCode and body. You can return body as a plain object — the platform serializes it automatically. Use JSON.stringify() only when you need to control serialization of special types like BigInt or Date.
The event object
event is whatever JSON the caller passes:
function cortexone_handler(event, context) { var numbers = event.num; var sum = numbers.reduce(function(a, b) { return a + b; }, 0); var mean = sum / numbers.length; return { statusCode: 200, body: { sum: sum, mean: mean, count: numbers.length } };}Environment variables
function cortexone_handler(event, context) { var apiKey = process.env.MY_API_KEY; return { statusCode: 200, body: { ready: !!apiKey } };}HTTP requests
HTTP access is controlled per version. When enabled, a fetch() function is available. It returns a synchronous response object — not a Promise.
function cortexone_handler(event, context) { var response = fetch("https://api.example.com/data"); // response.body is the raw HTTP response body as a string var data = JSON.parse(response.body); return { statusCode: response.status, body: data };}The response object contains:
| Field | Type | Description |
|---|---|---|
response.status | number | HTTP status code |
response.statusText | string | HTTP status text |
response.body | string | Response body as a raw string |
response.headers | object | Response headers |
You can also pass options for method, headers, and body:
function cortexone_handler(event, context) { var response = fetch("https://api.example.com/submit", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ key: "value" }) }); return { statusCode: response.status, body: JSON.parse(response.body) };}If HTTP access is not enabled for your version, calling fetch() will return an error.
What is available
| Feature | Available |
|---|---|
process.env | Yes — read environment variables |
fetch() | Conditional — depends on version setting |
JSON | Yes |
Math, Date, Array, etc. | Yes — standard built-ins |
console.log | Yes — output goes to execution logs |
require() / import | No — module imports are not supported |
| File system | No |
What is restricted
- No file system access
- No
requireorimport— all code must be in the files you upload - Each execution runs in a completely fresh VM
Resource limits
| Limit | Default |
|---|---|
| Max memory | 512 MB |
| Max runtime | 300 seconds |
| CPU limit | 2.0 cores |
Example: Array processing
function cortexone_handler(event, context) { var numbers = event.num; var sum = numbers.reduce(function(a, b) { return a + b; }, 0); var mean = sum / numbers.length; return { statusCode: 200, body: { sum: sum, mean: mean, count: numbers.length } };}Error responses
If your handler throws an error, the platform catches it and returns:
{ "statusCode": 500, "body": "{\"error\": \"Script execution failed\", \"type\": \"ReferenceError\", \"details\": \"...\"}"}Timeouts return statusCode: 408. You can return non-200 status codes intentionally:
function cortexone_handler(event, context) { if (!event.id) { return { statusCode: 400, body: { error: "id is required" } }; } return { statusCode: 200, body: { ok: true } };}Notes
- The main file must be named
cortexone_function.jsand must containcortexone_handler. - The handler is synchronous — async/await is not supported.
- There is no persistent state between executions.
contextis passed but not populated — useeventfor all input data.