ProgramMatek: Shopify Python API

The ShopifyAPI library enables Python developers to access the admin section of stores programmatically. By utilizing pyactiveresource, the API offers a user interface that is reminiscent of the ruby Shopify API gem. XML is used to transmit the data over HTTP, adhering to REST principles as closely as possible.

Installation

To effortlessly install or upgrade to the latest release, simply use pip or easy_install.

Usage

Requirements

All API usage takes place through Shopify applications. These applications can be created by shop owners for their own stores or by Shopify Partners for use by other shop owners. Here’s how to create applications for each:

  • Shop owners can create applications for themselves through their own admin. Create a private app
  • Shopify Partners can create applications through their admin. Create an app

Getting Started

ShopifyAPI utilizes pyactiveresource to communicate with the REST web service. Before anything else, pyactiveresource must be configured with the fully authorized URL of a specific store. Follow the steps below to obtain the URL:

  1. Create a new application in either the partners admin or your store admin. If it’s a private App, you’ll need the API_KEY and the PASSWORD. Otherwise, you’ll need the API_KEY and SHARED_SECRET.
  2. For a private App, simply set the base site URL as instructed. That’s it! You’re done. Skip to step 6 and start using the API!
  3. For a partner App, you’ll need to supply two parameters to the Session class before instantiation.
  4. In order to access a shop’s data, apps need an access token from that specific shop. This is a two-stage process. Redirect the user to the URL specified below before interacting with a shop for the first time:
    • client_id (Required): The API key for your app.
    • scope (Required): The list of required scopes.
    • 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.
    • Use the create_permision_url method to simplify this process. First, instantiate your session object, then call the method. You can also provide a custom redirect_uri if desired.
  5. 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:
    • client_id (Required): The API key for your app.
    • client_secret (Required): The shared secret for your app.
    • code (Required): The code you received in step 3.
    • Your permanent access token will be returned in the response.
    • You can use the request_token method to make the request and get the token for you. Pass all the parameters received from the previous call (shop, code, timestamp, signature) as a dictionary, and the method will verify the params, extract the temp code, and then request your token. The method will save the token to the session object and return it. For future sessions, simply pass the token when creating the session object.
  6. Activate the session before use.
  7. Now you’re ready to make authorized API requests to your shop! The data is returned as ActiveResource instances. Alternatively, you can use the temp method to initialize a Session and execute a command, which also handles temporarily setting ActiveResource::Base.site.
  8. If you want to work with another shop, you’ll first need to clear the session.
See also  Introducing the Data Enrichment API: Enhance Your ContentKing Experience

Console

This package also includes the shopify_api.py script, which allows you to easily open an interactive console to use the API with a shop. Follow these steps:

  1. Obtain a private API key and password to use with your shop (step 2 in “Getting Started”).
  2. Use the shopify_api.py script to save the credentials for the shop, making it quick to log in. The script utilizes PyYAML to save and load connection configurations in the same format as the ruby shopify_api. Follow the prompts for the shop domain, API key, and password.
  3. Start the console for the connection.
  4. To see the full list of commands, type:

Using the Development Version

To build the development version, use the specified command. Then, to install the package, use pip or easy_install.

Note: When running from the source tree, use the bin/shopify_api.py script. It will add the lib directory to the start of sys.path, ensuring that the installed version won’t be used.

Limitations

Currently, there is no support for asynchronous requests or persistent connections.

Additional Resources

Copyright

Copyright (c) 2012 “Shopify inc.”. See LICENSE for details.