Using Zenaton Node.js SDK


If your application is using node.js, you can us the Zenaton SDK to dispatch tasks and workflows and send events to running workflows. You will need to add it to the package.json file of your application:

{
  "dependencies": {
    "zenaton": "^0.7.3"
  }
}

Then install the library:

npm install

In your application code, you can now set up a Zenaton Client with your credentials:

const { Client } = require("zenaton");
const zenaton = new Client("<app id>", "<api token>", "<app env>");

# Dispatch a task

zenaton.run.task("<task name>", ...<task input>);

# Dispatch a workflow

zenaton.run.workflow("<workflow name>", ...<workflow input>);

# Schedule a task

zenaton.schedule("<cron expression>").task("<task name>", ...<task input>);

"<cron expression>" format is described here.

# Schedule a workflow

zenaton.schedule("<cron expression>").workflow("<workflow name>", ...<workflow input>);

"<cron expression>" format is described here.

# Actions to workflows

Before being able to interact with a workflow, you need a way to identify it. Currently, there are 3 ways to select workflows:

Type selector value
by id zenaton.select.workflow().withId("<id>")
by name zenaton.select.workflow("<name>")
by name and tag zenaton.select.workflow("<name>").withTag("<tag>")

This selector value should be used in requests below. For example:

const selector = zenaton.select.workflow("<name>");

# Send an event to workflows

selector.send("<event name>", ...<event data>);

# Pause running workflows

selector.pause();

# Resume paused workflows

selector.resume();

# Terminate running workflows

selector.terminate();

# Output

For those requests, the result is an object, such as:

{
  id: '<uuid>'
}

where:

  • id is a unique uuid referencing your request. You may want to store this value to be able to reference it later

If you want to get the id or catch an error, you should do:

run.workflow("myWorkflow")
  .then( function ( result ) { ... } );
  .catch( function (err) { ... } )

or within an async function:

await run.workflow("myWorkflow");