Only show these results:

Set up local webhook testing for the Nylas Node.js SDK

Nylas SDKs support local webhook testing. When you implement this feature, the SDKs create a tunnel connection to a websocket server and register it as a webhook callback to your Nylas account.

What you'll learn

This page discusses how to set up local webhook testing for the Nylas Node.js SDK.

Step 1: Initialize the Nylas object

The Nylas object provides access to every resource in the Nylas API. Before you make any API requests, you must initialize the Nylas object with your client ID, client secret, and access token:

  1. Call the .config() function and pass your <CLIENT_ID> and <CLIENT_SECRET>.
  2. Call the .with() function and pass your <ACCESS_TOKEN>.

All together, your code should look like the example below.

const Nylas = require('nylas');

Nylas.config({
clientId: <CLIENT_ID>,
clientSecret: <CLIENT_SECRET>
});

const nylas = Nylas.with(<ACCESS_TOKEN>);

Step 2: (Optional) Change the base API URL

You can choose to change the base API URL depending on your location:

Location API URL Scheduler API URL
United States (Oregon) https://api.nylas.com https://api.schedule.nylas.com
Europe (Ireland) https://ireland.api.nylas.com https://ireland.api.schedule.nylas.com

For more information, see Data residency and Migration guide for data centers.

If you're using the Node.js SDK v6.x and the Nylas API v3 Beta: The EU region is currently not available.

To change the API URL, pass the API_URL parameter to Nylas.config():

const Nylas = require('nylas');

Nylas.config({
clientId: <CLIENT_ID>,
clientSecret: <CLIENT_SECRET>,
apiServer: regionConfig[Region.Ireland].nylasAPIUrl
});

const nylas = Nylas.with(<ACCESS_TOKEN>);

If you're using the Nylas EU region: The base API URL defaults to the US region, but you can specify apiServer: regionConfig[Region.Ireland].nylasAPIUrl to use the EU region. See Data Residency for more information.

Step 3: Initialize the tunnel connection

Use the following code to call openWebhookTunnel and initialize the tunnel connection.

openWebhookTunnel: (config: OpenWebhookTunnelOptions) => Promise<Webhook>   

Step 4: Set callbacks and configuration parameters

You can set methods to override the websocket client's callback methods and set its configuration parameters. You must set the onMessage callback method to parse delta events from the webhook server. The other callbacks and configuration parameters are optional.

Nylas supports the following callbacks and configuration parameters:

export interface OpenWebhookTunnelOptions {
onMessage: (msg: WebhookDelta) => void; // Runs on receiving a notification.
onConnectFail?: (error: Error) => void; // Runs when an error occurs during initial connection.
onError?: (error: Error) => void; // Runs when an error is encountered during the server runtime.
onClose?: (wsClient: WebSocketClient) => void; // Runs after the connection has closed.
onConnect?: (wsClient: WebSocketClient) => void; // Runs when the initial server connection is established.
region?: Region; // The region to connect to. Supported regions are 'us' (US) and 'ireland' (EU). Defaults to 'us'.
triggers?: WebhookTriggers[]; // The list of webhook triggers to listen on. Defaults to all.
}

Example: Set up local webhook testing

The following example sets up local webhook testing for the Nylas Node.js SDK.

const Nylas = require('nylas');
const { openWebhookTunnel } = require('nylas/lib/services/tunnel');

// Initialize an instance of the Nylas SDK using the client credentials.
Nylas.config({
clientId: "CLIENT_ID",
clientSecret: "CLIENT_SECRET",
});

const nylas = Nylas.with(<ACCESS_TOKEN>);

// Define the callback for the onMessage event.
const onMessageListener = (delta) => {
if(delta.type === WebhookTriggers.MessageUpdated) {
console.log("Got message from webhook. Data: " + delta.objectData);
}
}

// Pass your config to create, register, and open the webhook tunnel for testing.
openWebhookTunnel({
onMessage: onMessageListener
}).then(r => console.log("Webhook connected and active. " + r))

More resources