Querying Admin API from ScriptTag via app proxy

nikodraca
Visitor
2 0 1

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.

Replies 4 (4)

Shayne
Shopify Staff
253 20 64

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 Advocate @ 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
Visitor
2 0 1

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.

Symediane
Shopify Partner
3 0 2

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?

shlomomeyer
Shopify Partner
1 0 1

Same issue for me. How can I access product info from a proxy api on the client storefront? i'm using loadCurrentSession but getting undefined