Shopify API Request

In Arigato Automation, the "Send a Shopify API request" action allows you to send any type of HTTP request to the Shopify REST API.

The Shopify REST API allows you add, update, get, and delete nearly any type of Shopify resource. Each type of resource and request method supports various options and filters.

It is recommended that you are familiar with how to make Shopify API requests to use this action type.

View Shopify REST API Documentation

Action Configuration

 Override Action Label

By default the action label will show the method and path that is entered:

Screenshot_2020-03-24_16.25.10.png

You can override the label to help describe what your custom API request is actually for:

Screenshot_2020-03-24_16.25.28.png

HTTP Method

This is the method required to make the HTTP call to the Shopify API. Typically:

  • GET: Get one or more pieces of data.
  • PUT: Update an existing piece of data.
  • POST: Create a new piece of data.
  • DELETE: Delete an existing piece of data.

API Path

The Shopify API Request action type will automatically build most of the URL and required HTTP headers in order to make successful API requests. The API Path option starts with the resource type, such as:

  • customers
  • customers/{customer.id}
  • orders
  • products/{product.id}/variants
  • locations

You do not need a starting slash, https, or query parameters. Query parameters are talked about below.

Data (format as JSON)

Data that needs to be passed to the API request. If the request uses the GET method, the JSON is passed to the request as typical query parameters.

Example of fetching a product by handle and returning only specific fields:

Screenshot_2020-03-24_16.36.52.png

Example of creating a new product and using some product tokens that we have available:

Screenshot_2020-03-24_16.38.44.png

 

Paginated GET Requests

If you want to fetch more than 250 items in a GET request you will need to specify the limit in the Data field. A maximum of 2,500 items can be fetched at a time (this performs 10 API requests). If a limit is set over 2,500 you will get an error. Example:

{"limit": 1000}

Please note that each API request will count towards your Shopify API call limit. Performing too many API calls in a short period may result in errors.

Handling the API Response

Depending on what API call you're making you can handle the response in two different ways. If the request you're making returns one or more Shopify resources you can use the " Run the response object(s) through a workflow" option. With this option selected you must select the object type that is being returned in the response, and you can then send that data to a subworkflow.

If you select the option " Run the response JSON through a workflow", the raw JSON response is passed to a unique HTTP Response workflow.

Option: Run the response JSON through a workflow

With this option selected a new field called "Additional Data (format as JSON)" is added which allows you to pass additional data to the subworkflow. Essentially, this added data is merged with the API response and passed to the HTTP Response workflow.

Let's take a look at a real example...

Example: Track a Customer's Order Total Average

For this example workflow we want to update a customer's order total average whenever a new order is paid.

1. Create a new workflow that runs on Order / Paid.

2. Add a Shopify API Request action.

3. Make a GET request to fetch all orders for the customer:

customers/{{ order.customer.id }}/orders

4. We only need the total_price field from the order, so we can add that to the Data JSON setting:

{<br>  "fields":"total_price"<br>}

5. What do you want to do with the response? We want to "Run the response JSON through a workflow".

6. In the  Additional Data field we want to pass the customer_id to the subworkflow. If we don't pass the information into the subworkflow we won't know which customer the order data is associated with.

{<br>  "customer_id": "{{ order.customer.id }}"<br>}

7. We can then click the "Create New Workflow" button to begin setting up the HTTP Response Workflow.

Screenshot_2020-03-24_16.53.06.png

8. In the HTTP Response workflow we're going to add a couple of conditions. First, let's make sure the HTTP Request was successful, so we'll add a condition that HTTP Response / Success is True.

Screenshot_2020-03-24_17.30.26.png

9. Next, we want to make sure we got a value back for under "orders.total_price". So we can add a condition based on a value in the response JSON object:

Screenshot_2020-03-24_16.56.15.png

* View additional documentation on the HTTP Response Workflow.

10. Next, we want to set a customer metafield value based on the order average. So we'll add an action "Set a metafield for any resource."

In there we'll set the following:

Screenshot_2020-03-24_17.05.04.png

Notice that the customer ID is coming from the HTTP Response JSON in:

{{ http_response.json.customer_id }}

And the value we are setting performs a loop over the orders to add up the total price and then calculate the average price of the orders.

{% set orders_total = 0 %}
{% for order in http_response.json.orders %}
 {% set orders_total = orders_total + order.total_price %}
{% endfor %}
{% set orders_count = http_response.json.orders | length %}
{{ (orders_total / orders_count) | number_format(2) }}

And that's it! We've setup a workflow to perform a custom API request, fetch a customer's orders, calculate their average order total, and set a metafield value for it.

For more information about the HTTP Response workflow type, including information about conditions and tokens, view the HTTP Response Workflow documentation.

JSON Encoding Correctly

When sending data to Shopify formatted as JSON, some types of fields will need to be JSON-encoded to ensure they don't cause an issue.

For example, when creating a new product with the product description (body_html) filled in, we use two filters on the token, raw and to_json.

The raw filter will ensure our HTML is not encoded before sending it. It's the difference between sending the raw string <br> or the encoded string &lt;br&gt;.

The to_json filter ensures that the contents of the string are safely encoded for HTML use. For example, if our body_html token had "quotes" in it, that would cause an error with our value. By using the to_json filter the "quotes" are correctly encoded to \"quotes\".

In our boolean field, we use the to_json filter to ensure that the token renders the correct true or false JSON value.

Also, notice that in our JSON the keys and values have quotes around them. Integer and boolean values should not have quotes around them.

{
  "product": { 
    "title": "Copy of - {{ product.title }}",
    "body_html": "{{product.body_html | raw | to_json }}",
    "boolean_field": {{ product.boolean_field | to_json }}
  }
}