Manage contacts with the Nylas Node.js SDK
The Nylas Node 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.
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 how to install the Nylas Node.js SDK and set up your environment. Before you start this tutorial, make sure that you do the following:
- Complete the Getting started with the Nylas Node.js SDK tutorial.
- Authenticate users with the Nylas Node.js SDK.
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.
-
Call the
config
function and pass your client ID (<CLIENT_ID>
) and client secret (<CLIENT_SECRET>
). -
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: Read contacts
Use the nylas.contacts
object to read contacts from an account's contact list.
- Call the
.list()
function to return all contacts. - Optionally, specify attributes for the contact object.
For information on available attributes, refer to the 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 3: Create and save contacts
Create a new contact and assign contact attributes. For information on available attributes, refer to 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
, andsurname
define the contact's first, middle, and last name.emailAddresses
adds an email address to the contact and setstype
towork
. Supportedtype
values arework,
,personal
, andother
.phoneNumbers
adds a phone number to the contact and setstype
asbusiness
. Google and Microsoft label the phone number with thebusiness
type as the contact’s work phone number. Supportedtype
values arebusiness
,home
,mobile
,page
,business_fax
,home_fax
,organization_main
,assistant
,radio
andother
.webPages
connects a website URL to the contact and setstype
tohomepage
. Supportedtype
values arehomepage
,profile
,work
, andblog
.
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:
You can create the same contact in Microsoft Outlook:
Step 4: 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. For information on how to retrieve contact IDs, refer to Read contacts.
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:
- Follow our Node.js SDK tutorials.
- Learn more about the Nylas API.
- Learn more about the Nylas Contacts API.
- Learn more about how the Nylas Communications Platform works.
- Read the blog post How to Manage Contacts with Nylas Node SDK.