Introducing a new JavaScript request client

Last updated: 18 Dec 2018
Today we are excited to share with you a new lightweight JavaScript client that puts the power in your hands.

The library was built with simplicity in mind and was designed to abstract the common things we see developers doing when using Moltin.

We needed a way to generate an authentication token, make a request with that token and return the response in JSON, without having to worry about the mechanics involved.

The new Moltin JavaScript client is perfect when developing client-side applications and for extending Moltin with webhooks inside a serverless function.

Overview

The new @moltin/request library exports only 6 functions:

  • get()
  • post()
  • put()
  • delete()
  • authenticate()
  • createCartIdentifier()

You will most likely always be using get() and post() on within your client application to get products and post to cart and checkout. While interacting with creating, updating and deleting resources on the server side, either inside an application or a serverless function.

To get started with the library, you will want to install via Yarn or NPM.

 

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

Once you have the dependency installed, you can now import the library and instantiate a new client.

  const { createClient } = require('@moltin/request')
const client = new createClient({
client_id: '...'
})

How to use

You can find all the available functions and parameters over at GitHub along with some example client and server applications that show just how easy it is to import and connect with your existing applications.

If we wanted to get all products, it's as simple as:

 

  const { createClient } = require('@moltin/request')
// import { createClient } from '@moltin/request'
const client = new createClient({
client_id: '...'
})
client
.get('products')
.then(console.log)
.catch(console.error)

It's just as easy to get a product by ID:

  const { createClient } = require('@moltin/request')
// import { createClient } from '@moltin/request'
const client = new createClient({
client_id: '...'
})
client
.get('products/:id')
.then(console.log)
.catch(console.error)

If you have any feedback, please reach out via the forum or over on GitHub issues.