Install the SDK

Install the SDK from npm.

Instantiate

Require the library and instantiate a new Moltin client.
You will need a client ID to authenticate with our API. You can find your store keys in your store dashboard.

1
npm install @moltin/sdk
      
1
      2
      3
      4
      5
      6
const moltin = require('@moltin/sdk');
      
      const Moltin = moltin.gateway({
        client_id: XXXX,
        client_secret: XXXX
      });
      
Account management

Account management

Manage features, admin users and billing.

Visit accounts
Store management

Store management

Manage your inventory and store settings.

Visit dashboard

A simple checkout flow

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.

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.

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.

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.

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!

1
const products = Moltin.Products.All();
      
1
      2
      3
Moltin.Cart.AddProduct(products.data[0].id, 3).then((item) => {
       alert(`Added ${item.name} to your cart`);
      });
      
1
const cart = Moltin.Cart.Items();
      
1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
Moltin.Cart.Checkout({
        customer: {
          name: 'John Doe',
          email: 'john@doe.co'
        },
        billing_address: {
          first_name: 'John'
          last_name: 'Doe',
          line_1: '123 Sunny Street',
          line_2: 'Sunnycreek',
          county: 'California',
          postcode: 'CA94040',
          country: 'US'
        },
        shipping_address: {
          first_name: 'Jon',
          last_name: 'Doe',
          line_1: '123 Sunny Street',
          line_2: 'Sunnycreek',
          county: 'California',
          postcode: 'CA94040',
          country: 'US'
        }
      });
      
1
      2
      3
      4
      5
      6
      7
      8
      9
      10
Moltin.Orders.Payment(order.id, {
        gateway: 'stripe',
        method: 'purchase',
        first_name: 'John',
        last_name: 'Doe',
        number: '4242424242424242',
        month: '02',
        year: '2020',
        verification_value: '123'
      });
      
Moltin documentation

Documentation

Get in-depth details about our API, features and how to use them.

View the docs
Moltin community forum

Community forum

Sign up to our community forum to get help from other users and the Moltin team.

Join forum
Moltin help and support

Help & support

For help with technical issues, please open a ticket on our support portal.

Get help