Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Passing session object into another function/file in order to query for shopID

Passing session object into another function/file in order to query for shopID

jomei1
Shopify Partner
6 0 0

Hey all, sorry if this is a dumb question, but I'm pretty new to shopify app development and I'm currently stuck trying to retrieve the shopID for the merchant store. I have a function that gets triggered by the orders/create webhook, and I need to then be able to get the shopID to then send somewhere else. 

Screenshot 2023-08-04 at 9.16.38 PM.png

I have attached two ideas I have for functions to do this, but I'm currently stuck trying to pass the session object through. Any ideas how I might be able to do this? Otherwise, do you guys know if there are perhaps more elegant solutions that I should try? 

 

I've also thought about adding an api in my index.js file, and trying to call it here instead, but am having trouble getting useAuthenticatedFetch to work. 

 

Thanks in advance! 

 

Replies 4 (4)

ShopifyDevSup
Shopify Staff
1453 238 527

hi @jomei1 . You can include 'id' in your query to the shop resource in the ADMIN API and this will return the shop_id. https://shopify.dev/docs/api/admin-graphql/2023-07/queries/shop#top, https://shopify.dev/docs/api/admin-graphql/2023-07/objects/Shop#field-shop-id.

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

jomei1
Shopify Partner
6 0 0

Thanks for the response, maybe I'm misunderstanding what the ADMIN API is, but the issue I'm running into is running the query itself, as the GraphQL query seems to require the session object, which my function can't currently access. Any idea how I would be able to do so? And/or how to run a query without requiring the session token to be passed through? Thanks. 

ShopifyDevSup
Shopify Staff
1453 238 527

Hi @jomei1 ,

 

Is your app using online access mode when it installs through OAuth?

If it needs to make Admin API requests outside of user sessions, have you considered requesting an offline token in the OAuth flow instead?

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us 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

jomei1
Shopify Partner
6 0 0

Yes, I believe it is. I figured out a solution by creating a session object using the shop object passed through the /api/webhooks call. (Following this example here:

 https://workshops.shopify.dev/workshops/webhooks-in-nodejs#8) Thanks again!