Making API calls HOW TO ???

New Member
9 0 0

Hi, i am struggling with this problem since weeks, and i don't have a clue how to get this work:

I have a Shopify Rails App hostet on Heroku. The App is already installed and has read/write credentials in order to set script tags.

Now i want to make these API calls https://help.shopify.com/api/reference/scripttag

If i only call /admin/script_tags.json nothing is working,

if i call https://myshop.myshopify.com/admin/script_tags.json i got CORS error in response

What am i missing?

Do i need to set up any routes in my routes.rb file? Or do i need to set up a proxy somehow?

How are these API calls ment to be called inside a .erb file? At the moment i use Rails.ajax to make these calls. But somehow i can't get it to work. Are there any working examples, or do i need to install shopify-express?

I have no more idea where to go from this point.

Please help me

0 Likes
Highlighted
Shopify Expert
27 0 14

Have you setup the authentication of your app with your Shopify store? The latter call you are making requires this. 

You should have a private app installed on your store front that gives the auth tokens to your app so it can make API calls to your store.
https://help.shopify.com/api/getting-started/authentication/oauth

JR

0 Likes
New Member
9 0 0

My authentication of the app seems to work, because the app is installed correct with all scopes via https://myapp.herokuapp.com/auth/shopify/callback

This is my omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :shopify,
           ShopifyApp.configuration.api_key,
           ShopifyApp.configuration.secret,
           scope: ShopifyApp.configuration.scope,
           setup: lambda { |env|
             strategy = env['omniauth.strategy']

             shopify_auth_params = strategy.session['shopify.omniauth_params']&.with_indifferent_access
             shop = if shopify_auth_params.present?
               "https://#{shopify_auth_params[:shop]}"
             else
               ''
             end

             strategy.options[:client_options][:site] = shop
           }
end

i can make API calls on my home_controller.rb like this

class HomeController < ShopifyApp::AuthenticatedController
  def index
    j = ShopifyAPI::ScriptTag.new(:src => "http://foo.com/foo.js", :event => "onload")
    j.save
  end
end

which is working and inserting the script tag as i want.

BUT

I can not make calls like this (this is only copied example of code, which will cause CORS error)

mydata = {
            "script_tag": {
            "event": "onload",
            "src": "https:\/\/www.foo.com\/foo.js"
           }
         };

mytoken = ??????? i have no idea where to get this from OR if i need this

Rails.ajax({
        url: 'https://myapp.myshopify.com/admin/script_tags.json',
        type: 'POST',
        data: mydata, 
        dataType: 'json',
        headers: {
          "Content-Type": "application/json",
          "X-Shopify-Access-Token": mytoken
        },
        success: function(result)   {
          console.log('success!')
        },
        error: function(err)    {
          console.log(err);
        }
      });

I only followed the instructions how to create an app with ruby, so almost all was created automatically. I don't exactly understand, what to do

0 Likes