README.rdoc 6.0 KB

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