Version:
Only show these results:

Test webhooks locally with the Nylas Java SDK

🚀 Local webhook testing for the v3 Kotlin/Java SDK is coming soon.

The SDKs for Nylas v2 support local webhook testing. When you test your webhooks locally, the SDK creates a tunnel connection to a websocket server and registers it to your Nylas account as a webhook callback.

What you'll learn

In this tutorial, you'll learn how to set up your local webhook testing for the Nylas Java SDK.

Step 1: Configure the API client

  1. Import the NylasClient class, and create a new instance.
  2. Import the NylasApplication class, and pass the NYLAS_CLIENT_ID and NYLAS_CLIENT_SECRET.
import com.nylas.NylasClient;
import com.nylas.NylasApplication;

public class NylasWebhook {
public static void main(String[] args) throws Exception {
NylasClient client = new NylasClient();
NylasApplication application = client.application("NYLAS_CLIENT_ID", "NYLAS_CLIENT_SECRET");
}
}

Step 2: Initialize the tunnel connection

Call WebhookTunnel.Builder and build a new WebhookTunnel instance.

public WebhookTunnel.Builder(NylasApplication app, WebhookHandler webhookHandler)   

The WebhookTunnel instance requires the following:

  • A configured API client (NylasApplication)
  • A class that implements WebhookHandler to handle notifications.

Set callbacks and configuration parameters

To listen on new events and set callbacks, provide a class that implements WebhookHandler.

The onMessage callback is required, while other callbacks and configuration parameters are optional. The onMessage callback returns a parsed delta event from the webhook server. The following are supported functions for callbacks:

void onMessage(Notification.Delta delta) // Executes on receiving a notification
void onOpen(short httpStatusCode) // Executes when the initial server connection has been established
void onClose(int code, String reason, boolean remote) // Executes after the connection has closed
void onError(Exception ex) // Executes when an error is encountered during the server runtime

You can add the following configurations to the builder process before building the WebhookTunnel instance:

  region(String region) // The region to connect to. Supported regions are 'us' (US) and 'ireland' (EU). Defaults to US.
triggers(Webhook.Trigger... triggers) // The list of webhook triggers to listen on. Defaults to all.

Learn more about Webhook notification triggers.

Example

import com.nylas.NylasClient;
import com.nylas.NylasApplication;
import com.nylas.Notification.Delta;
import com.nylas.services.Tunnel;

class HandleNotifications implements WebhookTunnel.WebhookHandler {
@Override
public void onMessage(Notification.Delta delta) {
log.info("onGot message from webhook. Data: " + notification.getAttributes());
}

@Override
public void onOpen(short httpStatusCode) {
log.info("OnOpen");
}
}

public class NylasWebhook {
public static void main(String[] args) throws Exception {
NylasClient client = new NylasClient();
NylasApplication application = client.application("NYLAS_CLIENT_ID", "NYLAS_CLIENT_SECRET");

new WebhookTunnel.Builder(application, new HandleNotifications())
.build()
.connect();
}
}

More resources