Dispatch JavaScript SDK
High- and low-level libraries for interacting with the Dispatch API.
Installation
$ npm install --save dispatch-node-sdk
Usage
Client SDK
The client SDK is meant for use on the browser. It assumes that there is only one active bearer token at a time - for server-level use, please use the raw Client
.
Instantiation
Create a new instance of the client with your client_id
and client_secret
.
import Dispatch from 'dispatch-node-sdk';
const dispatchClient = new Dispatch(clientID, clientSecret, 'https://api.dispatch.me');
Authentication
Set the Bearer Token
You can manually set the API bearer token if it came from an external source:
client.setBearerToken(bearerToken, refreshToken);
Log in with username/password
client.loginEmailPassword(email, password).then(() => {
return client.identifyUser();
}).then(user => {
console.log('Current user is', user);
}).catch(err => console.error('Failed to log in:', err));
Log in with phone + verification code
client.requestVerificationCode('+15555555555').then(() => {
alert('Verification code will be sent to your phone!');
}).catch(err => alert('Error getting verification code'));
client.loginPhoneNumber('+15555555555', verificationCode).then(token => {
alert('Got bearer token: ' + token);
}).catch(err => alert('Error logging in!'));
Login with limited-use auth token
client.loginAuthToken(token).then(() => {
return client.identifyUser();
}).then(user => {
console.log('Current user is', user);
}).catch(err => console.error('Failed to log in:', err));
Interacting with Data
Get a list of models
You can use the getCollection
method directly, like this:
client.getCollection('/v1/jobs', {
filter: {
status_eq: 'unscheduled',
}
}).then(jobs => {
jobs.forEach(job => console.log('Got job ID ' + job.get('id')));
}).catch(err => alert('Error loading jobs!'));
Or, use the defined collection, like this:
client.entities.jobs.get({
filter: {
status_eq: 'unscheduled',
}
});
Creating a Model
client.entities.jobs.create({
title: 'New job!',
}).then(...);
Retrieving a single model
Sometimes you may want to just get a single model instead of an entire collection. For example, to retrieve job #1:
client.getModel('/v1/jobs', 1)
.then(job => alert('Job 1 has status: ' + job.status))
.catch(err => alert('Error loading job #1'));
Similarly:
client.entities.jobs.getOne(1).then(...)
Updating a model
Once you have a model, you can modify it and save it:
model.status = 'new status';
client.entities.jobs.update(1, model).then(...);
Entity-specific Functions
Some entities have additional abstracted convenience functions. For example:
client.entities.job(123).addNote('note text').then(...)
Raw Client
Use the low-level raw client on the server-side for shared-key authentication:
import { RawClient, AUTH_MODE_HMAC } from 'dispatch-node-sdk';
const client = new RawClient({
authMode: AUTH_MODE_HMAC,
hmacCredentials: {
userID: 10,
userType: 'user',
secret: '<secret key>',
},
host: 'https://api-sandbox.dispatch.me',
});
client.get('/v1/jobs')
.then(jobs => console.log('Got %d jobs', jobs.length))
.catch(err => console.error(err));