Skip to content

Creating grants with OAuth and an API key

Nylas supports Hosted OAuth to get the user’s authorization for scopes and create their grant. You can then use their grant ID and your application-specific API key to access their data and make other requests. This allows you to use the same request method for everything in your project, including endpoints that don’t specify a grant (for example, the webhook endpoints).

How Hosted OAuth works

Nylas creates only one grant per email address in each application. If a user authenticates with your Nylas application using the email address associated with an existing grant, Nylas re-authenticates the grant instead of creating a new one.

  1. The user clicks a link or button in your project to start an authorization request.
  2. Nylas forwards the user to their provider where they complete the authorization flow.
  3. The provider directs the user to the Nylas callback URI and includes URL parameters that indicate whether the authorization succeeded or failed, along with other information.
  4. If the authorization succeeded, Nylas creates an unverified grant record.
  5. Nylas forwards the user to your project’s callback URI and includes the access code from the provider as a URL parameter.
  6. Your project uses the code to perform a token exchange with the provider.
  7. When the token exchange completes successfully, Nylas marks the grant record as verified and creates a grant ID for the user.

Start an authorization request

The first step of the authentication process is to start an authorization request. Usually this is a button or link that the user clicks.

Your project redirects the user to the authorization request endpoint and includes their information as a set of query parameters, as in the example below. When the user goes to this URL, Nylas starts a secure authentication session and redirects them to their provider’s website.

Each provider displays their authorization consent and approval steps differently. The steps are visible only to the user.

If a user authenticates using their Google account, they might be directed to Google’s authorization page twice. This is a normal part of the Hosted OAuth flow, and it ensures that the user approves all necessary scopes.

Pass user information in state parameter

Nylas Hosted OAuth supports the optional state parameter. If you include it in an authorization request, Nylas returns the unmodified value to your project. You can use this as a verification check, or to track information about the user that you need when creating a grant or logging them in.

For more information about the state parameter, see the OAuth 2.0 specification and the official OAuth 2.0 documentation.

Accept authorization response

After the user completes the authorization process, their provider sends them to Nylas’ redirect URI (https://api.us.nylas.com/v3/connect/callback) and includes URL parameters with information about the user. Nylas uses the information in the parameters to find your application using its client ID and, if the authentication succeeded, create an unverified grant record for the user.

Nylas then uses your application’s callback URI to direct the user back to your project, along with the code it received from the provider.

https://myapp.com/callback-handler?code=<CODE>

If you specified a state in the initial authorization request, Nylas includes it as a URL parameter.

Exchange code for access token

The OAuth code is a unique, one-time-use credential. This means that if your POST /v3/connect/token request fails, you’ll need to restart the OAuth flow to generate a new code. If you try to pass the original code in another token exchange request, Nylas returns an error.

Make a POST /v3/connect/token request to exchange the user’s code for an access token. Nylas returns an access token and other information about the user.

Nylas marks the user’s grant as verified and sends you their grant ID and email address.

You don’t need to record the user’s OAuth access token or any other OAuth information. Nylas stores what it needs in the user’s grant record.

Make requests with API key

Now that you have a grant ID for your user, you can make requests on their behalf with your application’s API key and their grant ID.