How to get active theme Id

Highlighted
Tourist
25 0 3

Hello,

I am developing shopify app which needed to update product-template.liquid file.

I want to add some HTML elements to the product-template.liquid when App is installed first time.

I find the below Asset API to update theme files:

PUT /admin/themes/#{id}/assets.json
{
  "asset": {
    "key": "templates\/index.liquid",
    "value": "<img src='backsoon-postit.png'><p>We are busy updating the store for you and will be back within the hour.<\/p>"
  }
}

 

But this API needed theme Id to update the theme files.

Can anyone please help me out to get the current active theme Id using API.

Thanks & regards

0 Likes
Highlighted
Shopify Expert
9930 105 1766

Use the Themes API to pull the list of themes, and check which has the role of main.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
5 Likes
Highlighted
Tourist
25 0 3

Hi Jason,

Thank you so much for reply.

This what I am looking for, the theme API to pull the list of themes.

GET /admin/themes.json

You solved my problem, Thanks for your support.

1 Like
Highlighted
Astronaut
1085 178 213

Using axios and koa-router this is how I did it. I created my own custom end-point and accessed it via the React front-end. Take a look at koa-router documentation for further information!

 
 
  router.get("/api/theme", async (ctx, next) => {
    try {
      
      const headers = {
        "X-Shopify-Access-Token": ctx.cookies.get("accessToken"),
      };

      const themesURL = `https://${ctx.cookies.get(
        "shopOrigin",
      )}/admin/api/2020-04/themes.json`;

      const { data: themes } = await axios.get(themesURL, {
        headers,
      });

      const themeId = themes.themes.filter(({ role }) => role === "main")[0].id;

      const assetURL = `https://${ctx.cookies.get(
        "shopOrigin",
      )}/admin/api/2020-04/themes/${themeId}/assets.json`;

      const { data: assets } = await axios.get(assetURL, {
        headers,
      });

      ctx.body = {
        assets,
      };
    } catch (error) {
      ctx.body = {
        error,
      };
    }
  });
 
 
◦ Problem Solved? ✔️ Please click on Accept as Solution and like.
◦ Hire me! Recommended by 25+ customers on Facebook. Click here to read their reviews.
◦ Contact me at: diego.boarutto.fortes@gmail.com
1 Like