Functions are JavaScript functions that you can define and execute in Cognigy.AI. Use Functions to assist with asynchronous processes, such as interacting with third-party systems through an HTTP API.
The execution of Functions is independent of Flows, but you can trigger them from a Flow. You can also use the inject and notify API endpoints in a Function to send results back into a Flow.
You can access the parameters and api objects from the Function’s arguments.
Limitations
- The maximum run-time of a Function instance is 15 minutes. For on-premises installations, you can change this limit via the
FUNCTION_EXECUTION_MAX_EXECUTION_TIME_IN_MINUTES environment variable in the values.yaml file. For long-running operations that exceed this limit, set up a dedicated middleware service, such as AWS Lambda or Azure Functions, and call back using the Inject API once the process completes. This approach is more scalable than Cognigy Functions for time-intensive tasks.
- You can define a Function only in JavaScript or TypeScript.
Working with Functions
You can view, create, edit, and delete Functions in Build > Functions. To define, trigger, and monitor a Function, use the Code and Instances tabs.Also, you can trigger a Function from the Flow editor by using the Trigger Function Node. You can view, create, edit, and delete Functions using the Cognigy.AI API. Using this API, you can also monitor, trigger, and stop a Function instance.
Examples
You can use HTTP requests to interact with third-party systems.
GET Request
export default async ({ parameters, api }: IFunctionExecutionArguments) => {
const response = await api.httpRequest({
method: "get",
url: "https://example.com/api/v2",
headers: {
"Accept": "application/json"
}
});
}
POST Request
export default async ({ parameters, api }: IFunctionExecutionArguments) => {
const response = await api.httpRequest({
method: "post",
url: "https://api.github.com/repos/octocat/Hello-World/issues",
headers: {
"Accept": "application/vnd.github.v3+json",
"Authorization": "Bearer <your-github-token>"
},
data: {
"title": "Found a bug",
"body": "I'm having a problem with this.",
"labels": ["bug"]
}
});
}
Functions can interact with Flows through the Inject and Notify APIs.The following examples assume that you pass userId and sessionId through the function’s parameters.
Injecting Text into Flows
export default async ({ parameters, api }: IFunctionExecutionArguments) => {
const { userId, sessionId } = parameters;
api.inject({
userId,
sessionId,
text: "This text was injected through a Function"
});
}
Sending Notifications
export default async ({ parameters, api }: IFunctionExecutionArguments) => {
const { userId, sessionId } = parameters;
api.notify({
userId,
sessionId,
text: "This text was injected through a Function"
});
}
Find more Function examples in the Cognigy’s functions repository.
Use Case
Functions can help you handle HTTP requests that exceed the timeout in the HTTP Request Node. The HTTP Request Node allows direct access to API responses from the Flow, which is not possible with Functions. However, Functions allow you to work with slower APIs or long-running processes.