• Solutions
  • Resources
book-icon
Featured Resources
The Definitive Guide to 3PL’s-min
eBook
The definitive guide to 3PL
Get the information and insights you need to make better-informed decisions for your business.
Download Now
Saysh Shoes-1
Case Study
Saysh
When Saysh saw a 1000% increase in order volume for their holiday sale, they successfully fulfilled every order—without overselling.
Read Story
integration-issues
Whitepaper
Online Customer Shopping Habits
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.
Download Now
How-To

How to build a custom JSON API in Shopify with Liquid

Learn how to build a custom JSON API in Shopify with Liquid - a trick for when you want to build really interesting AJAX experiences in a theme.


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

  • Create exactly the JavaScript objects you need for great AJAX experiences.
    • This allows you to build a recommendations engine for example that gets loaded AFTER initial pageload to keep your site fast.
  • No need for 3rd party server to interact with Shopify APIs

Cons

  • It is invalid JSON (RFC 4627)
    • You can’t set the content type to application/json.[Code 28, Structure 0]
    • You can’t set encoding to UTF-8, UTF-16 or UTF-32.[Code 29, Structure 0]
  • It is a “hack” that Shopify can remove at anytime.
    • However this has been available for years already.

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

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.

Similar posts