Version:
Only show these results:

Manage contacts with Kotlin/Java

This page explains how to use the Nylas Kotlin/Java SDK and Contacts API to manage your end 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 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.

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 ids 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 reference documentation.

The examples below combine the previous steps to list the first 10 contacts in an end 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 end 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<>();
contactEmails.add(new ContactEmail("swag@nylas.com", ContactType.WORK));

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.NylasClient
import com.nylas.models.ContactEmail
import com.nylas.models.ContactType
import com.nylas.models.CreateContactRequest
import com.nylas.models.WebPage

fun main(args: Array<String>) {
val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>")
val emails : List<ContactEmail> = listOf(ContactEmail("swag@nylas.com", ContactType.WORK))
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)
}

Explore the Contacts API

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

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