This section covers how to get started with making your first API request. You may use the API directly with a cURL request or make use of one of our supported SDKs.

1. Install SDK (recommended)

Although you can interact with the API directly using a plain old HTTP request, developers often rely on one of our SDKs.

Currently we actively maintain the JavaScript and Swift SDKs, and with the support of our open source community, host a variety of other SDKs on our GitHub.

Following the steps below, you can quickly get up and running with your API keys.

JavaScript SDK
Swift SDK

If you're using Yarn or npm to manage dependencies, inside the same directory as package.json you will want to run:

yarn add @moltin/sdk # npm install --save @moltin/sdk

If you are using webpack you will need to add the following to your projects configuration file.

node: {
fs: 'empty'
}

The Swift SDK is available to install via Cocoapods, Carthage and Swift Package Manager.

CocoaPods

Add the following to your Podfile:

pod 'Moltin', '~> 3.0.11'

Carthage

Add the following to your Cartfile:

github "Moltin/ios-sdk" ~> 3.0.11

Swift Package Manager

Add the following to your dependencies value in Package.swift:

dependencies: [
.package(url: "https://github.com/moltin/ios-sdk.git", from: "3.0.11")
]

2. Create a client (recommended)

One of the biggest advantages of using an SDK is the built-in support for storing and renewing your authentication token.

JavaScript SDK
JavaScript in the Browser
Swift SDK

With the SDK installed, you'll next want to import and instantiate the Moltin gateway.

import { gateway as MoltinGateway } from '@moltin/sdk'
// const MoltinGateway = require('@moltin/sdk').gateway
const Moltin = MoltinGateway({
client_id: 'XXXX'
})

You can use the UMD build directly from unpkg inside your HTML. This is great for CodePen and small applications.

You should not use client_credentials when authenticating this way.

Inside a <script /> tag you can instantiate the Moltin gateway using the global moltin.

<script src="https://unpkg.com/@moltin/sdk"></script>
<script>
const Moltin = moltin.gateway({
client_id: 'XXX'
})
</script>
let moltin = Moltin(withClientID: "<your client ID>")

3. Make your first API request

You'll need to authenticate to make your first API request, and every request must contain your Bearer token inside the header. If you're using an SDK, these silently handle authentication so you don't need to call the authenticate method directly.

Below, are some example API requests you can make. Try them out!

cURL
Node
Client-side
Swift SDK

With plain old HTTP request you can make a request to the API. First you will need to authenticate to get your access token.

Authenticate

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

Create a Product

Using the token returned from the response above, apply it below instead of XXXX.

curl -X POST https://api.moltin.com/v2/products \
-H "Authorization: Bearer XXXX" \
-d $'{
"data": {
"type": "product",
"name": "Deck Shoe",
"slug": "deck-shoe",
"sku": "deck-shoe-001",
"description": "Modern boat shoes were invented in 1935 by American Paul A. Sperry",
"manage_stock": true,
"price": [
{
"amount": 5891,
"currency": "USD",
"includes_tax": true
}
],
"status": "live",
"commodity_type": "physical"
}
}'

Get all Products

Make sure to apply your authentication token instead of XXXX.

curl -X GET https://api.moltin.com/v2/products \
-H "Authorization: Bearer XXXX"

With Node.js installed, create a file named index.js with the following:

const MoltinGateway = require('@moltin/sdk').gateway
const Moltin = MoltinGateway({
client_id: 'X',
client_secret: 'X'
})
Moltin.Products.All().then(products => {
console.log(products)
})

Save the file and run node index.js to see products logged to the console.

Using a client-side library like React, React Native, Vue or Angular simply add the JS SDK as a dependency to your project.

We don't recommend you authenticate with client credentials using this method

If you head to the Dashboard, add a product with an image and associated category or brand, you'll be able to query the results in your client application using the code below.

Get all Products

import { gateway as MoltinGateway } from '@moltin/sdk'
const Moltin = MoltinGateway({
client_id: 'XXXX'
})
Moltin.Products.All().then(products => {
console.log(products)
})

Get all Products with main image, brands and categories

import { gateway as MoltinGateway } from '@moltin/sdk'
const Moltin = MoltinGateway({
client_id: 'XXXX'
})
Moltin.Products.With(['main_image', 'brand', 'category']).All().then(products => {
console.log(products)
})
let moltin = Moltin(withClientID: "<your client ID>")
self.moltin.product.include([.mainImage]).all { (result: Result<PaginatedResponse<[moltin.Product]>>) in
switch result {
case .success(let response):
DispatchQueue.main.async {
print("Products", response.data)
}
case .failure(let error):
print("Products error", error)
}
}
}