# Workflows Beta

Stelace Workflows are the fastest way to run automated logic empowering Events, in a serverless fashion.

Events trigger your appropriate Workflows, meaning JavaScript logic is executed on our servers, as it would be with a service like Zapier or AWS Lambda, but natively leveraging all Stelace API endpoints and external systems as well.

Here is a working example taken from Heroes starter kit. Stelace team gets notified via Slack in real-time when a visitor flags any hero:

Heroes Demo Hero flag notification on Slack

// Sample Workflow object
{
  name: "flagVisitorHero",
  description: "Flags hero using custom Event",
  event: "flag_hero", // custom Event name triggering this Workflow
  context: "slackVariables",
  computed: {
    // Using simple JavaScript expressions and lodash
    shouldFlag: "!asset.platformData.createdByStelace && _.get(asset, 'metadata.flags', 0) > 0"
  },
  run: [
    {
      name: "flag",
      description: "Deactivates Hero Asset if already flagged before.",
      endpointMethod: "PATCH",
      endpointUri: "/assets/${asset.id}",
      endpointPayload: {
        active: "computed.shouldFlag ? false : undefined",
        metadata: {
          flags: "_.get(asset, 'metadata.flags', 0) + 1"
        }
      }
    },
    {
      name: "flagSlackNotif",
      description: "Notify Team on Slack when some hero is flagged.",
      endpointMethod: "POST",
      skip: "!env.SLACK_WEBHOOK_URL", // secured environment variables set in Stelace Dashboard
      endpointUri: "${env.SLACK_WEBHOOK_URL}",
      endpointPayload: {
        text: "`${asset.name} [${asset.id}] flagged on <heroes.demo.stelace.com|live demo>.`"
      }
    }
  ]
}

For more details about Workflows, you can have a look at our API reference API.

# Environment variables

Workflows can have environment variables organized in contexts to safely inject secrets and other values during execution without hard-coding them in Workflow run object.

In the example above, we use a context named slackVariables holding SLACK_WEBHOOK_URL variable. Environment variables loaded from context can be accessed using env. prefix in Workflows.

To set a context to be used in Workflows, private Configuration API API is the way to go. This requires a secret API key.