Skip to content

NylasConnectButton component

The NylasConnectButton is a prebuilt React component that triggers the Nylas Connect authentication flow. It supports both pop-up and redirect methods, includes sensible loading/disabled states, and offers styling options out of the box.

import { NylasConnectButton } from "@nylas/react/connect";
export default function ConnectCTA() {
return (
<NylasConnectButton
clientId={process.env.NYLAS_CLIENT_ID!}
redirectUri={process.env.NYLAS_REDIRECT_URI!}
/>
);
}

The component renders as <button type="button">. When a connection is in progress, the button is disabled and sets aria-busy for accessibility purposes.

We recommend using the NylasConnectButton component when you want a drag-and-drop authentication experience with minimal setup. If you need custom UI or UX, or more granular control over state, use the useNylasConnect hook instead.

Important: Conditional rendering and callbacks

Section titled “Important: Conditional rendering and callbacks”

The NylasConnectButton automatically handles OAuth callbacks when the component is mounted. However, if you render the button conditionally (for example, only when the user is not authenticated), the button won’t be present on the callback page to handle the OAuth response.

If you conditionally render the button, you must handle the callback yourself using the useNylasConnect hook.

The button is always rendered, so it can handle the callback automatically:

import { NylasConnectButton } from "@nylas/react/connect";
function App() {
return (
<NylasConnectButton
clientId={process.env.NYLAS_CLIENT_ID!}
redirectUri={process.env.NYLAS_REDIRECT_URI!}
onSuccess={(result) => {
console.log("Connected:", result.grantInfo?.email);
}}
/>
);
}

❌ Incorrect: Conditional rendering without manual callback handling

Section titled “❌ Incorrect: Conditional rendering without manual callback handling”

The button won’t be mounted on the callback page, so the OAuth response won’t be processed:

import { NylasConnectButton } from "@nylas/react/connect";
function App() {
const [isAuthenticated, setIsAuthenticated] = useState(false);
// ❌ Button won't be present after redirect, callback won't be handled
return (
<div>
{!isAuthenticated && (
<NylasConnectButton
clientId={process.env.NYLAS_CLIENT_ID!}
redirectUri={process.env.NYLAS_REDIRECT_URI!}
/>
)}
</div>
);
}

✅ Correct: Conditional rendering with manual callback handling

Section titled “✅ Correct: Conditional rendering with manual callback handling”

Use the hook to handle callbacks manually when rendering conditionally:

import { useNylasConnect } from "@nylas/react/connect";
function App() {
const { isConnected, grant, connect, isLoading } = useNylasConnect({
clientId: process.env.NYLAS_CLIENT_ID!,
redirectUri: process.env.NYLAS_REDIRECT_URI!,
autoHandleCallback: true, // Hook handles callback automatically
});
if (isLoading) {
return <div>Loading...</div>;
}
if (isConnected && grant) {
return (
<div>
<h2>Welcome, {grant.email}!</h2>
<p>Provider: {grant.provider}</p>
</div>
);
}
// Now it's safe to conditionally render the button
return (
<button
onClick={async () => {
try {
await connect({ method: "inline" });
} catch (error) {
console.error("Connection failed:", error);
}
}}
>
Connect your account
</button>
);
}
NameapiUrl
DescriptionThe Nylas API base URL.
Typehttps://api.us.nylas.com | https://api.eu.nylas.com
Defaulthttps://api.us.nylas.com
Namechildren
DescriptionCustom button content. Overrides text.
TypeReactNode?
NameclassName
DescriptionAdditional class names to add to the NylasConnectButton element.
Typestring?
NameclientId
DescriptionYour Nylas application’s client ID.
Typestring
NamecodeExchange
DescriptionA custom backend code exchange. This replaces the built-in token exchange.
TypeCodeExchangeMethod?
NamecssVars
DescriptionA set of custom CSS properties (for example, --nylas-btn-bg, --nylas-btn-fg, and so on).
TypeRecord<string, string>?
NamedefaultScopes
DescriptionDefault scopes to configure for the NylasConnect instance. These scopes are used when scopes is not provided. This is a configuration option that sets defaults for the client.
TypeNylasScope[]?
Namedisabled
DescriptionWhen true, disables the NylasConnectButton.
Typeboolean?
NameidentityProviderToken
DescriptionA callback that returns an external identity provider JWT. This is set to idp_claims during token exchange.
Type() => Promise<string | null> | string | null
NameloginHint
DescriptionAn email address to pre-fill on the provider authentication page.
Typestring?
Namemethod
DescriptionSpecify the authentication method. When inline, redirects the user to the provider using the URL returned from connect(). When popup, opens a pop-up window and resolves with ConnectResult on success.
Typepopup | inline
Defaultinline
NamepersistTokens
DescriptionWhen true, Nylas Connect stores tokens in localStorage. When false, tokens are stored in memory only.
Typeboolean?
Defaulttrue
NamepopupHeight
DescriptionThe height of the authentication pop-up, in pixels. Used when method is popup.
Typenumber?
Default600
NamepopupWidth
DescriptionThe width of the authentication pop-up, in pixels. Used when method is popup.
Typenumber?
Default500
Nameprovider
DescriptionThe OAuth provider that Nylas Connect will use to authenticate users.
TypeProvider?
NameredirectUri
DescriptionThe OAuth redirect URI that matches your application configuration.
Typestring?
Namescopes
DescriptionScopes to request for this specific authentication attempt. This is an option property that overrides defaultScopes when provided. Use this to request different scopes for individual connection attempts.
TypeNylasScope[]?
Namesize
DescriptionThe size of the NylasConnectButton when using one of the default styles.
Typesm | md | lg
Defaultmd
Namestyle
DescriptionInline styles for the NylasConnectButton.
TypeCSSProperties?
Nametext
DescriptionText to be displayed as the NylasConnectButton label. Ignored when children is specified.
Typestring?
Default”Connect your inbox”
Nameunstyled
DescriptionWhen true, disables the default NylasConnectButton style classes.
Typeboolean?
Defaultfalse
Namevariant
DescriptionThe visual variant for the default NylasConnectButton styles.
Typeprimary | outline
Defaultprimary
EventTypeDescription
onCancel(reason: string) => void?Called when the user cancels the authentication flow or closes the pop-up.
onError(error: Error) => void?Called when an error occurs during authentication.
onStart() => void?Called when the user clicks the button and the authentication flow begins.
onSuccess(result: ConnectResult) => void?Called when the pop-up authentication flow completes and the provider returns tokens for the user.