How to build a custom JSON API in Shopify with Liquid | ShapeKit

This is a little trick I’ve been using for a few years with Shopify when I want to build really interesting AJAX experiences in a theme.

Many people will tell you that accessing URL parameters is impossible, including top people at Shopify:

https://ecommerce.shopify.com/c/ecommerce-design/t/passing-query-parameters-17259

https://ecommerce.shopify.com/c/ecommerce-design/t/url-parameters-in-liquid-171887

However, it is possible! I’ve been using a technique to capture header information that is the same as Jason over @ FreakDesign but since his code is a bit cleaner with comments, I’ve opted to use his version for this tutorial. PS, if you’ve never seen his GitHub repo, check it out, there is a treasure trove of information.

Pros

Cons

Step #1 : Create a new page template in your theme

Go into the HTML/CSS Editor and click add a new template.

Paste in this code.

Step #2 : Create a new page and assign it the new template

In Shopify, go to Online Store > Pages, create a new page, and assign it the template you created in step 1. The page handle becomes part of the URL you use to call the API.

Step #3 : Hit that API!

Your URL will be something similar to this. Now you can replace the “test_tag” value with your tag and get all products and variants for that tag.

/pages/test-api?tag=test_tag&cache=false

Why would I ever need this?

You can do something similar to what Amazon does when loading recommending product and build other unique AJAX experiences.