# @dispatch/dispatch-node-sdk

> High- and low-level libraries for interacting with the Dispatch API

Latest version **0.0.4** (published 2016-05-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dispatch/dispatch-node-sdk
pnpm add @dispatch/dispatch-node-sdk
yarn add @dispatch/dispatch-node-sdk
bun add @dispatch/dispatch-node-sdk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2016-05-01 |
| First published | 2016-05-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dispatch Technologies, Inc. |
| Maintainers | aldeed, derekcaneja, jperl, jraede, raix |

## Links

- npm: https://www.npmjs.com/package/@dispatch/dispatch-node-sdk
- npm.io page: https://npm.io/package/@dispatch/dispatch-node-sdk

## Dependencies (3)

- [moment](https://npm.io/package/moment.md) ^2.13.0
- [simplecheck](https://npm.io/package/simplecheck.md) ^0.1.2
- [isomorphic-fetch](https://npm.io/package/isomorphic-fetch.md) ^2.2.1

## Recent versions

- 0.0.4 (latest) — 2016-05-01
- 0.0.3 — 2016-05-01
- 0.0.2 — 2016-05-01

## README

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`.

```javascript
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:

```javascript
client.setBearerToken(bearerToken, refreshToken);
```

#### Log in with username/password
```javascript
client.loginEmailPassword(email, password).then(token => {
  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
```javascript
client.requestVerificationCode('+15555555555').then(() => {
  alert('Verification code will be sent to your phone!');
}).catch(err => alert('Error getting verification code'));

// Later...
client.loginPhoneNumber('+15555555555', verificationCode).then(token => {
  alert('Got bearer token: ' + token);
}).catch(err => alert('Error logging in!'));
```

### Interacting with Data
By default, the SDK wraps each entity in a `Model` class, (very similar to a Backbone model), which exposes methods like `refresh`, `set`, `get`, and `save`.

#### Get a list of models
For example, get a list of unscheduled jobs:

```javascript
client.getCollection('/v1/jobs', {
  status_eq: 'unscheduled'
}).then(jobs => {
  jobs.forEach(job => console.log('Got job ID ' + job.get('id')));
}).catch(err => alert('Error loading jobs!'));
```

#### Updating a model
Once you have a model, you can modify it and save it:

```javascript
model.set('status', 'scheduled');

model.save().then(() => {
  alert('Saved!');
}).catch(err => alert('Error saving job!'));
```

#### 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:

```javascript
client.getModel('/v1/jobs', 1)
.then(job => alert('Job 1 has status: ' + job.get('status')))
.catch(err => alert('Error loading job #1'));
```

## Raw Client
Use the low-level raw client on the server-side for shared-key authentication:

```javascript
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));
```

---
_Source: https://npm.io/package/@dispatch/dispatch-node-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
