How to get the template ID from a Shopify webpage for programatic use of the Section Rendering API

Shopify's Section Rendering API is powerful, but it requires the template ID, which isn't accessible through any public API.

Rob Johnson

By Rob Johnson

Fri Jan 12 2024


Shopify has a handy Section Rendering API where you can pass the following parameters on a live page URL to grab the HTML for certain sections. This can be helpful for rendering the same section in another app or custom webpage elsewhere.


An example of an absolute URL is:`.

The output from that endpoint will be a json object like this:

  "main-password-header": "SECTION HTML (ESCAPED) RETURNED HERE"

For custom sections however, you have a URL that looks something like this.

In the above URL the ID 12345678912345 is the template ID and 391178d3-08f5-48cb-865d-c407f193babe is the section ID. The format of the rest of the parameter value is always the same.

This means in order to programatically request that section via the Section Rendering API, you need to know the template ID and the section ID. The problem is that there is no public endpoint that gives you the ID of the template ID, whereas the section ID's are all contained in the section .json file such as product.product-example.json at the bottom of the file in the order array. Example below.

"order": [

Solution 1

You can run this javascript that will extract the template ID from a page by looking at the first <section> tag and parsing out the template ID integer from the 'id' for that section.

function extractTemplateId(id) {
    const prefix = 'shopify-section-template--';
    const startIndex = id.indexOf(prefix) + prefix.length;
    const endIndex = id.indexOf('__', startIndex);
    const numberPart = id.substring(startIndex, endIndex);
    return parseInt(numberPart, 10);

// get the first shopify section ID for extraction
const first_section_id = document.querySelector('main section.shopify-section')
const fullId =

// Run the extractTemplateId function
const shopifyTemplateId = extractTemplateId(fullId);
console.log(shopifyTemplateId); // Output example: 12345678912345

Solution 2 (recommended)

If you have control of the Shopify theme, you can add the following code to main-product.liquid.

{% liquid 
  assign section_id = 'template--12345678912345__391178d3-08f5-48cb-865d-c407f193babe'
  assign section_id_parts = section_id | split: 'template--'
  assign section_with_id = section_id_parts[1]
  assign section_id_parts = section_with_id | split: '__'
  assign template_id = section_id_parts[0]

And then add the data-template_id attribute to the <section> block seen here on line 5:

  id="MainProduct-{{ }}"
  class="page-width section-{{ }}-padding"
  data-section="{{ }}"
  data-template_id="{{ template_id }}"

This will then allow you to grab the template_id for each page with the following simple JS code:

// output example: 12345678912345


Both solution 1 and 2 depend on the default string that Shopify generate for a section ID. This could change at any given time although not likely. If you're wanting to do something programmatic, make sure to add a fallback or log/alert if this ever fails.

# commerce 14 # seo 5 # tools 6 # amazon 1 # sql 4 # shopify 9 # javascript 13 # projects 4 # css 2 # git 2 # php 3 # analytics 4 # monitoring 2 # api 6 # python 2 # aws 2