Functions
Execute functions and easily access their return values
If you want to execute a function and return a value, you can use our Execute Function API.
While our Execute Code API is great for running scripts, it can be cumbersome to
return values from a function, since we only have access to stdout
and stderr
.
The Execute Function API is designed to make it easier to pass values to and from
a function in a way that’s more natural to programmers and LLMs alike.
This API supports Python, JavaScript, and TypeScript. It requires you to write
your code in a specific way, with a function execute
that takes a single
argument input
. Your function must also return a value that can be serialized
to JSON.
Writing your execute
function
The only rules are that your function must be named execute
, take a single
argument input
, and return a value that can be serialized to JSON. This
function will be passed as the code
argument to your API call.
Our execution environment will automatically execute your function as long as it’s named correctly.
Passing input to the function
When calling the API, you can pass an input
argument to your request. This
will be passed to your function as the input
argument. If you omit input
in
your execution request, your function will receive null
as input
.
Output
The output of a function execution will contain both the output of the function
as well as the execution details (exit_code
, stdout
, stderr
).
We will also return an output_status
field, which will be valid
if the
function returned a valid JSON object; json_serialization_error
if the
function returned a value that could not be serialized to JSON; and
error
if your function raised an error during execution.