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.
Head to the Moltin Dashboard, sign in, select your project and take note of your client_id
.
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 yourclient_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 youraccess_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.