Back to Top

libraries > Shopify.js

Shopify

Interact with the Shopify Admin API.

Shopify.get(path[, options, connectionInfo])

Make a GET request to a Shopify site.

Parameters

Name Type Description
path string
options Options

Additional configuration for Shopify calls

Optional
connectionInfo ConnectionInfo

If you would like to connect to a separate Shopify website that Mesa is not installed on, create a Private App and include a connectionInfo object.

Optional

Returns

object

Shopify.post(path, data[, options, connectionInfo])

Make a POST request to a Shopify site.

By default Shopify calls will auto-wrap any outgoing JSON data, eg. Shopify.post('/admin/products.json', data) will result in { "product": { data } } use options.skipJsonWrap=true to override this behavior

Parameters

Name Type Description
path string
data object
options Options

Additional configuration for Shopify calls

Optional
connectionInfo ConnectionInfo

If you would like to connect to a separate Shopify website that Mesa is not installed on, create a Private App and include a connectionInfo object.

Optional

Returns

object

Shopify.put(path, data[, options, connectionInfo])

Make a PUT request to a Shopify site.

By default Shopify calls will auto-wrap any outgoing JSON data, eg. Shopify.put('/admin/products.json', data) will result in { "product": { data } } use options.skipJsonWrap=true to override this behavior

Parameters

Name Type Description
path string
data object
options Options

Additional configuration for Shopify calls

Optional
connectionInfo ConnectionInfo

If you would like to connect to a separate Shopify website that Mesa is not installed on, create a Private App and include a connectionInfo object.

Optional

Returns

object

Shopify.patch(path, data[, options, connectionInfo])

Make a PATCH request to a Shopify site.

By default Shopify calls will auto-wrap any outgoing JSON data, eg. Shopify.patch('/admin/products.json', data) will result in { "product": { data } } use options.skipJsonWrap=true to override this behavior

Parameters

Name Type Description
path string
data object
options Options

Additional configuration for Shopify calls

Optional
connectionInfo ConnectionInfo

If you would like to connect to a separate Shopify website that Mesa is not installed on, create a Private App and include a connectionInfo object.

Optional

Returns

object

Shopify.delete(path[, options, connectionInfo])

Make a DELETE request to a Shopify site.

By default Shopify calls will auto-wrap any outgoing JSON data, eg. Shopify.post('/admin/products.json', data) will result in { "product": { data } } use options.skipJsonWrap=true to override this behavior

Parameters

Name Type Description
path string
options Options

Additional configuration for Shopify calls

Optional
connectionInfo ConnectionInfo

If you would like to connect to a separate Shopify website that Mesa is not installed on, create a Private App and include a connectionInfo object.

Optional

Returns

object

getAllProducts([query={}, limit=250])

Make consecutive calls to Shopify in order to retrieve all products. https://help.shopify.com/en/api/reference/products/product

Parameters

Name Type Description
query={} object

Parameters to append to the Shopify querystring.

Optional
limit=250 number

Result count for Shopify query.

Optional

Examples

// returns array of all products
const Shopify = require('vendor/Shopify.js');
Shopify.getAllProducts();

Returns

array

appendToArray([data], The)

Update a value if it already exists, or append it to the array if it does not exist. The example routine below should be every time we are updating tags or note_attributes to ensure that multiple Automations will work nicely with each other and not overwrite values set in other Automations.

Parameters

Name Type Description
data array

An array of values that you would like to append a value to

Optional
The string object

value to append to the array. For tags, this would be a string. For note_attributes, this would be an object.

Examples

Mesa.log.debug('Calling shopify to get the latest order note_attributes');
const order = Shopify.get(`admin/orders/${payload.id}.json`);
let noteAttributes = order.order.note_attributes;
noteAttributes = Shopify.appendToArray(noteAttributes, {
   name: 'dob',
   value: 'Jan 1 2000',
});

Returns

array

getVariantInventoryData(variantId)

Get inventory location information for a Shopify variant. https://help.shopify.com/en/api/reference/inventory/inventorylevel

Parameters

Name Type Description
variantId number

Shopify variant id.

Examples

// returns { inventory_item_id: {number}, [ { inventory_levels: { inventory_item_id: {number}, location_id: {number}, available: {number}, updated_at: {string} } ] }
const Shopify = require('vendor/Shopify.js');
Shopify.getVariantInventoryData(123456);

Returns

InventoryData

buildVariantInventoryUpdate(variantId, inventoryCount[, adjust=true, fulfillableAlter])

Build InventoryLevel update information for a Shopify variant. https://help.shopify.com/en/api/reference/inventory/inventorylevel

Parameters

Name Type Description
variantId number

Shopify variant id.

inventoryCount number

Value to set as inventory.

adjust=true bool

Switch for available_adjustment vs available.

Optional
fulfillableAlter fulfillableAlter

