Version:
Only show these results:

Using the Ruby SDK

The Nylas Ruby SDK is an open-source software development kit that enables you to use Ruby to integrate the Nylas APIs with your application.

⚠️ The v6.x Nylas Ruby SDK is only compatible with v3.x Nylas APIs. If you're still using an earlier version of Nylas, you should keep using the v5.x Ruby SDK until you can upgrade to Nylas v3.

Before you begin

Before you can start using the Nylas Ruby SDK, make sure you have done the following:

Install the Nylas Ruby SDK

To install the Nylas Ruby SDK, follow these steps:

  1. Add a reference to the Nylas gem in your application's Gemfile:

    gem 'nylas'   
  2. Open your terminal and run the bundle command.

  3. Run gem install nylas to install the Nylas gem.

MacOS 10.11 (El Capitan)

The Nylas gem requires you to have OpenSSL installed. However, Apple stopped bundling OpenSSL with its native Ruby version as of MacOS 10.11. If you're using MacOS El Capitan and have trouble installing the gem, you can run the following commands in your terminal to install OpenSSL.

sudo brew install openssl
sudo brew link openssl --force
gem install nylas

Initialize the Client object

All of Nylas' functionality is available through the Client object. Before you make requests, initialize the Client object with the API key.

#!/usr/bin/env ruby
require 'nylas'

nylas = Nylas::Client.new(
api_key: "<NYLAS_API_KEY>"
)

(Optional) Change the base API URL

You can choose to change the base API URL depending on your location, as in the table below.

Location Nylas API URL
United States (Oregon) https://api.us.nylas.com
Europe (Ireland) http://api.eu.nylas.com

For more information, see the data residency documentation and the Migration guide for data centers.

To change the API URL, pass the NYLAS_API_URI parameter with the API URL of your location.

#!/usr/bin/env ruby
require 'nylas'

nylas = Nylas::Client.new(
api_key: "<NYLAS_API_KEY>",
api_uri: "<NYLAS_API_URI>"
)

🔍 The base API URL defaults to the Nylas U.S. region. See the data residency documentation for more information.

Test Ruby SDK installation

Now that you have the Ruby SDK installed and set up, you can make a simple program to test your installation. For example, the following code makes a request to return information about your Nylas application.

# frozen_string_literal: true

# Load gems
require 'nylas'

# Initialize Nylas client
nylas = Nylas::Client.new(
api_key: '<NYLAS_API_KEY>'
)

application = nylas.applications.get_details()
puts application

Authenticate end users

The Nylas APIs use OAuth 2.0 and let you choose to authenticate using either an API key or access token. For more information about authenticating with Nylas, see the Authentication guide.

In practice, Nylas' REST API simplifies the OAuth process to two steps: redirecting the end user to Nylas, and handling the auth response.

Redirect end user to Nylas

The following code sample redirects an end user to Nylas for authentication.

# frozen_string_literal: true

require 'nylas'
require 'sinatra'

set :show_exceptions, :after_handler
enable :sessions

error 404 do
'No authorization code returned from Nylas'
end

error 500 do
'Failed to exchange authorization code for token'
end

nylas = Nylas::Client.new(
api_key: '<NYLAS_API_KEY>',
api_uri: '<NYLAS_API_URI>'
)

get '/nylas/auth' do
config = {
client_id: '<NYLAS_CLIENT_ID>',
provider: 'google',
redirect_uri: 'http://localhost:4567/oauth/exchange',
login_hint: 'swag@nylas.com',
access_type: 'offline'
}

url = nylas.auth.url_for_oauth2(config)
redirect url
end

🔍 Nylas provides granular auth scopes that allow you to control the level of access your application has to end users' data. For a list of scopes that Nylas supports, see the Authentication scopes documentation.

Handle the authentication response

Next, your application has to handle the authentication response from Nylas, as in the example below.

get '/oauth/exchange' do
code = params[:code]
status 404 if code.nil?

begin
response = nylas.auth.exchange_code_for_token({
client_id: '<NYLAS_CLIENT_ID>',
redirect_uri: 'http://localhost:4567/oauth/exchange',
code: code
})
rescue StandardError
status 500
else
response[:grant_id]
response[:email]
session[:grant_id] = response[:grant_id]
end
end

Latest supported version

For the latest supported version of the SDK, see the Releases page on GitHub.

Method reference

The Nylas Ruby SDK includes method documentation, so you can easily find the implementation details that you need.

GitHub repositories

The Nylas Ruby SDK repository houses the Ruby SDK. You can contribute to the SDK by creating an issue or opening a pull request.

For Nylas code samples, visit the Nylas Samples repository.

Tutorials