Running tasks with Axios in Node.js

You can run a digital worker task by using Axios, a Promise-based HTTP client for Node.js.

Before you begin

See Running digital worker tasks to know how to get the access token from UMS.

About this task

In the following command lines, replace <task_integration_details> with the URL of the task that you want to run, for example:
https://9.20.193.65:30600/api/v1/tasks/5d9ae9b4db85c90049d2f877/runs

To find this URL, open your list of tasks in Digital Worker. Next to the name of the task, click the overflow menu and select Integration details.

You can get the integration details and run a task only when this task is activated.

Certificate validation: The following examples of commands skip the certificate validation, but you can adapt these commands to work with your own certificate.

Procedure

  1. Trigger the run by executing the following code:
    const taskInput = // Input of the task
        let triggerRun = {};
        try {
            triggerRun = (await axios.post('<task_integration_details>', taskInput, {headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer <accessToken>' }})).data;
        } catch(error) {
            // Deal with the error
        }
    

    After executing, you get a response that contains the runId corresponding to the ID of the run and a runURL corresponding to the path that is used to get the result of the run.

    runUrl corresponds to <task_integration_details>/<run_ID> in the following step.

  2. Get the result of the run by executing the following code. You must set the variable runId with the value of the run ID.
    const runId = triggerRun.runId;
        try {
            if (runId === undefined) {
                throw new Error('Cannot run the task');
            }
            const runResult = await new Promise((resolve, reject) => {
                const responsePolling = setInterval(async () => {
                    try {
                        const response = await axios.get(`<task_integration_details>/${runId}`, {headers: {Authorization: 'Bearer <accessToken>'}});
                        if (response.data.status !== 'in_progress') {
                            clearInterval(responsePolling);
                            resolve(response.data);
                        }
                    } catch (error) {
                        reject(error);
                    }
                }, 1000);
            });
            // Deal with the result
        } catch(error) {
            // Deal with the error
        }

    run_ID is the runId field in the object that is returned when you trigger the run.

    The following fields are also returned:
    • log: the logs of the execution of the task.
    • duration: the total duration of the run.
    • result: the output of the run, which might be also an object or a string. In this example, there is no output.