Shopify
Last updated
Was this helpful?
Last updated
Was this helpful?
The Shopify action sends requests to the Shopify admin API. It supports both REST and GraphQL requests.
Important Notice
Shopify is deprecating the Shopify Admin REST API which the Mechanic REST objects depend on. The first round of deprecations involve the product and variant endpoints. Read about the deprecation here and here. Use the GraphQL going forward. The product and variant objects will cease to work on on Feb 1, 2025 due to the changes being made by Shopify. Shopify will phase out the REST API completely over time, you can read more about this here.
All of our library tasks will be ported to use GraphQL only, which will provide a model for how you can update your custom tasks. You'll be able to update your non-customized library tasks with a click of a button Please see these guides for migrating your custom tasks to GraphQL.
This action has several usage styles, each with a different set of constraints on action options.
This usage style invokes the Shopify GraphQL Admin API. In this style, a single GraphQL query string is supplied as the action options. The action tag has specific support for this action type, allowing this string to be provided as the contents of an action block.
{% action "shopify" %}
mutation {
customerCreate(
input: {
email: "test@example.com"
}
) {
customer {
id
}
userErrors {
field
message
}
}
}
{% endaction %}
This usage style invokes the Shopify GraphQL Admin API, and supports combining GraphQL queries with GraphQL variables. This can be useful for re-using queries with multiple inputs, and is critical when dealing with very large pieces of input. Because GraphQL queries (excluding whitespace) are limited in length to 50k characters, GraphQL variables can be used in cases when large inputs (like Base64-encoded images) need to be submitted.
Option
Description
query
Required; a string containing a GraphQL query
variables
Required; a JSON object mapping variable names to values
{% capture query %}
mutation DeleteProduct($productId: ID!) {
productDelete(
input: {
id: $productId
}
) {
userErrors {
field
message
}
}
}
{% endcapture %}
{% action "shopify" %}
{
"query": {{ query | json }},
"variables": {
"productId": "gid://shopify/Product/1234567890"
}
}
{% endaction %}
This example shows how the query and variables may be built up separately, and provided to the action using concise tag syntax.
{% assign metafield_owner_id = "gid://shopify/Customer/507332001849" %}
{% assign metafield_value = hash %}
{% assign metafield_value["foo"] = "bar" %}
{% capture query %}
mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
key
namespace
value
createdAt
updatedAt
}
userErrors {
field
message
code
}
}
}
{% endcapture %}
{% assign metafield = hash %}
{% assign metafield["ownerId"] = metafield_owner_id %}
{% assign metafield["namespace"] = "demo" %}
{% assign metafield["key"] = "demo" %}
{% assign metafield["type"] = "json" %}
{% assign metafield["value"] = metafield_value | json %}
{% assign metafields = array %}
{% assign metafields = metafields | push: metafield %}
{% assign variables = hash %}
{% assign variables["metafields"] = metafields %}
{% action "shopify" query: query, variables: variables %}
Important Notice
Shopify is deprecating the Shopify Admin REST API which the Mechanic REST objects depend on. The first round of deprecations involve the product and variant endpoints. Read about the deprecation here and here. Use the GraphQL going forward. The product and variant objects will cease to work on on Feb 1, 2025 due to the changes being made by Shopify. Shopify will phase out the REST API completely over time, you can read more about this here.
This usage style invokes the Shopify REST Admin API. It accepts an array of option values, containing these elements in order:
Operation Must be one of "create"
, "update"
, or "delete"
.
Resource specification When creating, use a single string (e.g. "customer"
). When updating or deleting, use an array (e.g. ["customer", 123]
).
An object of attributes Only applies to creating and updating.
This example creates a (minimal) customer record.
{% action "shopify" %}
[
"create",
"customer",
{
"email": "test@example.com"
}
]
{% endaction %}
This example appends a line to the order note (assuming a task subscription to shopify/orders/create).
{% action "shopify" %}
[
"update",
[
"order",
{{ order.id | json }}
],
{
"note": {{ order.note | append: newline | append: newline | append: "We're adding a note! πͺ" | strip | json }}
}
]
{% endaction %}
This example deletes a product, having a certain ID.
{% action "shopify" %}
[
"delete",
["product", 4814813560893]
]
{% endaction %}
Important Notice
Shopify is deprecating the Shopify Admin REST API which the Mechanic REST objects depend on. The first round of deprecations involve the product and variant endpoints. Read about the deprecation here and here. Use the GraphQL going forward. The product and variant objects will cease to work on on Feb 1, 2025 due to the changes being made by Shopify. Shopify will phase out the REST API completely over time, you can read more about this here.
This usage style invokes Shopify REST Admin API. It accepts an array of option values, containing these elements in order:
Operation Must be one of "get"
, "post"
, "put"
, or "delete"
Request path The entire, literal request path to use, including the requested API version β e.g. "/admin/api/2020-01/orders.json"
A JSON object of attributes In general, this means a wrapper object whose key is named after the current resource type, and whose value is the same set of data that would be used in the resourceful style
This example creates a (minimal) customer record.
{% action "shopify" %}
[
"post",
"/admin/api/2020-01/customers.json",
{
"customer": {
"email": "test@example.com"
}
}
]
{% endaction %}
This example appends a line to the order note (assuming a task subscription to shopify/orders/create).
{% action "shopify" %}
[
"put",
"/admin/api/2020-01/orders/{{ order.id }}.json",
{
"order": {
"note": {{ order.note | append: newline | append: newline | append: "We're adding a note! πͺ" | strip | json }}
}
}
]
{% endaction %}
This example deletes a product, having a certain ID.
{% action "shopify" %}
[
"delete",
"/admin/api/2020-01/products/4814813724733.json"
]
{% endaction %}
All of our library tasks will be ported to use GraphQL only, which will provide a model for how you can update your custom tasks. You'll be able to update your non-customized library tasks with a click of a button Please see these guides for migrating your custom tasks to GraphQL.
All of our library tasks will be ported to use GraphQL only, which will provide a model for how you can update your custom tasks. You'll be able to update your non-customized library tasks with a click of a button Please see these guides for migrating your custom tasks to GraphQL.