🚀 Understanding the Shopify Section Rendering API with Code Examples
Shopify’s Section Rendering API is a powerful tool for developers looking to create fast, dynamic user experiences on storefronts. Introduced to help reduce full-page reloads, it allows you to update sections of a page via AJAX, enhancing interactivity and performance.
In this article, we’ll cover:
🧠 What is the Section Rendering API?
The Section Rendering API lets you fetch and render one or more sections on your Shopify store using AJAX requests. Instead of refreshing the whole page, you can dynamically update parts like the cart drawer, product recommendations, or filtering/sorting results.
Why Use It?
⚙️ How It Works
The basic URL format to fetch a section:
GET /?sections=section-id-1,section-id-2
You can also add query parameters like variant, collection, or others depending on the page context.
The server responds with JSON containing the rendered HTML of each requested section.
📦 Common Use Cases
🛠️ Setting Up a Section (Liquid + JavaScript Example)
Let’s say we have a section that displays a product’s price and we want to update it dynamically when a variant changes.
Step 1: Create the Section
sections/product-price.liquid
<div id="ProductPrice-{{ product.id }}">
<span class="product-price">
{{ product.selected_or_first_available_variant.price | money }}
</span>
</div>
Step 2: Include the Section on Your Product Page
product.liquid or main-product.liquid
Recommended by LinkedIn
{% section 'product-price' %}
Step 3: Write JavaScript to Fetch and Update the Section
document.querySelectorAll('input[name="id"]').forEach((radio) => {
radio.addEventListener('change', function () {
const variantId = this.value;
fetch(`/products/${productHandle}?variant=${variantId}§ions=product-price`)
.then((res) => res.json())
.then((data) => {
const updatedHTML = data['product-price'];
document.getElementById(`ProductPrice-${productId}`).outerHTML = updatedHTML;
});
});
});
Replace productHandle and productId with their actual values (from Liquid or JavaScript context).
🧪 Real-World Example: Filter Collection Without Reload
Let’s say we want to filter a collection and update only the product grid.
Step 1: Create a Section for the Grid
sections/collection-product-grid.liquid
<div id="CollectionProductGrid">
{% for product in collection.products %}
<div class="product-card">
{{ product.title }}
</div>
{% endfor %}
</div>
Step 2: JavaScript for AJAX Filtering
const filterForm = document.querySelector('#CollectionFilters');
filterForm.addEventListener('change', () => {
const formData = new FormData(filterForm);
const searchParams = new URLSearchParams(formData).toString();
fetch(`${window.location.pathname}?sections=collection-product-grid&${searchParams}`)
.then(res => res.json())
.then(data => {
document.getElementById('CollectionProductGrid').innerHTML = data['collection-product-grid'];
});
});
📝 Tips and Best Practices
🧩 Debugging Tips
🧨 Limitation
🏁 Final Thoughts
The Shopify Section Rendering API is a game-changer for creating fast, app-like experiences on Shopify stores. Whether you’re updating prices, filtering collections, or building custom carts—understanding and using this API will give your Shopify theme a serious performance boost.