Dispatch Sender Node.js Library
The Dispatch Sender Node library provides convenient access to the Dispatch Sender API from applications written in server-side JavaScript.
Installation
Install the package with:
npm install dispatch-node --save
Usage
The package needs to be configured with your account's API Key, which is
available in the Dispatch Dashboard.
const Dispatch = require("dispatch-node");
const dispatch = new Dispatch("key_EHKD5tbatzX59DArnTBjhr46G5n1f4aJkvwPByft");
dispatch.deliveries
.list()
.then((response) => console.log(response.data))
.catch((error) => console.error(error));
Or using ES modules and async
/await
:
import Dispatch from "dispatch-node";
const dispatch = new Dispatch("key_EHKD5tbatzX59DArnTBjhr46G5n1f4aJkvwPByft");
(async () => {
const response = await dispatch.deliveries.list();
console.log(response.data);
})();
Using Promises
Every method returns a chainable promise which can be used instead of a regular callback:
const sender = {
name: "Dispatch Roasters"
email: "roasters@getdispatch.app",
location_id: "loc_1uzCQL4cTjjw3vRfE2qYk1"
}
const recipient = {
name: "Jamie Jones"
email: "jamie.jones@getdispatch.app",
address: {
address_line1: "500 7th Ave",
city: "New York",
state: "NY"
zipcode: "10018"
}
}
const parcel = {
length: 10
width: 10
height: 10
weight: 5.5
}
dispatch.deliveries
.create(sender, recipient, [parcel])
.then(response => {
const delivery = response.data
const rate = delivery.rates[0]
return dispatch.deliveries.buy(delivery.id, rate.id)
}).then(response => {
}).catch(err => {
})