# Manage contacts with Kotlin/Java

Source: https://developer.nylas.com/docs/v3/sdks/kotlin-java/manage-contacts/

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](/docs/v3/email/contacts/).

## Before you begin

Before you start, you must have done the following tasks:

- [Installed and set up the Nylas Kotlin/Java SDK](/docs/v3/sdks/kotlin-java/).
- [Authenticated one or more users](/docs/v3/sdks/kotlin-java/#authenticate-users).

## 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.

```java
ListContactsQueryParams queryParams = new ListContactsQueryParams.Builder().limit(10).build();
ListResponse<Contact> contacts = nylas.contacts().list("<NYLAS_GRANT_ID>", queryParams);
```

```kt
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.

```java
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()
  );
}
```

```kt
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](/docs/reference/api/contacts/).

The examples below combine the previous steps to list the first 10 contacts in a user's account, and their details.

```java
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()
          );
    }
  }
}
```

```kt
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.

```java
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);
  }
}
```

```kt
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)
}
```