Skip to content

Create grants with OAuth and an API key

Nylas supports the familiar Hosted OAuth flow to get the user’s authorization for scopes and create their grant ID. Then, you use 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 application, including endpoints that don’t specify a grant (for example, webhook notifications and some group calendar features).

Before you begin

Before you begin, make sure you set up all the required parts for your authentication system:

  1. If you haven’t already, log in to the Nylas Dashboard and create an application.
  2. Generate an API key for your application.
  3. Create a provider auth app in the provider’s console or application. See the detailed instructions for creating a Google or Azure provider auth app.
  4. Create a connector for the provider you want to authenticate with.
  5. Add your project’s callback URIs (“redirect URIs”) in the Nylas Dashboard.

Creating grants with OAuth and API key

The rough OAuth process is…

  1. The user clicks a link or button in your app to start an OAuth request.
  2. Nylas forwards the user to their auth provider where they complete the OAuth flow, either accepting or declining the requested scopes.
  3. The auth provider directs the user back to the Nylas callback URI, and includes URL parameters that indicate success or failure, and other information.
  4. If auth was successful, Nylas creates an unverified grant record, then forwards the user to your application’s callback_uri and includes the access code from the provider in the URL as a parameter.
  5. Your application uses the code to perform a token exchange with the provider.
  6. When the token exchange completes successfully, Nylas marks the grant record as verified and sends the grant_id to you.

At this point, instead of using doing a token exchange to get an access token for the specific user, you can use an API key you created to access the Nylas APIs. Your app can use that grant_id to specify which data to access on behalf of 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 (GET api.us.nylas.com/v3/connect/auth) and includes their information as query parameters, as in the example below. When the user goes to this URL, Nylas starts a secure authentication session and redirects them to the authentication provider website.

/v3/connect/auth?
client_id=<NYLAS_CLIENT_ID>
&redirect_uri=https://myapp.com/callback-handler // Your application's callback_uri.
&response_type=code
&access_type=online
&provider=google
&state=sQ6vFQN

This cURL request uses access_type=online to specify not to send a refresh token. This is technically optional, but we’ve included it in the example because we’re using the API key method and don’t want a refresh token.

There are several other optional items you can choose to pass:

  • provider indicates which connector your Nylas application should use. If you only have one connector you can omit this parameter. If you have more than one connector, you can either specify which one to use, or omit this and Nylas gives the user a list of connectors to choose from when they start the auth process.
  • scope is optional if you set default scopes on the connector. Scopes set from this request override the default connector scopes.

You can also start an authorization request using the Nylas SDKs, as in the examples below.

Authorization response and grant creation

Each provider displays the consent and approval steps differently, and it’s visible only to the user. In all cases the user authenticates, and either accepts or declines the scopes your project requested.

Next, the auth provider sends the user to the Nylas redirect_uri (https://api.us.nylas.com/v3/connect/callback). Nylas uses the information in the response to find your Nylas application by client_id and, if the auth was successful, create an unverified grant record for the user and record their details.

Get the user’s code

Nylas uses your Nylas application’s callback_uri (for example, app.example.com/callback-handler) to forward the user back to your project. Nylas includes the code it got from the auth provider as a query parameter, as in the example below.

https://myapp.com/callback-handler?
code=R2E_t1Mf88lipCSqrKvWZhKKzpMVXb0UOuct-U5LidwJud5vMjH1I4q...
&state=... // If you included a state in the initial auth request, it's returned here.

Token exchange and grant verified

Make a POST /v3/connect/token request to exchange the code for an access token. The authentication provider responds with an access token and other information.

When this is successfully completed, Nylas marks the user’s grant as verified and sends you their grant_id and email address.

Record the grant ID and use your API key

As long as authentication succeeded, you can stop the OAuth process here, record the grant_id created for the user, and continue using your API key to make requests on behalf of this grant, as in the following examples.

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.

Using the state parameter to pass information about the user

Nylas Hosted OAuth includes the standard state parameter. This is an optional parameter in Nylas, and if you include it in an authorization request, Nylas returns the value unmodified back to the application. You can use this as a verification check, and to track information about the user that you need when creating a grant or logging them in.

Learn more about the state parameter in the OAuth 2.0 specification or in the official OAuth 2.0 documentation.