Skip to content

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 };
}
ParameterTypeDescription
eventobjectThe input payload sent by the caller
contextobjectExecution 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:

FieldTypeDescription
response.statusnumberHTTP status code
response.statusTextstringHTTP status text
response.bodystringResponse body as a raw string
response.headersobjectResponse 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

FeatureAvailable
process.envYes — read environment variables
fetch()Conditional — depends on version setting
JSONYes
Math, Date, Array, etc.Yes — standard built-ins
console.logYes — output goes to execution logs
require() / importNo — module imports are not supported
File systemNo

What is restricted

  • No file system access
  • No require or import — all code must be in the files you upload
  • Each execution runs in a completely fresh VM

Resource limits

LimitDefault
Max memory512 MB
Max runtime300 seconds
CPU limit2.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.js and must contain cortexone_handler.
  • The handler is synchronous — async/await is not supported.
  • There is no persistent state between executions.
  • context is passed but not populated — use event for all input data.