Skip to content

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 };
}
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. 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:

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() returns 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 - external packages are not supported
File systemNo

What is restricted

  • No external packages - there is no require or import, 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

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 } };
}

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.js and must contain cortexone_handler.
  • The handler is synchronous - async/await is not supported.
  • Each run starts fresh - no state carries over between calls.
  • context is passed but not populated - use event for all input data.