README.rdoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 JSON 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: http://docs.shopify.com/api/tutorials/creating-a-private-app
  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. === Installation
  11. To easily install or upgrade to the latest release, use {gem}[http://rubygems.org/]
  12. gem install shopify_api
  13. === Getting Started
  14. 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:
  15. 1. First create a new application in either the partners admin or your store admin. For a private App you'll need the API_KEY and the PASSWORD otherwise you'll need the API_KEY and SHARED_SECRET.
  16. 2. For a private App you just need to set the base site url as follows:
  17. shop_url = "https://#{API_KEY}:#{PASSWORD}@SHOP_NAME.myshopify.com/admin"
  18. ShopifyAPI::Base.site = shop_url
  19. That's it you're done, skip to step 7 and start using the API!
  20. For a partner app you will need to supply two parameters to the Session class before you instantiate it:
  21. ShopifyAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
  22. 3. In order to access a shop's data, apps need an access token from that specific shop. This is a two-stage process. Before interacting with a shop for the first time an app should redirect the user to the following URL:
  23. GET https://SHOP_NAME.myshopify.com/admin/oauth/authorize
  24. with the following parameters:
  25. * client_id – Required – The API key for your app
  26. * scope – Required – The list of required scopes (explained here: http://docs.shopify.com/api/tutorials/oauth)
  27. * redirect_uri – Optional – The URL that the merchant will be sent to once authentication is complete. Defaults to the URL specified in the application settings and must be the same host as that URL.
  28. We've added the create_permision_url method to make this easier, first instantiate your session object:
  29. session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com")
  30. Then call:
  31. scope = ["write_products"]
  32. permission_url = session.create_permission_url(scope)
  33. or if you want a custom redirect_uri:
  34. permission_url = session.create_permission_url(scope, "https://my_redirect_uri.com")
  35. 4. Once authorized, the shop redirects the owner to the return URL of your application with a parameter named 'code'. This is a temporary token that the app can exchange for a permanent access token. Make the following call:
  36. POST https://SHOP_NAME.myshopify.com/admin/oauth/access_token
  37. with the following parameters:
  38. * client_id – Required – The API key for your app
  39. * client_secret – Required – The shared secret for your app
  40. * code – Required – The token you received in step 3
  41. and you'll get your permanent access token back in the response.
  42. There is a method to make the request and get the token for you:
  43. token = session.request_token(code)
  44. Which will request the token, save it to the session object and return it. For future sessions simply pass the token in when creating the session object:
  45. session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com", token)
  46. 5. The session must be activated before use:
  47. ShopifyAPI::Base.activate_session(session)
  48. 6. Now you're ready to make authorized API requests to your shop! Data is returned as ActiveResource instances:
  49. shop = ShopifyAPI::Shop.current
  50. # Get a specific product
  51. product = ShopifyAPI::Product.find(179761209)
  52. # Create a new product
  53. new_product = ShopifyAPI::Product.new
  54. new_product.title = "Burton Custom Freestlye 151"
  55. new_product.product_type = "Snowboard"
  56. new_product.vendor = "Burton"
  57. new_product.save
  58. # Update a product
  59. product.handle = "burton-snowboard"
  60. product.save
  61. Alternatively, you can use #temp to initialize a Session and execute a command which also handles temporarily setting ActiveResource::Base.site:
  62. products = ShopifyAPI::Session.temp("SHOP_NAME.myshopify.com", token) { ShopifyAPI::Product.find(:all) }
  63. 8. If you want to work with another shop, you'll first need to clear the session::
  64. ShopifyAPI::Base.clear_session
  65. === Console
  66. This package also includes the `shopify` executable to make it easy to open up an interactive console to use the API with a shop.
  67. 1. Obtain a private API key and password to use with your shop (step 2 in "Getting Started")
  68. 2. Use the `shopify` script to save the credentials for the shop to quickly log in.
  69. shopify add yourshopname
  70. Follow the prompts for the shop domain, API key and password.
  71. 3. Start the console for the connection.
  72. shopify console
  73. 4. To see the full list of commands, type:
  74. shopify help
  75. == Using Development Version
  76. Download the source code and run:
  77. rake install
  78. == Additional Resources
  79. http://api.shopify.com <= Read the tech docs!
  80. http://ecommerce.shopify.com/c/shopify-apis-and-technology <= Ask questions on the forums!
  81. == Copyright
  82. Copyright (c) 2012 "Shopify inc.". See LICENSE for details.