Only show these results:

Manage contacts with the Nylas Node.js SDK

The Nylas Node.js SDK is the quickest way to integrate Google and Microsoft (Exchange, Outlook, Office365) contacts into your app using JavaScript and the Nylas Contacts API. The Contacts API makes it simple to create and modify contacts for your end users' Google and Microsoft accounts.

ℹ️ This tutorial applies to the Nylas Node.js SDK v6 and the Nylas API v2.7. The Contacts API is currently not available in the API v3 beta, and the Node.js SDK v7 beta is not compatible with the API v2.7.

This page discusses how to manage Google and Microsoft contacts with the Nylas Node.js SDK and Contacts API. A Contact object can include basic information (for example, names and email addresses) and extended information (for example, notes and websites).

What you'll learn

In this tutorial, you'll learn how to do the following tasks:

Before you begin

Before you start, install the Nylas Node.js SDK v6 and set up your environment:

Step 1: Read contacts

You can use the nylas.contacts object to read contacts from an end user's account:

  1. Call the .list() function to return all contacts.
  2. (Optional) Specify attributes for the Contact object (see the GET /contacts documentation for more information on available attributes).

The example below reads the first and last names, email addresses, and IDs of the first ten contacts in the account's contact list.

const Nylas = require('nylas');

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});

const nylas = Nylas.with(ACCESS_TOKEN);

nylas.contacts.list({limit: 10}).then(contacts => {
for (const contact of contacts) {
console.log(`Name: ${contact.givenName} ${contact.surname} | Email: ${contact.emailAddresses[0]['email']} | ID: ${contact.id}`);
}
});

Step 2: Create and save a contact

After you read contacts from a user's account, you can create and save a contact. The following example creates a new Google Contact object and assigns its attributes.

const { default: Contact } = require('nylas/lib/models/contact');

const contact = new Contact(nylas, {
givenName: "My",
middleName: "Nylas",
surname: "Friend",
notes: "Make sure to keep in touch!",
emailAddresses: [{type: 'work', email: '[email protected]'}],
phoneNumbers: [{type: 'business', number: '(555) 555-5555'}],
webPages: [{type: 'homepage', url: 'nylas.com'}]
});
  • The givenName, middleName, and surname parameters define the contact's first, middle, and last name, respectively.
  • The notes parameter adds a note to the contact.
  • emailAddresses adds an email address to the contact and sets its type (work, personal, or other).
  • phoneNumbers adds a phone number to the contact and sets its type (business, home, mobile, page, business_fax, home_fax, organization_main, assistant, radio, or other). Note that Google and Microsoft label any business-type phone numbers as the contact’s work phone number.
  • The webPages parameter associates a website URL with the contact and sets its type (homepage, profile, work, or blog).

For more information on available contact attributes, see the POST /contacts documentation.

Next, use the .save() function to save the contact to Nylas, as in the code snippet below. Nylas then syncs the contact to the user's service provider.

contact.save().then(contact => {
console.log(contact);
});

Example: Create and save a contact

The example below creates and saves a contact called "My Nylas Friend".

const Nylas = require('nylas');
const { default: Contact } = require('nylas/lib/models/contact');

Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const nylas = Nylas.with(ACCESS_TOKEN);

const contact = new Contact(nylas, {
// Define the first, middle, and last name.
givenName: "My",
middleName: "Nylas",
surname: "Friend",
notes: "Make sure to keep in touch!",

// Add the contact's email address.
emailAddresses: [{type: 'work', email: '[email protected]'}],

// Add the contact's phone number.
phoneNumbers: [{type: 'business', number: '(555) 555-5555'}],

// Add the contact's webpage.
webPages: [{type: 'homepage', url: 'nylas.com'}]
});

contact.save().then(contact => {
console.log(contact);
});

When you create a contact for a Google account, it looks like this:

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

For Microsoft Outlook, the new contact looks like this:

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

Step 3: 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.delete() function. You can use the same endpoint that you used in Step 1 to retrieve the contact ID.

Example: Delete a contact

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

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

const Nylas = require('nylas');
Nylas.config({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
});
const nylas = Nylas.with(ACCESS_TOKEN);

contact = nylas.contacts.find(CONTACT_ID)
contact = nylas.contacts.delete(contact.id)

What's next?

If you’ve made it this far, congrats! You’ve learned how to manage contacts with the Nylas Contacts API! 🎉

There's plenty more that you can do with Nylas. Take a look at these resources: