Skip to main content
Version: v5.1

How to create and run a workflow

Overview

This page describes how to use the Workflow Service to create a new workflow that fetches data from a REST API using the REST_CONNECTOR task. The Workflow service will then process the result with a script using the SCRIPT_EXECUTION task.

Useful concepts

There are some key concepts related to the Workflow Service that will help with understanding this example. See the resources below:

Steps for creating and running a new workflow

To create and run a new workflow, do the following:

  1. Create a workflow definition
  2. Run the workflow
  3. Check the workflow status
  4. Reference the task outputs
  5. Get individual task outputs/results

Step 1: Create a workflow definition

To create a new workflow definition, use the IafWorkflowSvs.createWorkflowDef API call as shown in the sample code below.

import IafWorkflowSvc from '@dtplatform/platform-api';

// Define your workflow definition
const workflowDef = {
_name: "REST to Script Workflow",
_description: "Fetch data from REST API and process with script",
_namespaces: ["your-namespace"],
_userType: "rest_script_demo",
_taskDefs: [
{
_name: "fetch_artist",
_type: "REST_CONNECTOR",
_sequenceno: 1,
_inputParams: {
_url: "https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg",
_to: "json",
_auth: {
_type: "Bearer",
_token: "YOUR_SPOTIFY_TOKEN"
}
}
},
{
_name: "process_artist",
_type: "SCRIPT_EXECUTION",
_sequenceno: 2,
_inputParams: {
"_userType": "artist_script",
"_scriptName": "ProcessArtist",
"artistData": "${fetch_artist.output}" // <-- Pass previous output as a parameter
}
}
]
};

// Create the workflow definition
const ctx = { /* your context, e.g., auth info */ };
const createdDef = await IafWorkflowSvc.createWorkflowDef(workflowDef, ctx);
console.log('Created WorkflowDef:', createdDef);

Step 2: Run the workflow

To start the workflow instance, use IafWorkflowSvc.runWorkflow as shown in the sample code below.

const workflowDefId = createdDef._id;
const runResult = await IafWorkflowSvc.runWorkflow(workflowDefId, null, ctx);
console.log('Workflow Run Result:', runResult);

Step 3: Check the workflow status

To check the status of your workflow, use IafWorkflowSvc.getWorkflowStatus.

const workflowId = runResult._id;
const status = await IafWorkflowSvc.getWorkflowStatus(workflowId, ctx);
console.log('Workflow Status:', status);

Step 4: Reference the task outputs

You can reference task outputs in order to chain task outputs together. To chain tasks outputs, use the ${task_name.output} syntax in your workflow definition.

Step 5: Get individual task outputs/results

Fetch the output/result of a specific task, by using IafWorkflowSvc.getTaskById as shown in the sample code below.

const taskId = status._tasks[0]._id; // or the specific task you want
const taskResult = await IafWorkflowSvc.getTaskById(workflowId, taskId, ctx);
console.log('Task Output:', taskResult);