Using the shopify api gem with the credentials of a private app
From Shopify Wiki
You want to do certain things to your products — or to your orders — that aren't so easy to accomplish in your shop admin. You want to use the Shopify API for this, and you have Ruby installed on your computer. This article will show you how to use the shopify_api gem in the interactive Ruby console —irb— to 'speak' to your shop and inflict upon it what you will.
Contents |
Getting your URL slash whip ready
Go to Apps → Manage apps in your shop admin:
Click on the "Click here" link at the bottom of the /admin/applications page:
The /admin/api page lists all the private API keys you have created. You can generate a new key, or delete existing keys if you are no longer using them.
Treat all your API keys like you'd treat the password to your shop admin. With an API key and password, anyone can access and modify the content of your shop. We're speaking ultimate control.
The developers at Jaded Pixel are so extremely nice boys and women that they have given you an Example URL that you will be able to paste in your Terminal to whip your shop into submission — filled-up with your API key, password and shop URL:
You can select the http://apikey:password@hostname/admin part of that URL and copy it to your clipboard. Leave out the /orders.xml part.
Opening up irb
Go into your Terminal or Ruby console and launch irb:
$ irb
Summoning the powers of the shopify_api gem
In irb, include the shopify_api gem:
>> require 'rubygems' => true
>> require 'shopify_api' => true
Telling your shop who's boss
Your shop will only obey you if you identify yourself correctly as its master or dominatrix.
In irb, the magic phrase is:
>> ShopifyAPI::Base.site = "https://username:password@some-shop.myshopify.com/admin"
That URL between double quotes is the one you copied to your clipboard in the 'Getting your URL slash whip ready' section. You may actually want to save the whole command somewhere well-protected.
Examples of stuff you can do
Correct the misspelled email address of a customer
Miss Jones bought some stuff from you but misspelled her email address on the checkout pages. Oh dear. Looks like you can't edit her email address on the order details page in your admin. Let's fix that email address.
You need to know the order ID. The order ID of an order is given away by the URL to that order's details page in your admin:
Let's get that order using its ID:
>> order = ShopifyAPI::Order.find(24079572)
Then, let's edit the email address:
>> order.email = "juliejones@gmail.com" => "juliejones@gmail.com" >> order.save => true
We're done.
You can change an order’s note, note_attributes, email, and buyer_accepts_marketing. Those are the only attributes modifiable through the API. You can, of course, fulfill, open, close and accept payment for an order using the API.
Hide — or publish — a bunch of products
There's no easy way to bulk publish or hide products in a shop. That is, unless you use the API.
One trick is to create a smart or custom collection that will include the products you want to, say, hide.
You need to know your collection ID for the next step. The ID of a collection is given away by the URL to that collection page in your admin:
Let's grab the products in that collection:
>> products = ShopifyAPI::Product.find( :all, :params => { :collection_id => 2466172, :limit => 250 } )
To make sure we won't be committing a blunder, let's check how many products we have just grabbed:
>> products.size => 22
Does the count you get make sense? If so, go on.
Let's hide those products:
>> products.each { |p| p.published_at = nil; p.save }
Note: That's right, to hide a product, you have to set its published_at attribute to nil.
To publish a product that's hidden, you'd use this:
# publish product.published_at = Time.now.utc product.save
If we have grabbed a bunch of products:
>> products.each { |p| p.published_at = Time.now.utc; p.save }
We're done.
Change a vendor name from x to y
There's no easy way to edit a vendor name in your shop. That is, unless you use the API.
Let's grab the products in your shop that have the vendor name you wish to edit:
>> products = ShopifyAPI::Product.find(:all,:params => {:vendor => 'Shopify', :limit => 250})
To make sure we won't be committing a blunder, let's check how many products we have just grabbed:
>> products.size => 17
Does the count you get make sense? If it does, go on.
Let's edit the vendor for those products:
>> products.each { |p| p.vendor = 'Whip it'; p.save }
And we're done.
Have many resources to update?
If you have more than 300 API calls to make, you'll need to throttle those calls. This article will show you how: Learning to Respect the API calls limit.





