This page explains how to use the Nylas Kotlin/Java SDK and Contacts API to manage your users’ contacts. For more information, see the Contacts documentation.
Before you begin
Before you start, you must have done the following tasks:
List contacts
This section walks through how to return information about a 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.
ListContactsQueryParams queryParams = new ListContactsQueryParams.Builder().limit(10).build();ListResponse<Contact> contacts = nylas.contacts().list("<NYLAS_GRANT_ID>", queryParams);
val contacts = nylas.contacts().list(dotenv["NYLAS_GRANT_ID"])
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 id
s to modify contacts later.
for(Contact contact : contacts.getData()){ assert contact.getEmails() != null;
System.out.printf("Name: %s | Email: %s | Id: %s%n", contact.getGivenName(), contact.getEmails().get(0).getEmail(), contact.getId() );}
for(contact in contacts.data){ println("Name: ${contact.givenName} | " + "Email: ${contact.emails?.get(0)?.email} |" + "Id: ${contact.id}")}
For more information about the attributes a Contact object includes, see the Contacts references.
The examples below combine the previous steps to list the first 10 contacts in a user’s account, and their details.
import com.nylas.NylasClient;import com.nylas.models.*;
public class ReadAllContacts { public static void main(String[] args) throws NylasSdkTimeoutError, NylasApiError { NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build(); ListContactsQueryParams queryParams = new ListContactsQueryParams.Builder().limit(10).build(); ListResponse<Contact> contacts = nylas.contacts().list("<NYLAS_GRANT_ID>", queryParams);
for(Contact contact : contacts.getData()){ assert contact.getEmails() != null;
System.out.printf("Name: %s | Email: %s | Id: %s%n", contact.getGivenName(), contact.getEmails().get(0).getEmail(), contact.getId() ); } }}
import com.nylas.NylasClient
fun main(args: Array<String>) { val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>") val contacts = nylas.contacts().list("<NYLAS_GRANT_ID>")
for(contact in contacts.data){ println("Name: ${contact.givenName} | " + "Email: ${contact.emails?.get(0)?.email} |" + "Id: ${contact.id}") }}
Create a contact
The following examples show how to create a contact and assign its values using the Nylas Kotlin/Java SDK. After you create a contact and save it to Nylas, Nylas syncs it to the user’s provider.
import com.nylas.NylasClient;import com.nylas.models.*;import java.util.ArrayList;import java.util.List;
public class CreateAContact { public static void main(String[] args) throws NylasSdkTimeoutError, NylasApiError { NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build();
List<ContactEmail> contactEmails = new ArrayList<>();
List<WebPage> contactWebpages = new ArrayList<>(); contactWebpages.add(new WebPage("https://www.nylas.com", ContactType.WORK));
CreateContactRequest requestBody = new CreateContactRequest.Builder(). emails(contactEmails). companyName("Nylas"). givenName("Nylas' Swag"). notes("This is good swag"). webPages(contactWebpages). build();
Response<Contact> contact = nylas.contacts().create("<NYLAS_GRANT_ID>", requestBody);
System.out.println(contact); }}
import com.nylas.NylasClientimport com.nylas.models.ContactEmailimport com.nylas.models.ContactTypeimport com.nylas.models.CreateContactRequestimport com.nylas.models.WebPage
fun main(args: Array<String>) { val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>") val webpage : List<WebPage> = listOf(WebPage("https://www.nylas.com", ContactType.WORK))
val contactRequest = CreateContactRequest.Builder(). emails(emails). companyName("Nylas"). givenName("Nylas' Swag"). notes("This is good swag"). webPages(webpage). build()
val contact = nylas.contacts().create("<NYLAS_GRANT_ID>", contactRequest)
print(contact.data)}