dudettes
dudettes

Reputation: 21

Call Shopify API from App Proxy

Goal

I want to create a following app.

Merchant selects 'top products' for each customer. These will be stored in my backend.

For example:

The customer should then be able to login, go to a page in the store (e.g. store.com/top_products) and see their own 'top products'.

What I managed to do so far

I managed to deploy the rails Shopify app (https://github.com/Shopify/shopify_app, which uses the shopify_api gem) to Heroku and connect it to my store.

I also managed to setup the app proxy. I get the right response from the app for both the admin app page and the app proxy.

Essentially my public app seems to work fine.

What I have to do now

I will have to call something like this in the app proxy:

ShopifyAPI::Product.find(:all, params: { limit: 10 })

However, this can be only called from ShopifyApp::AuthenticatedController (like in the admin app controller) and not ShopifyApp::ApplicationController (like in the proxy controller).

So far I only managed to call the APIs and see the correct results from the admin dashboard of the store.

I cannot manage to call ShopifyAPI::Product.find from unauthenticated App Proxy.

Google doesn't really help me. This is similar to my problem (has no solution though): https://ecommerce.shopify.com/c/shopify-discussion/t/how-to-connect-to-a-store-with-api-from-app-proxy-request-429417

Question

  1. What would be the best way to call the API from the App Proxy?
  2. Or how do I display some information on the store frontend using my backend which talks to the store's API? Is an app proxy the correct approach?

Upvotes: 2

Views: 1467

Answers (1)

David Lazar
David Lazar

Reputation: 11427

An App Proxy call from Shopify gives you the store as parameter shop. Hence you can look up the shop in your backend and do anything with an authenticated session. I do this all the time with App Proxy and Webhooks. You validate the calls are from Shopify, you get the shop name, hence you can open a secure session. Additionally, you can send in the customer ID with your App Proxy call to get the products for the customer.

You are doing the right thing, the App Proxy is the right pattern, and so carry on. You are minutes away from glory.

From one of my Proxy controllers...

shop = Shop.find_by(shopify_domain: params[:shop])
if shop
  shop.with_shopify_session do
    products = ShopifyAPI::Product.find(params[:id])
    # la-dee-da I got a product...
  end
end

Upvotes: 7

Related Questions