Callback function allows fulfillment location to be altered.

Optional

Examples

// note string for location_id, as it is required by Shopify API post
// returns { inventory_item_id: {number}, location_id: {string}, available|available_adjustment: {number} } ] }
const Shopify = require('vendor/Shopify.js');
Shopify.buildVariantInventoryUpdate(123456, 10, true|false);

Returns

InventoryUpdateData

buildFulfillmentTrackingUpdate(orderId, variantId, fulfillmentProps[, fulfillableAlter])

Build Fulfillment information for a Shopify variant. fulfillmentProps.location_id is added by this function, so do not include. https://help.shopify.com/en/api/reference/shipping-and-fulfillment/fulfillment#update

Parameters

Name Type Description
orderId number

Shopify order id.

variantId number

Shopify variant id.

fulfillmentProps FulfillmentTrackingProps

properties to attach to the fulfillment tracking.

fulfillableAlter fulfillableAlter

Callback function allows fulfillment location to be altered.

Optional

Examples

// returns { order_id: {number}, fulfillment: {  location_id: {string}, line_items: [{ id: {number} }], notify_customer: {bool}, tracking_company: {string}, tracking_numbers: [{string}], tracking_urls: [{string}]  } }
const Shopify = require('vendor/Shopify.js');
Shopify.buildFulfillmentTrackingUpdate(123456, 123456, { line_items: [{ id: 123456 }], notify_customer: true, tracking_company: 'fed ex', tracking_numbers: ['123456'] });

Returns

FulfillmentTrackingUpdateData

{Object} Options

options parameter for Shopify calls

By default Shopify calls will auto-wrap any outgoing JSON data, eg. Shopify.post('/admin/products.json', data) will result in { "product": { data } } use skipJsonWrap=true to override this behavior

Properties

Name Type Description
query object

Parameters to append to the querystring.

Optional
headers object

Headers to send to the request.

Optional
debug=false bool

Log request information and response headers.

Optional
skipJsonWrap=false bool

Skip the named auto wrapping of outgoing data

Optional

{Object} ConnectionInfo

connectionInfo parameter for Shopify calls

Properties

Name Type Description
host string

Hostname of external Shopify site, including .myshopify.com. For example: mysite.myshopify.com.

username string

The Shopify private app API Key.

password string

The Shopify private app Password.

{Object} FulfillableLocation

Fulfillable location shape returned by fulfillableAlter https://help.shopify.com/en/api/reference/inventory/location

Parameters

Name Type Description
location_id number

The ID of the location that the inventory level belongs to.

{Function} fulfillableAlter(inventoryLevels)

Callback allows buildVariantInventoryUpdate fulfillment location to be altered. https://help.shopify.com/en/api/reference/inventory/inventorylevel

Parameters

Name Type Description
inventoryLevels Array.<Object>

Inventory levels for the variant at different locations.

inventoryLevels.inventory_item_id number

Inventory item id for variant

inventoryLevels.location_id number

Location id

inventoryLevels.available number

Inventory count

inventoryLevels.updated_at string

Last updated

Returns

FulfillableLocation

fulfillableLocation

{Object} InventoryData

Return from getVariantInventoryData.

Properties

Name Type Description
inventory_item_id number

Variant inventory item id

inventory_levels Array.<Object>

Inventory levels for the variant at different locations.

inventory_levels.inventory_item_id number

Inventory item id for variant

inventory_levels.location_id number

Location id

inventory_levels.available number

Inventory count

inventory_levels.updated_at string

Last updated

{Object} InventoryUpdateData

Return from buildVariantInventoryUpdate. Will either have available or available_adjustment depending on adjust param. https://help.shopify.com/en/api/reference/inventory/inventorylevel

Properties

Name Type Description
inventory_item_id number

Variant inventory item id

location_id string

Location id for update

available_adjustment number

Value to update if adjust=true, for inventory_levels/adjust.json

available number

Value to set if adjust=false, for inventory_levels/set.json

{Object} FulfillmentTrackingProps

Fulfillment properties for update. location_id is added by the buildFulfillmentTrackingUpdate function, so do not include. https://help.shopify.com/en/api/reference/shipping-and-fulfillment/fulfillment#update

Properties

Name Type Description
location_id string

Location id !ADDED BY buildFulfillmentTrackingUpdate!

line_items Array.<Object>

Fulfillment line items

Optional
line_items.id number

Fulfillment line item ID

notify_customer bool

Whether the customer should be notified.

Optional
tracking_company string

The name of the tracking company.

Optional
tracking_numbers array

A list of tracking numbers, provided by the shipping company.

Optional
tracking_urls array

The URLs of tracking pages for the fulfillment.

Optional

{Object} FulfillmentTrackingUpdateData

Properties

Name Type Description
order_id number

Shopify order id.

fulfillment FulfillmentTrackingProps

the fulfillment properties.