MESA Docs
MESA Homepage
  • Welcome to MESA
    • Installing MESA
    • Dashboard
    • Core Concepts
    • Getting Help
  • Templates
    • Template Library
    • Installing & Editing
  • Workflow Builder
    • Triggers
    • Actions
    • Fields
      • Variables
      • Formatting Variables
      • Liquid Templating
    • Testing
  • Workflow Activity
    • Tasks
      • Troubleshooting
      • Replay
    • Logs
    • Time Travel
  • Best Practices
    • Set Titles & Descriptions
    • Track Time Saved
    • Enable Failure Notifications
    • Avoid Infinite Loops
  • Going Further
    • Plans & Billing
    • Notifications
    • Credentials
    • Understanding the Queue
    • Export & Import Workflows
    • Platform Thresholds & Limits
  • Built-in Tools
    • Activity Log
    • AI
    • API
    • Approval
    • MCP
    • Custom Code
      • Libraries
        • MESA SDK
        • Filter
        • Loop
        • Transform
        • oAuth
        • Shopify
        • ShopifyGraphql
      • FAQ
    • Data
    • Delay
    • Email
    • Filter
    • Form
      • Configure
      • Going Further
      • Technical Notes
    • FTP
    • Image
    • Loop
    • Package Tracking
    • Paths
    • Schedule
    • Scraper
    • SMS
    • Transform
    • Virtual Output
    • Weather
    • Web Request
    • Webhook
  • Apps
    • Airtable
    • Amazon S3
    • Asana
    • Blog Studio
    • ChannelApe
    • Claude
    • ChatGPT
    • Dall-E 2
    • Delighted
    • Digital Humani
    • Discord
    • DocuSign
    • Dropbox
    • Etsy
    • Facebook
    • Fera.ai
    • Gatsby
    • Gmail
      • Send Email
    • Google Analytics 4
    • Google Analytics UA
    • Google Calendar
    • Google Drive
    • Google Forms
    • Google Sheets
      • Add Row
      • Query Rows
      • Update Row
      • Row Created
      • Row Updated (Advanced)
      • Creating New Spreadsheets
      • Technical Notes
    • Gorgias
    • Govalo
    • Help Scout
    • Hubspot
    • Infinite Options
    • Intercom
    • IPDetective
    • Judge.me
    • Katana
    • Klaviyo
      • API Deprecation
    • Loop Returns
    • Loyalty Lion
    • Mailchimp
    • Mailgun
    • Mandrill
    • Mantle
    • Notion
    • Odoo
    • Omnisend
    • Page Studio
    • Pinterest
    • Printful
    • PrintNode
    • QuickBooks
    • Rebuy
    • Recharge
    • Remove.bg
    • Returnly
    • Rivo
    • Salesforce
    • Segment
    • Sendlane
    • Shippo
    • ShipStation
    • Shopify
      • Technical Notes
        • Configuring Your Payload
        • Modifying and Filtering Get Lists
        • Parameters
        • Error Codes
        • Accuracy of orders_count Variable
        • Accuracy of Count Products Action
        • Using the Gift Cards API
        • How to Find a Specific Record in the Testing Interface
        • Order, Customer, and Product Updated Trigger Frequency
    • Shopify Flow
    • Shopify Partner
    • Shopify Plus
    • Shopify Retail POS
    • Skio Subscriptions
    • Slack
    • Smartrr
    • Smile.io
    • Square
      • Updating Inventory
      • Frequently Asked Questions
    • Stamped.io
    • Stripe
    • Thanks.io
    • TikTok
    • Tracktor
    • Twilio
    • Uploadery
    • Wonderment
    • Xero
    • Yotpo
    • Yotpo Loyalty
  • For developers
    • Admin API
    • Command Line Interface
    • Embedding templates
  • Frequently asked questions
    • Why isn't the log search returning logs with the search I entered?
    • Is it possible to handle errors or retry steps?
    • How do I handle a failed task?
    • Does MESA auto-save workflows?
    • Can I Use Apps That Don't Have a MESA Connector?
    • Why is my workflow action adding the same data each time it occurs?
    • How do I cancel automations that are already in progress?
    • How do I cancel my MESA subscription or 7-day trial?
    • Can you import code from npm in custom code?
    • Do you have a Slack Community?
