Script Specification

The most basic Mesa Script is exports a class with a script() method. The code is executed in a V8 environment and can be written in standard Javascript or ES6. The class can contain additional methods or variables.

Parameters

During runtime, the script() method will be called and passed two parameters, payload and context:

Name

Name Type Description
payload object The payload data
context object Contains data relative to the current task that is running.
context.task object Metadata about the current Task that is executing. Available on: All Inputs and Outputs.
context.shop object Metadata about the Shopify store that the Task is running on
context.input object Metadata about the Input that triggered the Task. Available on: All Inputs. Optional
context.output object Metadata about the Output that triggered the Task. Available on: All Outputs. Optional
context.headers object The headers passed from the webhook. Available on: Shopify Webhook and JSON Webhook Inputs. Optional
context.filename object The name of the file that was read or is being saved on the FTP ServerAvailable on: FTP Inputs and FTP Outputs. Optional  

Basic Mesa Script

const Mesa = require('vendor/Mesa.js');

/**
 * A Mesa Script exports a class with a script() method.
 */
module.exports = new class {

  /**
   * Mesa Script
   *
   * @param {object} payload The payload data
   * @param {object} context Additional context about this task
   */
  script = (payload, context) => {

    // Your code goes here
  }
}

Mesa Scripts can call any of the Mesa SDK methods or methods from our libraries.

What goes in each script?

Input Scripts

  • Input Mesa Scripts should generally be pretty slim.
  • In the case of webhook inputs, you can generally use the default utility scripts in-output-send.js or in-push-virtual-output.js which just pass the payload through to the output.
  • FTP inputs oftentimes decode the CSV or XML payload, and loops through each each record, calling Mesa.output.send(). See the in-ftp-product-import.js example.

Output Scripts

  • Output Mesa Scripts contain most of the logic, including mapping and third party APIs.
  • Virtual Output Scripts should typically validate that there are enough records to send, and loop through the payload, calling Mesa.output.send().
  • If an output is updating an attribute that is an array, you should make a Shopify.get() call before the updating the array value to ensure that multiple Automations interacting with the same webhook can avoid clashing.

Naming Conventions

Inputs and Outputs

The Input and Output names should describe what the inputs and outputs do. Examples:
Shopify Order Webhook
Update Shopify Order
Add Shopify Order Tag
Add Weather Forecast

Keys lowercase alpha-numeric characters with dashes. They should be prefixed by their type. Examples:
in-shopify-orders-webhook
out-ftp-save-vo
out-ftp-save

Typically every in- key should have a corresponding out- key. Virtual Output keys should have a -vo suffix.

Script file names

All lowercase alpha-numeric characters with dashes. They should be prefixed by their type and generally match their input/output. Examples:
in-shopify-orders-webhook.js
out-ftp-save.js

Secrets and Storage

All lowercase alpha-numeric characters with dashes. Storage items can have an optional .json or .liquid suffix.

Timezones

All system timezones are UTC. If you need to get dates in a different timezone, use the Mesa.date.setTimezone(timezone) method.

Unsupported nodejs methods

Scripts are run in a stock V8 environment. This means that some common NodeJS methods are not available:
request(): Alternative: Mesa.request.*() Documentation.
console.log(): Alternative: Mesa.log.*() Documentation.
DOM manipulation methods: Alternative: Mesa.xml.decode() Documentation.