Build your first workflow with Zenaton
A workflow is a series of steps written in plain javascript code using Zenaton functions to orchestrate data and events across multiple APIs and applications.
A Zenaton workflow has the following structure:
module.exports.handle = function*() {
// describe your workflow steps here // using the functions below
};
There are 2 ways to launch a workflow:
A workflow instance is launched with the parameters that you include.
[
{"id": 123, "name": "John"},
{"id": 456, "items": [
{"sku": 987, "name": "item_a"},
{"sku": 654, "name": "item_b"}
]
}
]
Note: it's a list of JSON serializable objects.
Then don't forget to reference them in your main function:
module.exports.handle = function*(user, cart)
Sending events to a running workflow
An event is composed of a _name_ and _data_, which is a list of JSON serializable objects.
[
{
"name": "updateCart",
"data": [
{
"items": [
{"sku": 45678, "name": "item_c"},
{"sku": 29517, "name": "item_d"}
]
}
]
}
]
View real-time execution data in one place:
Use our pre-cooked connectors
Use one of our pre-cooked connectors to call an API in your workflow and manage authentication and tokens.
const gmail = this.connector("gmail", "CONNECTOR_ID")
//Request payload
const params = {
headers: { ... },
query: { ... },
body: { ... }
}
//Synchronous request
const output = yield gmail.get("users/me/messages", params)
//Example for Github to list my public repositories
const output = yield github.get("/user/repos", { query: { visibility: "public" } });
Call any other 3rd Party APIs by using our generic http connector Include the HTTP client at the beginning of the workflow
const http = this.connector("http")
Send an HTTP request asynchronously and the workflow will continue running without waiting for a response.
http.post('https://httpbin.org/anything', {body: {...}})
Send an HTTP request synchronously and wait for a response before proceeding to the next step in your workflow by adding the yield
keyword
const data = yield http.get('https://httpbin.org/anything')
Add a query-string, headers and body
http.post('https://httpbin.org/anything', {
query: { foo: 'bar'}
headers: { 'content-type': 'application/json'},
body: { name: 'John'}
})
There are 2 ways to trigger processing of tasks on your server:
Within a workflow, you can connect to your own services exposing an endpoint, by using our generic http connector (link to documentation):
const http = this.connector("http")
//asynchronous call
http.post('your_own_endpoint', {body: {...}})
//synchronous call
const data = yield http.get('your_own_endpoint')
Make sure your services are reachable from the public internet and if needed, add authorization info in the header requests:
http.post('your_own_endpoint', {
query: { foo: 'bar'}
headers: { 'content-type': '...', authorization: '{ ... }'},
body: { name: 'John'}
})
Using seconds
//Wait for 5 days
yield this.wait.for(5 \* 24 \* 3600)
Or using Zenaton duration
helpers
const { duration } = require("zenaton")
//Wait for 2h40
yield this.wait.for(duration.hours(2).minutes(30))
Learn more about the duration
helpers.
const { datetime } = require("zenaton")
//Wait until next monday at 8:00
yield this.wait.until(datetime.monday().at("8:00"))
Learn more about the datetime
helper or how to setup timezones here.
You can pause a workflow and wait until an event is received.
yield this.wait.event("UserActivatedEvent").forever()
But in case the event doesn't happen, add a time limit to ensure that the workflow will complete:
const { duration } = require("zenaton")
//Wait for the UserActivatedEvent event until 5 days.
yield this.wait.event("UserActivatedEvent").for(duration.days(5))
The workflow can trigger different steps depending on the event received or the data in the event.
const event = yield this.wait.event("UserActivatedEvent").for(duration.hours(2))
if (event === null) {
// the event does not happen within 2 hours.
} else {
// the event occurs, the workflow receives its data
const \[name, data] = event
if(data.foo === 'bar') {
//do something
} else {
//do something else
}
}
Learn more about the wait function in our documentation.
Sample Projects
Start building workflows
Sign-up and run a sample project Learn more