Access Shopify store GraphQL data inside python in Notebooks

Hello, I am trying to create a custom report in shopify notebooks to rank discount codes by revenue. In order to do that i am accessing my shopify store data via graphQL inside shopify notebooks’s python editor. I am javascript developer so my GQL & Python knowledge is limited

Here is the code

access_token = '1234567890'
headers = {
    "Content-Type": "application/json",
    "X-Shopify-Storefront-Access-Token": access_token
}
#get list of all dynamic discounts first
query = '''
{
  codeDiscountNode(id: "gid://shopify/DiscountCodeNode/1246057332849") {
    codeDiscount {
      ... on DiscountCodeBasic {
        codeCount
        codes(first: 100) {
          edges {
            node {
              code
              id
            }
          }
        }
        customerSelection {
          __typename
        }
        startsAt
        endsAt
      }
    }
  }
}
'''
response = await http.post(url="https://mystore.myshopify.com/admin/api/2023-10/graphql.json", body={"query": query}, headers=headers)
response

But I am getting an error

OSError: Failed to fetch

Screenshot of ShopifyQL Notebooks

What am I missing?

Couple of updates

Update 1: Above configuration works perfectly fine in postman, which means access token and permissions in the app are perfectly fine and correct.

Update 2: In ‘Shopify Notebooks’ a public GraphQL api request/response works perfectly fine see below screenshot, which means notebook can make outside world GraphQL requests.

So I am speculating if Shopify is blocking notebook requests to its own API? - which does not make any sense.

Update 3:

Access token was for admin Api so fixed access header “X-Shopify-Storefront-Access-Token” to “X-Shopify-Access-Token”

But this also did not fix the issue

Update 4:

In network tab I can see a CORS error, which I dont get as its got all access token it needs

Update 5: (final update)
So after some googling I found out that Shopify blocks client side request to APIs to avoid secret token from being publicly exposed. So their solution is to use App Proxy.

People developing themes or app from partner portal can configure Proxy app following this guide

https://shopify.dev/docs/apps/online-store/app-proxies#calculate-a-digital-signature

Since I was building a custom app that is why I could not use Shopify proxy app. So solution for me is to create a server side app that will call shopify securely and then Shopify notebook will call that server side app. This is bit convoluted way to access dynamic discounts but mystery is now solved. Hope this will same someone else some time.

This video explain this process for people trying to do this for custom apps

https://www.youtube.com/watch?v=KCukESgeGUc

Note: if shopify team is reading this. please enable full admin access for Notebook access via ShopifyQL so we dont have to do such gymnastics to get access to simple thing such as dynamically generated discount codes and their revenue performance.

1 Like

I completely agree! If Graphql is accessible via the Shopify GraphiQL app, why is that not also available in the notebooks? Doing something as simple as filtering out orders by tags becomes a multi-day chore, where the actual query is just a few lines. Please Shopify devs, make this happen!