Rails & Shopify: DelayedJob (non WebHook) - Authenticate and create session

Topic summary

Main issue: running a scheduled background job (outside webhooks) needs a valid Shopify session to update products, but the developer lacks the shop_domain at runtime.

Key guidance:

  • The shop_domain is required to establish a Shopify session and uniquely identifies each store.
  • It should be saved in your database during app installation. If it isn’t, update the installation flow to persist it.
  • In a job, iterate over stored shops and open a session per shop, e.g., using Shop.find_each and shop.with_shopify_session to perform updates.

Outcome and status:

  • The proposed approach is to rely on persisted shop_domain values and create sessions per shop during the job run.
  • The original poster confirmed the advice was helpful; no further issues were raised. The discussion appears resolved with clear action items (store shop_domain and use with_shopify_session).
Summarized with AI on January 11. AI used: gpt-5.

Hello,

To create a valid Shopify session, you’ll indeed need the shop_domain. The shop_domain is a unique identifier for each shop and is necessary to establish a session.

If you’re running a job needs to update products on Shopify, you would likely have this information stored somewhere in your database. When a store installs your app, should be storing the shop_domain in your database.

Here’s a general way you could use the shop_domain to establish a Shopify session in a job:

Shop.find_each do |shop|
  shop.with_shopify_session do
    # Your code to update products on Shopify goes here
  end
end

In this example, Shop.find_each is iterating over each Shop record in your database, and for each shop, it establishes a Shopify session and runs the code within the block. If for some reason you don’t have the shop_domain stored, you would need to adjust your app installation process to save this information.

Hope this helps!