• Log in
  • Evaluate
  • Contact Us

Get in touch  

Getting started with Python

Version 1 Guide

1

Install the SDK with PIP

You can install the Moltin SDK from the Python package manager

$ pip install moltin

2

Install the SDK from source
Alternatively, you can install the SDK from source
$ git clone https://github.com/moltin/python-sdk.git
      $ cd python-sdk
      $ python setup.py install

3

Initialize Moltin

Initialize the Moltin object with your client_id and client_secret. You can find your store keys in your store dashboard.

from moltin.moltin import Moltin
      
      m = Moltin("XXXX", "XXXX")

4

Authenticate

Authenticate by calling the authenticate method.

access_token = m.authenticate()
      # This returns an AccessToken object
      # access_token.token: the token string
      # access_token.has_expired(): has the token expired
      #
      # The access token is automatically passed to subsequent requests,
      # so you shouldn't normally need to use the returned token
      # except when persisting in a session or db

A simple checkout flow

1

Get products

Now that we’ve authenticated with the Moltin API lets get one of our sample products. Please note you will need to create a product first via the API or store management dashboard.

This API call should contain an array of products. A good place to display this data is on a product or listing page.

product = m.Product.find_by({'slug': 'decorative-hedgehogs'})

2

Add to cart

Using a product ID we can insert a quantity of this product into the cart with one simple request. The first parameter passed is the product ID, the second the quantity to be added to the cart. A third parameter can be added for modifiers and variations (e.g. size and color) if these are set up.

This response should contain an array of the individual cart item data e.g. title, quantity, price. A good example of integrating this would be to add a simple button or form to the product page that posts the product ID and quantity to this endpoint.

cart = m.Cart()  # create a new cart
      cart.add_item({
         'id': product_id
         'quantity': 1,
      })

3

Get cart contents

Now that there is at least one product in the cart let’s get the full cart contents.

This response should contain an array of all cart items and cart totals. A good place to show this data would be on the cart or orders page, or even a widget in the header of your website.

items = cart.contents()

4

Convert to order

When the customer is ready to checkout we need to convert the cart to an order. This call lets you define the payment gateway and conditional order parameters such as customer, billing and shipping addresses.

This call will return an object containing data for the newly created order.

order = cart.checkout({
       'gateway': 'dummy',
       'customer': {
         'first_name': 'Jon',
         'last_name': 'Doe',
         'email': 'jon.doe@gmail.com'
       },
       'bill_to': {
           'first_name': 'Jon',
           'last_name': 'Doe',
           'address_1': '123 Sunny Street',
           'address_2': 'Sunnycreek',
           'city': 'Sunnyvale',
           'county': 'California',
           'country': 'US',
           'postcode': 'CA94040',
           'phone': '6507123124'
       },
       'ship_to': 'bill_to',
       'shipping': 'free_shipping'
      });

5

Process payment

Once we’ve converted a cart into an order we’re now ready to process a payment. In this example, we’ve used the dummy gateway so we just need to provide some card details. The data you need to provide in this step depends on your chosen gateway.

Congratulations, if you made it this far you’ve implemented a simple step by step checkout!

checkout = m.Checkout.payment('purchase', order['result']['id'], {
        'data': {
          'number': '4242424242424242',
          'expiry_month': '02',
          'expiry_year': '2020',
          'cvv': '123'
        }
      });

Ready to get started?

It always starts with a conversation.

Get in touch