Skip to content
Skip to main content

Quickstart: auth with @nylas/connect

Last updated:

@nylas/connect is a JavaScript library that connects a user’s mailbox to your app. It does 2 things for you: it runs the whole OAuth flow so you don’t build redirect routes or a callback handler, and it lets you keep addressing that user by the ID your app already has.

@nylas/connect is a browser and Node.js library that opens an OAuth popup, completes the PKCE exchange, stores the resulting tokens, and refreshes them in the background. It ships with zero runtime dependencies and handles 4 provider values directly: google, microsoft, imap, and icloud.

Without it, connecting a mailbox means writing an authorization redirect, a callback route, a code-for-token exchange, token storage, and refresh logic. The library replaces those 5 pieces with a single connect() call:

const result = await nylasConnect.connect({ method: "popup" });
console.log(result.grantInfo?.email);

If you omit provider, the user picks their own from the Nylas login screen. Pass it when you already know which mailbox you’re connecting.

Not if you connect through an identity provider. Nylas links the grant to the sub claim in your IdP’s JWT, so you keep querying by the user ID your app already stores. You call /v3/grants/me with that ID in a header instead of adding a grant_id column and a lookup to every query path.

That linkage comes from your IdP, so it isn’t available on every setup. The third option below signs its own JSON Web Key Set (JWKS) instead of using a hosted provider. Here’s what each of the 3 setups gets you:

SetupSkips the OAuth plumbingKeeps your user IDs
@nylas/connect with Auth0, Clerk, or WorkOSYesYes
@nylas/connect with a custom JWKS endpointYesYes, but you run the JWT infrastructure
@nylas/connect on its ownYesNo, you store the grant ID

Use an identity provider if you have one. The custom JWKS path works and is documented, but you take on issuing and rotating the signing keys yourself. Running the library on its own is the fastest way to a working prototype, and you can add an IdP later without changing how you call connect().

You need a Nylas account, an application client ID, and an identity provider account. This quickstart uses Auth0, and the pattern is identical for the other 4 options covered in the external IdP guides.

  • A Nylas account. Sign up if you don’t have one.
  • An identity provider account: Auth0, Clerk, Google Identity, WorkOS, or anything that exposes a JWKS endpoint.
  • Node.js and a browser to run the examples.

Auth0 needs to know which origin is allowed to request tokens. Create an application in the Auth0 Dashboard, then set the 2 URL fields to your local origin so the popup can return a token to the page that opened it.

  1. Set Allowed Callback URLs to http://localhost:3000.
  2. Set Allowed Web Origins to http://localhost:3000.
  3. Save your Domain and Client ID.

Nylas validates the origin that requests a token and the URI it redirects back to, so both of these 2 values must be registered before the first connect() call succeeds. Add them under Hosted Authentication > Identity Providers in the Dashboard.

  • Allowed Origins: http://localhost:3000
  • Callback URIs: http://localhost:3000

Install the library alongside your IdP’s SDK. @nylas/connect has zero runtime dependencies, so it adds exactly 1 package to your bundle plus whatever your identity provider brings.

For React, install @nylas/react instead and use the useNylasConnect hook, which wraps the same client.

The identityProviderToken callback is what ties the 2 systems together. @nylas/connect calls it during the token exchange and sends the JWT to Nylas as idp_claims, which is how the resulting grant gets linked to your user. Return a fresh token each time; most IdP SDKs handle the refresh for you.

Replace the 3 placeholder values:

  • <AUTH0_DOMAIN>: your Auth0 domain, such as your-app.us.auth0.com
  • <AUTH0_CLIENT_ID>: your Auth0 Client ID
  • <NYLAS_CLIENT_ID>: your Nylas application’s client ID from the Dashboard

connect() returns a ConnectResult. The email address lives on result.grantInfo?.email, not on the result itself.

This is the part that saves you a database column. Send your IdP’s access token as the bearer token and your user’s ID in the X-Nylas-External-User-Id header, then address the mailbox as /v3/grants/me. No grant_id appears anywhere in your code.

The limit parameter caps the response at 5 messages here; the Messages endpoint returns 50 by default and accepts up to 200. The same header works across the Email, Calendar, and Contacts endpoints.

Grants break when a user changes their password or revokes access, so check the status before you rely on a mailbox being reachable. getConnectionStatus() returns 1 of 4 values: connected, expired, invalid, or not_connected.

const status = await nylasConnect.getConnectionStatus();
const session = await nylasConnect.getSession();
if (session?.grantInfo) {
console.log("Connected as:", session.grantInfo.email);
console.log("Provider:", session.grantInfo.provider);
}

Treat expired and invalid as recoverable. Re-running connect() preserves the grant ID, object IDs, and sync state, as described in handling expired grants.