Direct API on extension-only apps error

Direct API on extension-only apps error

Samirjs
Shopify Partner
7 0 1

I find that it is possible to call the shopify api on extension directly. The doc is available on this link: Admin UI extensions. According to the documentation, API calls from extension are automatically autenticated by default.

However, I can't get it to work properly. I get this erro when I use fetch to get data from shopify api

 

Samirjs_0-1734179909313.png

 

 

The code of my extension file "star_rating.liquid":

 

<script>
    fetch('shopify:admin/api/graphql.json', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      query: `
        query {
          products(first: 10) {
            edges {
              node {
                id
                title
                handle
              }
              cursor
            }
            pageInfo {
              hasNextPage
            }
          }
        }
      `
    }),
  }).then(resp => resp.json()).then(body => {
    console.log(body)
  });
</script>

<div style="background-color: blue; width: 400px; height: 400px; text-align: center">
  <p style="color: white">teste</p>
</div>

{% schema %}
{
  "name": "Star Rating",
  "target": "section",
  "settings": [
    { "type": "product", "id": "product", "label": "product", "autofill": true },
    { "type": "color", "id": "colour", "label": "Star Colour", "default": "#ff0000" }
  ]
}
{% endschema %}

 

 

Replies 2 (2)

ErSanjay
Shopify Partner
334 20 49

@Samirjs  Hi, I hope you are doing well..

It seems that the issue you’re facing might be due to an error in the extension code. One thing I noticed is that the schema in your code doesn’t seem to align with the documentation for Admin UI extensions. Additionally, it looks like you might be working on a Theme App Extension.

To address the issue, you may need to update the code to match the documentation standards. If the problem persists, I’d suggest sharing more details or the relevant code snippet here. That way, I or another community member can assist you more effectively.

Hope this helps! 😊

Business Owner & Shopify Plus, Shopify app , Shopify Consultant - Full Stack Sofware Engineer
Warm regards,
Er Sanjay

If you find yourself in need of assistance with your store, don't hesitate to reach out! Feel free to send me a direct message, and I'll do my best to help you out.
Samirjs
Shopify Partner
7 0 1

Yes. You are right. I'm using Theme App Extension and the documentation is for Admin UI Extension. So isn't there a way to make a direct api request on Theme App Extension? I'm trying to make requests to shopify api on an extension-only app.