README.rdoc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. = Shopify API
  2. The Shopify API gem allows Ruby developers to programmatically access the admin section of Shopify stores.
  3. The API is implemented as XML over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like Order, Product, or Collection, has its own URL and is manipulated in isolation. In other words, we’ve tried to make the API follow the REST principles as much as possible.
  4. == Usage
  5. === Requirements
  6. All API usage happens through Shopify applications, created by either shop owners for their own shops, or by Shopify Partners for use by other shop owners:
  7. * Shop owners can create applications for themselves through their own admin (under the Preferences > Applications tab).
  8. * Shopify Partners create applications through their admin: http://app.shopify.com/services/partners
  9. For more information and detailed documentation about the API visit http://api.shopify.com
  10. === Getting Started
  11. ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveResource has to be configured with a fully authorized URL of a particular store first. To obtain that URL you can follow these steps:
  12. 1. First create a new application in either the partners admin or your store admin and write down your API_KEY and SHARED_SECRET.
  13. 2. You will need to supply two parameters to the Session class before you instantiate it:
  14. ShopifyAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
  15. 3. Create a new Session for a specific shop. That session is not fully valid yet, but it can be used to create a URL that you will redirect your users to:
  16. session = ShopifyAPI::Session.new("yourshopname.myshopify.com")
  17. session.valid? # returns false
  18. 4. To access the API shop owners need a "token" from that specific shop. In order to get this token they need to authorize with that shop first. To get this token they should redirect the user to the following URL:
  19. GET https://SHOP_NAME.myshopify.com/admin/oauth/authorize
  20. with the following parameters:
  21. * client_id – Required – The API key for your app
  22. * scope – Required – The list of required scopes (explained below)
  23. * redirect_uri – Optional – The URL that the merchant will be sent to once authentication is complete. Must be the same host as the Return URL specified in the application settings
  24. 5. Once authorized, the shop redirects the owner to the return URL of your application where the "token" gets sent to (it's param name is just "t"). Use that token to instantiate a "valid" session, that is ready to make calls to that particular shop.
  25. token = params[:t]
  26. session = ShopifyAPI::Session.new("yourshopname.myshopify.com", token)
  27. session.valid? # returns true
  28. 6. Now you can activate the session and you're set:
  29. ActiveResource::Base.activate_session = session
  30. 7. Get data from that shop (returns ActiveResource instances):
  31. shop = ShopifyAPI::Shop.current
  32. latest_orders = ShopifyAPI::Order.find(:all)
  33. Alternatively, you can use #temp to initialize a Session and execute a command which also handles temporarily setting ActiveResource::Base.site:
  34. latest_orders = ShopifyAPI::Session.temp("yourshopname.myshopify.com", token) { ShopifyAPI::Order.find(:all) }
  35. 8. Finally, you can also clear the session (for example if you want to work with another shop):
  36. ShopifyAPI::Base.clear_session
  37. == Copyright
  38. Copyright (c) 2012 "Shopify inc.". See LICENSE for details.