The "shopify" action

Anything that Shopify allows developers to do, Mechanic lets you automate. This means that you can create, update, or delete nearly anything in your Shopify account.

The "shopify" action has three styles of usage:

  • Resourceful REST, which uses Shopify's REST admin API in a style reminiscent of ActiveResource
  • Explicit REST, which uses Shopify's REST admin API with explicitly defined values for HTTP method, url, and body
  • GraphQL, which uses Shopify's GraphQL admin API

Use resourceful REST if it's easy to do so – its job is to keep simple things simple. Use explicit REST if you need to get really specific about how your API request works. Use GraphQL when you need a feature not available in the REST API, or if it's otherwise more convenient/natural to do so.

Tip: if you're working with tags, always use GraphQL, to avoid accidentally overwriting tags added or removed by a simultaneously-running task. Learn more about tagging

Resourceful REST

Accepts an array of options, containing these elements in order:

  1. Operation
    Must be one of "create" , "update" , or "delete" .
  2. Resource specification
    When creating, use a single string (e.g. "customer" ).
    When updating or deleting, use an array (e.g. ["customer", 123] ).
  3. An object of attributes
    Only applies to creating and updating.

A task which tags a customer upon ordering might look like this:

{% action "shopify" %}
  [
    "update",
    [
      "customer",
      {{ order.customer.id | json }}
    ],
    {
      "tags": {{ order.customer.tags | add_tag: "ordered" | json }}
    }
  ]
{% endaction %}

Explicit REST

Accepts an array of options, containing these elements in order:

  1. Operation
    Must be one of "post" , "put" , or "delete" 
  2. Request path
    Literally, the path you want to request from Shopify – the entire path. For example, /admin/orders.json .
  3. An object of attributes
    Same as the resourceful options style. :)

A task which tags a customer upon ordering might look like this:

{% action "shopify" %}
  [
    "put",
    "/admin/customers/{{ order.customer.id }}.json",
    {
      "tags": {{ order.customer.tags | add_tag: "ordered" | json }}
    }
  ]
{% endaction %}

GraphQL

Accepts a valid GraphQL query.

A task which tags a customer upon ordering might look like this:

{% assign customer = shop.customers[order.customer.id] %}

{% action "shopify" %}
  mutation {
    tagsAdd(
      id: {{ customer.admin_graphql_api_id | json }}
      tags: "ordered"
    ) {
      userErrors {
        field
        message
      }
    }
  }
{% endaction %}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.