Creating Shopify pages via Rails using shopify-api gem

I’m working on adding new pages to a Shopify store using the shopify-api gem in my Rails app. The authentication setup works fine for GET requests, but I’m having trouble with POST operations.

I need to create pages programmatically, and I tried two different approaches. First, I attempted using the ShopifyAPI models directly:

def add_store_page
  @new_page = ShopifyAPI::Page.new(:title => "Sample Page", :body_html => "<h2>Sample Content</h2>")
  @new_page.save
end
helper_method :add_store_page

When that didn’t work, I tried making a direct API call:

def build_page
  page_params = {
    "page": {
      "title": "Sample Page",
      "body_html": "<h2>Content created successfully</h2>"
    }
  }
  
  @api_endpoint = @store_url + "/admin/api/2020-07/pages.json"
  
  Rails.ajax({
    type: "POST",
    url: @api_endpoint,
    data: page_params
  })
end

I’m getting errors about undefined methods and nil values. The GET requests work perfectly, so the authentication is set up correctly. What am I missing for POST requests to work properly?

I ran into similar issues when implementing page creation through the Shopify API. The main problem is likely with your session handling for write operations. Make sure you’re properly initializing the session context before making POST requests.

In your first approach, you need to ensure the API session is active. Try wrapping your code like this:

ShopifyAPI::Session.temp(domain: shop_domain, token: access_token, api_version: '2021-01') do
  @new_page = ShopifyAPI::Page.new(title: "Sample Page", body_html: "<h2>Sample Content</h2>")
  @new_page.save
end

For the direct API approach, Rails.ajax won’t work server-side. Use Net::HTTP or a proper HTTP client gem instead. Also check that your app has the required write permissions in your Shopify app settings - read permissions alone won’t allow POST operations. The scopes need to include write_content or similar depending on your API version.

Had this exact problem a few months back when building a custom page management system. The issue is probably in your error handling and response checking. When I was debugging my POST requests, I found that even though they seemed to fail, sometimes the pages were actually being created but the response wasn’t being processed correctly.

Try adding proper error handling to see what’s actually happening. For the ShopifyAPI model approach, check @new_page.errors after calling save - it often contains specific validation messages. Also make sure you’re not hitting rate limits since POST requests count differently than GET requests.

One thing that caught me off guard was that the body_html parameter needs proper HTML escaping in some cases. Try creating a simple page first with just plain text in the body_html to rule out content issues. The Rails.ajax approach definitely won’t work from your controller since that’s meant for client-side JavaScript - stick with the ShopifyAPI gem or use proper server-side HTTP libraries.

check your api version first - the 2020-07 version ur using might be deprecated. also make sure ur handling the session token properly for write operations. i had this exact issue and it was becuase my token wasnt getting passed correctly in the headers for POST requests even tho GET worked fine.

The issue you’re experiencing is likely related to API permissions and session context. I encountered this when integrating page creation into a client’s Rails application last year. The authentication working for GET but failing for POST typically indicates your app doesn’t have the correct write scopes configured in your Shopify partner dashboard.

For the ShopifyAPI model approach, verify that your session is properly activated before attempting the save operation. The session needs to be explicitly set with the correct shop context. Also, the API version matters significantly - newer versions have stricter validation requirements for page content.

Regarding your second approach, Rails.ajax is client-side JavaScript and won’t execute in your Rails controller. You need to use a server-side HTTP client like HTTParty or Net::HTTP. However, I’d recommend sticking with the ShopifyAPI gem since it handles authentication headers and request formatting automatically. Check your app’s installed scopes in the Shopify admin to ensure you have write_content permissions enabled.