Querying Admin API from ScriptTag via app proxy

Highlighted
New Member
2 0 0

I feel like I must be missing something: I have an app creates a ScriptTag, I would like this ScriptTag to make an request to my backend via an app proxy, and have that route query the grapql admin API and return the response to my script.

This is what the process payments route does in the app template by access koa's context (ctx) sessions and cookies. When I try to make a similar request, ctx.session and ctx.cookies are missing both shopOrigin and accessToken.

 

 

router.get('/app_proxy', ctx => {
  // accessToken or shopOrigin both undefined here
  const { shopOrigin, accessToken } = ctx.session

  // make graphql request here, this is pseudo-code
  const response = fetch(`https://${shopOrigin}.com/graphql.json`, {
    headers: {
      'X-Shopify-Access-Token': accessToken,
    },
  })
})


I have a route on my backend:My script tag calls its using fetch: 

await fetch(`https://store.myshopify.com/app/app_proxy`, { credentials: true })


And yet I don't have access to any koa ctx session variable or cookie... Am I doing this completely wrong? Is there a better way to do this?

Thanks.

0 Likes
Shopify Staff
Shopify Staff
176 7 38

Hey Nikodraca,

App Proxies are generally to render a liquid template on the storefront, the app provides the template and then the storefront renders it. This can be a good way for a customer of a merchant to interact with your app. 

It's hard for me to talk about specifics here since I don't know what you're doing — but generally you should avoid using App Proxies to redirect requests from ScriptTags 

1 Like
New Member
2 0 0

Hey Shayne,

 

Thanks for your reply. In that case what you recommend doing when you need product/store data (that isn't available on the AJAX API) on the storefront?

Example: When a user navigates to a product display page, fetch the inventory for the product and display it.

0 Likes