Querying Admin API from ScriptTag via app proxy

nikodraca
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
Shayne
Shopify Staff
Shopify Staff
202 9 46

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 

Shayne | Developer @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

nikodraca
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
Symediane
New Member
2 0 0

Same problem for me, 

- I make a call from a ScriptTag to my app using the proxy (/apps/my-app-proxy/app_path)

- in my app, on /app_path I receive the request and I would like to create a metafield via the REST shopify API but I miss the "X-Shopify-Access-Token" because it is only generated when the call comes from the app

Do I need to use an offline access token? and how can I generate it?

0 Likes