JavaScript
JavaScript Function tools run on a V8 engine. The runtime is synchronous only - async/await, Promises, and setTimeout are not supported. Each run starts in a fresh VM with no state carried over from previous 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. 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 Secrets
Store credentials as Environment Secrets at /user/secrets, attach them to your tool from the Code step of the Tool Editor, and read them through process.env:
function cortexone_handler(event, context) { var apiKey = process.env.MY_API_KEY; return { statusCode: 200, body: { ready: !!apiKey } };}See Environment Variables for the full workflow.
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() returns 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 - external packages are not supported |
| File system | No |
What is restricted
- No external packages - there is no
requireorimport, and no package manifest. All code must live in the files you upload. - No file system access.
- Each run starts in a fresh VM - no globals carry over and no files persist between runs.
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 } };}Testing your code
Save and run sample payloads from the Test panel inside the Code step of the Tool Editor. The panel shows the returned statusCode, body, and any console.log output from your handler. See Testing & Executing for the full workflow.
Notes
- The main file must be named
cortexone_function.jsand must containcortexone_handler. - The handler is synchronous - async/await is not supported.
- Each run starts fresh - no state carries over between calls.
contextis passed but not populated - useeventfor all input data.