Problem with ScriptTag Creation
I’m trying to add a script tag to my Shopify store using a Rails application but keep running into issues. Here’s what I’m doing:
- My Rails app successfully authenticates with Shopify and gets the access token.
- I have a JavaScript file with a simple function that I want to load.
- When I try to create the script tag, I get an error.
My Code:
ShopifyAPI::Base.site = @access_token
script_tag = ShopifyAPI::ScriptTag.create(
:event => "onload",
:src => "http://myserver.example.com/assets/custom.js"
)
Error Message:
URI::BadURIError (both URI are relative):
app/controllers/shop_controller.rb:25:in `callback'
The app is running on Heroku and this error happens every time I try to install the script tag. What could be causing this URI error? Am I setting up the base site URL incorrectly?
Had this exact problem building a Shopify app last year. You’re getting URI::BadURIError because you’re not setting up the session context before making API calls. Don’t set the site directly - use the session approach that newer versions of the ShopifyAPI gem expect.
Try this instead:
session = ShopifyAPI::Session.new(
domain: "#{shop_domain}.myshopify.com",
token: @access_token,
api_version: "2023-10"
)
ShopifyAPI::Context.activate_session(session)
Then create your script tag without manually setting the base site. This handles URL construction internally and should fix the BadURIError. Also double-check that your custom.js file is actually accessible at that URL - I’ve seen script creation fail silently when the source file returns a 404.
You’re getting URI::BadURIError because you’re passing the access token directly to the site property - but it expects a full URL, not just a token. The gem can’t resolve relative paths without a proper base URL. I hit this exact issue on Heroku. The fix: store your shop domain separately from the access token, then build the API endpoint URL correctly. Don’t mix them up. Also check that your custom.js file is actually serving from the right Heroku path. Asset compilation moves files around in production, so the URL might’ve changed.
This URI::BadURIError happens when the ShopifyAPI gem can’t build proper URLs because your base config is missing or wrong. I’ve seen this before - it’s usually because the site parameter isn’t a proper URL format. You’re passing @access_token directly to ShopifyAPI::Base.site, but that’s not right. The site needs your shop’s API endpoint URL, not the token. Handle the access token separately through headers or session config. Make sure you’re building the site URL with your shop domain and API version - like https://your-shop.myshopify.com/admin/api/2023-10/. Also double-check that @access_token actually has the shop domain info you need, not just the token string.
I encountered this same URI::BadURIError when working on a Rails project with Shopify. The main issue usually arises from the way the Shopify API expects the base URL to be formatted. You should set the ShopifyAPI::Base.site to the full shop URL instead of directly using the access token. Here’s how you can modify your code:
ShopifyAPI::Base.site = "https://#{shop_domain}.myshopify.com/admin/api/2023-10/"
ShopifyAPI::Base.headers['X-Shopify-Access-Token'] = @access_token
Ensure you replace shop_domain with your actual Shopify store domain. This change should resolve the issue as it provides the API with the required absolute URL.
ur mixing up the site param with the access token. the site should be ur shop’s full URL like https://yourshop.myshopify.com/admin/api/2023-10/ and the token goes in headers. also check if ur JavaScript file URL is actually accessible from Heroku - the assets pipeline can mess with URLs in production.