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 with JavaScript and the Nylas Contacts API. The Nylas Contacts API makes it very easy to create and modify contacts for your users' Google and Microsoft accounts.

You'll use the Nylas Node.js SDK and Contacts API to manage Google and Microsoft contacts. A contact can include basic information, such as names and email addresses, and extended information, such as notes and websites.

What you'll learn

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

  • Read contacts from an account's contact list.
  • Create and save Google and Microsoft contacts.
  • Delete contacts.

Prerequisites

Prerequisites include installing the Nylas Node.js SDK and setting up your environment. Before you start this tutorial, make sure that you do the following:

Step 1: Initialize the Nylas object

At its core, the Nylas Communication Platform is an API client that interfaces with all major contact providers.

The Nylas object provides access to every resource in Nylas APIs. Before making any request, you must initialize the Nylas object with your client ID, client secret, and access token.

  1. Call the config function and pass your client ID (<CLIENT_ID>) and client secret (<CLIENT_SECRET>).

  2. Call the with function and pass your access token (<ACCESS_TOKEN>).

const Nylas = require('nylas');

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

const nylas = Nylas.with(<ACCESS_TOKEN>);

Step 2: Change the base API URL

You can optionally change the base API URL depending on your location:

Location API URL Scheduler API URL
United States (Oregon) https://api.nylas.com https://api.schedule.nylas.com
Europe (Ireland) https://ireland.api.nylas.com https://ireland.api.schedule.nylas.com

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

To change the base URL to the EU (Ireland) region, set apiServer: regionConfig[Region.Ireland].nylasAPIUrl. The base API URL defaults to the US region.

const Nylas = require('nylas');

Nylas.config({
clientId: <CLIENT_ID>,
clientSecret: <CLIENT_SECRET>,
apiServer: regionConfig[Region.Ireland].nylasAPIUrl
});

const nylas = Nylas.with(<ACCESS_TOKEN>);

Step 3: Read contacts

Use the nylas.contacts object to read contacts from an account's contact list.

  1. Call the .list() function to return all contacts.
  2. Optionally, specify attributes for the contact object.
    For information on available attributes, see GET /contacts.

Example: Read contacts

The following example returns the first ten contacts and logs the first name (givenName), last name (surname), first email address (emailAddresses), and ID (id) for each contact:

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 4: Create and save contacts

Create a new contact and assign contact attributes. For information on available attributes, see POST /contacts.

Create contacts

The following example creates a new Google contact object and assigns 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'}]
});
  • givenName middleName, and surname define the contact's first, middle, and last name.
  • note adds a note to the contact.
  • emailAddresses adds an email address to the contact and sets type to work. Supported type values are work,, personal, and other.
  • phoneNumbers adds a phone number to the contact and sets type as business. Google and Microsoft label the phone number with the business type as the contact’s work phone number. Supported type values are business, home, mobile, page, business_fax, home_fax,organization_main, assistant, radio and other.
  • webPages connects a website URL to the contact and sets type to homepage. Supported type values are homepage, profile, work, and blog.

Save contacts

Use the .save() function to save the contact to Nylas. Nylas syncs the new contact to Google.

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

Example: Create and save contacts

The following example 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);
});

The following is the example contact created in Google:

Google contact example

You can create the same contact in Microsoft Outlook:

Microsoft contact example

Step 5: Delete contacts

To delete a contact from Google, get the id of the contact and pass it as an argument to the contacts.delete() function. You can use the same endpoint you used earlier to Read contacts to retrieve the contact ID.

Example: Delete contacts

The following example deletes a specific contact from Google. Make sure that you want to do this before proceeding.

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)

More resources

If you’ve made it this far, congrats! You’ve successfully learned how to manage Google and Microsoft contacts with the Nylas Contacts API! There's plenty more that you can do with Nylas. Take a look at the following resources: