Version:
Only show these results:

Manage contacts with Ruby

This page explains how to use the Nylas Ruby SDK and Contacts API to create and manage contacts.

Before you begin

Before you start, you must have done the following tasks:

List contacts

This section walks through how to return information about an end user's contacts, including their first and last names, email addresses, and IDs.

The following code snippets set a limit on the number of contacts Nylas returns: a maximum of 10.

query_params = { limit: 10 }

contacts = nylas.contacts.list(identifier: "<NYLAS_GRANT_ID>",
query_params: query_params)

Next, iterate through the list of Contact objects to get their first and last names, their primary email address, and their id. You can use the ids to modify contacts later.

contacts.each {|contact|
puts "Name: #{contact[:given_name]} #{contact[:surname]} | " \
"Email: #{contact[:emails][0][:email]} | ID: #{contact[:id]}"
}

For more information about the attributes a Contact object includes, see the Contacts reference documentation.

The examples below combine the previous steps to list the first 10 contacts in an end user's account, and their details.

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

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

contacts, _ = nylas.contacts.list(identifier: "<NYLAS_GRANT_ID>")
contacts.each {|contact|
puts "Name: #{contact[:given_name]} #{contact[:surname]} | " \
"Email: #{contact[:emails][0][:email]} | ID: #{contact[:id]}"
}

Create a contact

When you create a contact, Nylas syncs it to the end user's provider. The following example creates a contact, assigns its attributes, and saves it to Nylas.

require 'nylas' 

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

request_body = {
given_name: "My",
middle_name: "Nylas",
surname: "Friend",
emails: [{email: "nylas-friend@example.com", type: "work"}],
notes: "Make sure to keep in touch!",
phone_numbers: [{number: "555 555-5555", type: "business"}],
web_pages: [{url: "https://www.nylas.com", type: "homepage"}]
}

contact, _ = nylas.contacts.create(identifier: "<NYLAS_GRANT_ID>",
request_body: request_body)

puts contact

For more information on Contact objects and their attributes, see the Contacts reference documentation.

When you create a contact for a Google account, it renders like this on the provider:

A Google Contact card displaying the information for a contact named My Nylas Friend.

For Microsoft Outlook, new contacts look like this:

A Microsoft Contact card displaying the information for a contact named My Nylas Friend.

Delete a contact

To delete a contact from an end user's account, you must get the contact's id and pass it as an argument to the contacts.destroy() function. You can use the same code from the List contacts section to retrieve a contact's ID.

⚠️ The following code snippet deletes the contact you specify! Make sure the contact is one that you actually want to delete.

The code sample below finds and deletes a specific contact from an end user's account.

require 'nylas' 

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

status, _ = nylas.contacts.destroy(identifier: "<NYLAS_GRANT_ID>",
contact_id: "<CONTACT_ID>")

puts status

Explore the Contacts API

If you've made it to this point, congratulations! You've learned how to manage contacts with the Nylas Ruby SDK and Contacts API! 🎉

There's plenty more that you can do with Nylas. Take a look at the following resources to learn more: