Skip to content

Functions

Functions are JavaScript functions that you can define and execute in Cognigy.AI. You can use Functions to assist with long-running and asynchronous processes, such as interacting with third-party systems through HTTP API.

The execution of Functions is independent of Flows, but you can trigger them from a Flow. You can also use the Inject & Notify APIs 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.
  • 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

Interact with Third-Party Systems

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"]
        }
    });
}
Interact with Flows

Functions can interact with Flows through the Inject & Notify APIs.

The following examples assume that you pass userId and sessionId through the Function's parameters.

Injecting Text into a Flow

export default async ({ parameters, api }: IFunctionExecutionArguments) => {
    const { userId, sessionId } = parameters;

    api.inject({
        userId,
        sessionId,
        text: "This text was injected through a Function"
    });
}

Sending a Notification

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 Function 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.

More Information