npm package: @sobol/sdk
Description: A SDK for writing Sobol apps.
Author: Sobol
Latest version: 2.0.0

# @sobol/sdk

A JavaScript SDK for developing integrations with Sobol.

For our **Starter App**, see [@sobol/app](https://www.npmjs.com/package/@sobol/app).

## Installation

```bash
npm install @sobol/sdk
```

## Webhook

To bootstrap a webhook, use the `webhook` class as follows:

```javascript
const Client = require('@sobol/client');
const Webhook = require('@sobol/sdk/webhook');
const {
  WEBHOOK_MESSAGE_TYPE
} = require('@sobol/sdk/constants');

// === Configure Webhook ===
Webhook.configure({
  url: 'https://example.com/production/events/sobol',
  events: [
    'goal:created',
    'goal:updated',
    'goal:deleted',
  ],
  client: Client, // must be already configured
});

// === Event Handler ===
// - upon receiving some webhook event, verify handshake
const msg = '<some message object from sobol>';
const { type } = msg;

if (type === WEBHOOK_MESSAGE_TYPE.UrlVerification) {
  Webhook.verify(msg); // perform handshake
} else {
  console.log(msg);
}
```

### Options

| Option           | Type     | Description                                                  | Default                                   |
|------------------|----------|--------------------------------------------------------------|-------------------------------------------|
| `url`            | String   | Webhook URL                                                  | `''`                                      |
| `events`         | String   | Webhook Events                                               | `[]`                                      |
| `client`         | Object   | [@sobol/client](https://www.npmjs.com/package/@sobol/client) |                                           |

## Webhook Server

To bootstrap a webhook server, use the `webhook/server` class as follows:

```javascript
const Webhook = require('@sobol/sdk/webhook');
const WebhookServer = require('@sobol/sdk/webhook/server');

// === Server Setup ===
WebhookServer.configure({
  port: 8080,
  webhook: Webhook, // must be already configured
}, (/* server, app, router */) => {
  // customize express app here
});

// === Register Webhook Handler ===
WebhookServer.registerWebhook((msg) => {
  log(`[${APP_NAME}] ... sobol webhook event received:`, JSON.stringify(msg));
});

// === Start Server ===
WebhookServer.listen();
```

### Options

| Option           | Type     | Description                                                  | Default                                   |
|------------------|----------|--------------------------------------------------------------|-------------------------------------------|
| `port`           | Number   | Port to listen                                               | `8085`                                    |
| `prefix`         | String   | Prefix for route                                             | `/:env/events`                            |
| `webhook`        | Object   | [@sobol/sdk/webhook](#webhook)                               |                                           |

## Help and Feedback

For help and feedback, please send us an email to team@sobol.io and we will respond as soon as possible.