Skip to content

Instantly share code, notes, and snippets.

@notrab
Last active September 25, 2018 08:44
Show Gist options
  • Save notrab/e0d0a5e6e50d38d7229695b8d2f734fe to your computer and use it in GitHub Desktop.
Save notrab/e0d0a5e6e50d38d7229695b8d2f734fe to your computer and use it in GitHub Desktop.

Authentication

Every request to Moltin requires an accompanying Bearer token. There are two token types available; implicit and client_credentials.

We're going to be using the implicit grant type for the next few steps. The implicit grant type can be thought of as READ ONLY and are mostly used client-side.

Every user is given their own API keys for every project they're invited to or create. These are required to authenticate.

Get your API keys

Head to the Moltin Dashboard, sign in, select your project and take note of your client_id.

How to authenticate

Now that we have our client_id we will be making our very first request. Requests throughout this tutorial will use curl, so there's no need to install an SDK.

Open Terminal, or your command-line equivelent and make the following request.

Replace XXXX with your client_id.

curl -X POST https://api.moltin.com/oauth/access_token \
     -d "client_id=XXXX" \
     -d "grant_type=implicit"

The request will return something similar to:

{
  "expires": 1537779121,
  "identifier": "implicit",
  "expires_in": 3600,
  "access_token": "0e82127a8d9c20abb3d6e48561cbfe81448956ab",
  "token_type": "Bearer"
}

Make note of access_token. This is your implicit Bearer token that is required for all future requests to the API.

Make note of the expiry time, you'll need a new access_token when it expires.

## How to make an authorised request

Now let's go ahead and make an example request. We'll make a request to a new Cart with the ID abc, passing along the Authorization header.

Replace XXXX with your access_token.

curl https://api.moltin.com/v2/carts/abc \
     -H "Authorization: Bearer XXXX"

The URL parameter abc must be unique to your project. As this is the first time you've made a request, you will get the following request:

{
  "data":{
    "id":"a054e10e-5fb7-4a76-a496-c59638d45939",
    "type":"cart",
    "links":{
      "self":"https://api.moltin.com/carts/a054e10e-5fb7-4a76-a496-c59638d45939"
    },
    "meta":{
      "display_price":{
        "with_tax":{
          "amount":0,
          "currency":"",
          "formatted":"0"
        },
        "without_tax":{
          "amount":0,
          "currency":"",
          "formatted":"0"
        }
      },
      "timestamps":{
        "created_at":"0001-01-01T00:00:00Z",
        "updated_at":"0001-01-01T00:00:00Z"
      }
    }
  }
}

As we can see above, the cart is empty and in the next lesson we will add some items to the cart.

Cart

Since we don't have any products within our Moltin inventory, we'll go ahead and add a custom item to the Cart.

Custom items are a great choice if you manage your inventory outside of Moltin and slowly wish to migrate your product data.

Add to Cart

Replace XXXX with your access_token.

curl -X POST https://api.moltin.com/v2/carts/abc/items \
     -H "Authorization: Bearer XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "custom_item",
        "name": "T-Shirt",
        "sku": "tshirt-001",
        "description": "A Moltin branded T-shirt",
        "quantity": 1,
        "price": {
          "amount": 10000
        }
      }
    }'

Replace XXXX with your access_token.

curl -X POST https://api.moltin.com/v2/carts/abc/items \
     -H "Authorization: Bearer XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "custom_item",
        "name": "Jeans",
        "sku": "jeans-orange-001",
        "description": "Moltin branded jeans",
        "quantity": 1,
        "price": {
          "amount": 12500
        }
      }
    }'

Checkout

Now have a Cart with items, we can proceed to Checkout the order using the /checkout endpoint provided to us by the Moltin API.

To checkout, you must provide details of the person paying for and receiving on delivery.

There are two types of checkout, with and without customer ID. If you have a customer registered with Moltin, and they're logged into your application, you can easily assign orders to customers. In this example we won't be using a customer ID and instead pass along the name and email for the customer.

Checkout with Customer object

Replace XXXX with your access_token.

curl -X POST https://api.moltin.com/v2/carts/abc/checkout \
     -H "Authorization: Bearer XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
       "data": {
         "customer": {
           "email": "[email protected]",
           "name": "Jamie Barton"
         },
         "billing_address": {
           "first_name": "John",
           "last_name": "Doe",
           "company_name": "Moltin",
           "line_1": "2nd Floor British India House",
           "line_2": "15 Carliol Square",
           "city": "Newcastle upon Tyne",
           "postcode": "NE1 6UF",
           "county": "Tyne & Wear",
           "country": "UK"
         },
         "shipping_address": {
           "first_name": "John",
           "last_name": "Doe",
           "phone_number": "(555) 555-1234",
           "company_name": "Moltin",
           "line_1": "2nd Floor British India House",
           "line_2": "15 Carliol Square",
           "city": "Newcastle upon Tyne",
           "postcode": "NE1 6UF",
           "county": "Tyne & Wear",
           "country": "UK",
           "instructions": "Leave in porch"
         }
       }
    }'

Once you you send this request, your Cart will be converted to an unpaid Order.

The Order contains all of the information from the Cart, including the Cart Items which have now been transferred as Order Items.

The next step with an order is paying for your order, and that's what we'll do in the next video.

Welcome

In this short videos series I will walk you through creating your very first API request with Moltin.

We will cover:

  • Authenticating implicitly
  • Adding items to the Cart
  • How to Checkout a Cart
  • How to pay for your order

Please ask any questions on Twitter or on our forum at forum.moltin.com

Paying for your order

Welcome to Moltin - Join us on the journey to reinvent commerce

Hi Jamie

Welcome to Moltin. You're moments away from making your first API request.

Check out this short video series walking you authenticating, adding to cart and paying for your order using the command line.

Moltin: Your first API request

Make sure to visit our API reference and Developer Portal to kickstart your Moltin development.

If you have any questions on how to get stared, please join us on the forum.

Jamie Developer Success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment