How to retrieve parent store domain from embedded Shopify application

I built a public Shopify application that runs embedded within merchant stores. The challenge I’m facing is getting the actual store domain from within my app code.

When my application loads, the URL structure looks like this:
https://merchantstore.myshopify.com/admin/apps/my-application

I tried using $_SERVER['HTTP_REFERER'] to capture the store URL, but this method returns my application’s URL rather than the merchant’s store domain. Since my app runs inside an iframe, I can’t rely on JavaScript solutions because I need the store URL on the server side to handle billing redirects properly.

The main issue is that I need to programmatically determine which store is using my app so I can either redirect users to the appropriate billing page or modify the current view to show billing information.

Is there a reliable way to extract the parent store domain from within an embedded Shopify app using server-side code?

totally! using the session token is the way to go. decode the JWT and you can get the shop domain easily. much safer than messing with referers and headers that get all wonky in iframes!

The shop parameter comes through during the initial OAuth flow - you should store it with the access token in your session or database. Shopify automatically includes the shop domain when merchants install your app, so if you’re losing it between requests, your session management’s probably the issue. If you’re using App Bridge, check the context there since it keeps track of the shop info. You can also verify everything’s working by looking at your webhook headers - Shopify always includes the shop domain there. Just make sure you’re storing and retrieving the shop ID consistently throughout your app.