Skip to content

NylasConnect class overview

The NylasConnect class is the core of the Nylas Connect library. It provides methods for OAuth authentication, token management, and grant creation and management. The primary use case for NylasConnect is integrating with external identity providers (IDPs) like Auth0, Clerk, Google Identity, and WorkOS, allowing you to link your users’ existing identities with their email accounts.

When using NylasConnect with an external identity provider, configure it with the identityProviderToken callback:

import { NylasConnect } from "@nylas/connect";
const nylasConnect = new NylasConnect({
clientId: process.env.NYLAS_CLIENT_ID!,
redirectUri: process.env.NYLAS_REDIRECT_URI!,
apiUrl: process.env.NYLAS_API_URL || "https://api.us.nylas.com",
// Provide your IDP token for authenticated API calls
identityProviderToken: async () => {
return await getYourIdpToken();
},
environment: "production",
persistTokens: true,
debug: false,
logLevel: "error",
});
// Set up authentication state monitoring
nylasConnect.onConnectStateChange((event, session, data) => {
console.log("Auth state changed:", event);
switch (event) {
case "SIGNED_IN":
// Handle successful connection
console.log("Connected:", session?.grantInfo?.email);
break;
case "SIGNED_OUT":
// Handle disconnection
console.log("Disconnected");
break;
case "CONNECT_ERROR":
// Handle authentication errors
console.error("Auth error:", data?.error);
break;
}
});

The NylasConnect constructor accepts a ConnectConfig object with the following options.

Property
Toggle details
Section titled “External identity provider integration (Recommended)”

The recommended way to use NylasConnect is with an external identity provider. This pattern allows you to:

  • Unified authentication: Users authenticate once with your IDP (Auth0, Clerk, Google, WorkOS, etc.)
  • Seamless email access: Link their email accounts to their existing identity
  • Simplified API calls: Use IDP tokens instead of managing separate API keys
  • Enhanced security: Leverage your IDP’s security features and compliance

Get started: Follow the External Identity Provider guide for implementation steps with your chosen provider.

You can also use NylasConnect for direct OAuth flows without an external IDP. This is suitable for:

  • Simple applications that only need email access
  • Prototypes and proof-of-concepts
  • Scenarios where you don’t have an existing authentication system

For standalone usage, see the Nylas Connect overview for basic implementation.

The NylasConnect class includes the following methods. Click on any method name to view its detailed documentation.

ConnectError extends the standard Error interface and adds fields for better error handling.

PropertyTypeDescription
codestringAn error code for programmatic error handling.
descriptionstring?A human-readable description of the error.
docsUrlstring?A link to relevant documentation.
fixstring?A suggested solution for resolving the error.
originalErrorError?The original error, if the ConnectError wraps another error.
NameEnvironment
DescriptionYour project’s deployment environment.
Enumdevelopment | staging | production

Default scope settings for different providers.