Powered by GitBook
On this page
  • Configure
  • To open the code editor
  • Anatomy of a Custom Code step
  • Using Libraries
  • Going Further
  • Logging information
  • Throwing errors
  • Interacting with external APIs
  • Technical Notes
  • Timezones
  • Unsupported NodeJS methods

Was this helpful?

  1. Built-in Tools

Custom Code

PreviousMCPNextLibraries

Last updated 2 months ago

Was this helpful?

The Custom Code tool allows you to add complex logic to a workflow that might otherwise be difficult with MESA's built-in tools. You can create custom integrations to third-party services, even those that MESA does not yet integrate, and code custom business logic using JavaScript.

Configure

To open the code editor

Click the </> Edit Code button to edit your custom code.

Anatomy of a Custom Code step

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

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

  /**
   * MESA Script
   *
   * @param {object} prevResponse The response from the previous step
   * @param {object} context Additional context about this task
   */
  script = (prevResponse, context) => {

    // Retrieve the Variables Available to this step
    const vars = context.steps;

    // Add your custom code here
    let response = {}

    // Call the next step in this workflow
    // response will be the Variables Available from this step
    Mesa.output.next(response);
  }

}

Code is executed in a V8 environment and can be written in standard JavaScript or ES6. When this step is run, the script() method will be called with two parameters: prevResponse and context. In order for your workflow to continue its subsequent steps, be sure to include a call to Mesa.output.next(response); somewhere within your script() method.

The context object

Property
Type
Description

steps

Object

An object containing the variables for each step that has been run so far, named with the step's unique key.

For example, to access a step with a key of "shopify" use: context.steps['shopify'].

trigger

This object is the configuration of the current step being run.

task

This object is information about the current task's execution.

The Trigger object

Property
Type
Description

metadata

Object

The values entered in configuration fields, with variable replacements.

raw_metadata

Object

The values entered in configuration fields, without variable replacements.

fields

Object

An object of the configuration fields from the UI.

The Task object

Property
Type
Description

context.headers

Object

A key-value object of the HTTP headers passed to this task.

is_test

Object

Is the current task being run as a test?

Using Libraries

Going Further

Logging information

Throwing errors

Throw a JavaScript error to halt the execution of your workflow:

throw new Error("your error description")

In the User Interface, this will look like:

Interacting with external APIs

Technical Notes

Timezones

Unsupported NodeJS methods

Scripts are run in a stock V8 environment. This means that some common NodeJS methods are not available:

  • async or await: All Mesa.request.*() methods are synchronous, so async and await are not necessary.

Libraries offer built-in functionality that extends the power of your code. The is included by default and can be imported as needed.

To debug your script, it can be helpful to log data at a particular step in your code. This can be done by calling . This is equivalent to console.log() in browser-based JavaScript. When the workflow runs, the info will be available in your workflow's .

Use to make HTTP requests to external APIs. This is the equivalent to fetch() in browser-based JavaScript.

All system timezones use your Shopify Store's timezone. You can view and update your timezone from the Shopify Dashboard: Admin > Settings > General, under "Standards and formats". If you need to get dates in a different timezone, use the method.

request(): Alternative: Mesa.request.*() .

console.log(): Alternative: Mesa.log.*() .

DOM manipulation methods: Alternative: Mesa.xml.decode() .

MESA SDK library
other libraries
Trigger
Task
Logs tab
Mesa.log.info()
Mesa.request.get()
Mesa.date.setTimezone(timezone)
Documentation
Documentation
Documentation