Using the Shopify API command line interface (CLI)

| 3 min read

A good many developers probably have installed the shopify_api gem from in order to built an Application to provide new functionality to shops. In fact, without even wanting to develop a full application, this gem is great for quick and dirty manipulation of stores. I am constantly using the Ruby console IRB to check if certain inventory makes sense, or other tidbits. It is almost essential to use this gem to advantage. If you have ever used Textmate on OSX and hooked up the handy Shopify Bundle from Meeech, then you are very used to typing in an API key and password to a domain to edit the assets, theme, etc. Turns out, there is yet another way to interact with shops, and it also involves the command line.

Ruby developers are generally very familiar with rakefiles and rake tasks. Unlike makefiles and the make command, two things generally reserved for people that think wave equations are fun for breakfast distractions unlike the rest of us that just think waves are pretty can like to jump in them, rake and rakefiles are comprehensible and indispensable.

As with anything progressive, rake was deemed to be ripe for a re-factoring and during the glory days of Merb, before Rails switched to Merb-ishness, Merb had Wycats and Thor. Thor is like rake, but even more fun some would say. Thor tasks are yet another way of running ruby tasks so that developer life is easier and more automatic. Turns out, the Shopify developer gang built a Thor task command-line interface (CLI) into the Shopify gem. I finally got around to playing with it this rainy weekend, many months after it was introduced to me at a beer drinkup I attended with the Shopify nerds.

When you install the Shopify API gem

gem install shopify_api 

you get this command line interface for free! In order to make it a little more compatible with my current environment, I tweaked it a little as per the gist here: cli.rb

If you copy that code into your favourite text editor and save it as cli.rb, you can experiment with this neat option. Once you save this file to your system (*nix compatible), you can mark it as executable with
chmod +x cli.rb

If you then type cli.rb list you will see nothing!!! Yay... unless you see errors, which would be mean you messed something up. If that is the case, and some error chunks are blown, edit the file till none are thrown.

Once going, try this:

cli.rb add mysite

You will be prompted that will be created for you, or you could type in something else.

Now, you fill in the API key, and API password for the site, and all of a sudden, this is a good thing. A configuration file has been made especially for this site. Big deal you say. Bah... Do that crap all the time... Okay.

So next, try:

cli.rb console

And now, you're in a full-blown IRB session, authenticated to that store. Wonderful. Now you can ask questions like:


and get an instant answer. No messing around. I used to just use IRB and paste in my Shop's Auth Key, but that is too painful, I can never remember all 280 digits off the top of my head. Even connecting to Heroku and asking my Shop for the credentials is a pain, and looking inside my note application also sucks. This is just a simpler, cleaner way to make a config file and hang out.

There are enough other commands to keep it all nice and organized. In fact, the current list is:

  cli.rb add CONNECTION        # create a config file for a connection named CONNECTION
  cli.rb console [CONNECTION]  # start an API console for CONNECTION
  cli.rb default [CONNECTION]  # show the default connection, or make CONNECTION the default
  cli.rb edit [CONNECTION]     # open the config file for CONNECTION with your default editor
  cli.rb help [TASK]           # Describe available tasks or one specific task
  cli.rb list                  # list available connections
  cli.rb remove CONNECTION     # remove the config file for CONNECTION
  cli.rb show [CONNECTION]     # output the location and contents of the CONNECTION's config file

Neat. Enjoy. I know I will.