Skip to content
Skip to main content

Creating grants with Hosted Authentication 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).

  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.

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.

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.

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.

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.

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.