# Manage contacts with Ruby

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

This page explains how to use the Nylas Ruby SDK and Contacts API to create and manage contacts.

## Before you begin

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

- [Installed and set up the Nylas Ruby SDK](/docs/v3/sdks/ruby/).
- Authenticated one or more 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.

```ruby
query_params = { limit: 10 }

contacts = nylas.contacts.list(identifier: "<NYLAS_GRANT_ID>",
    query_params: query_params)
```

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.

```ruby
contacts.each {|contact|
  puts "Name: #{contact[:given_name]} #{contact[:surname]} | " \
      "Email: #{contact[:emails][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.

```ruby
#!/usr/bin/env ruby
require 'nylas'

nylas = Nylas::Client.new(
    api_key: "<NYLAS_API_KEY>"
)

contacts, _ = nylas.contacts.list(identifier: "<NYLAS_GRANT_ID>")
contacts.each {|contact|
  puts "Name: #{contact[:given_name]} #{contact[:surname]} | " \
      "Email: #{contact[:emails][0][:email]} | ID: #{contact[:id]}"
}
```

## Create a contact

When you create a contact, Nylas syncs it to the user's provider. The following example creates a contact, assigns its attributes, and saves it to Nylas.

```ruby
require 'nylas'

nylas = Nylas::Client.new(
    api_key: "<NYLAS_API_KEY>"
)

request_body = {
  given_name: "My",
  middle_name: "Nylas",
  surname: "Friend",
  emails: [{email: "nylas-friend@example.com", type: "work"}],
  notes: "Make sure to keep in touch!",
  phone_numbers: [{number: "555 555-5555", type: "business"}],
  web_pages: [{url: "https://www.nylas.com", type: "homepage"}]
}

contact, _ = nylas.contacts.create(identifier: "<NYLAS_GRANT_ID>",
    request_body: request_body)

puts contact
```

For more information on Contact objects and their attributes, see the [Contacts references](/docs/reference/api/contacts/).

When you create a contact for a Google account, it renders like this on the provider:

<img
  src="/_images/google/contact-example.png"
  alt="A Google Contact card displaying the information for a contact named My Nylas Friend."
  style="height:300px;"
/>

For Microsoft Outlook, new contacts look like this:

<img
  src="/_images/microsoft/contact-example.png"
  alt="A Microsoft Contact card displaying the information for a contact named My Nylas Friend."
  style="height:300px;"
/>

## Delete a contact

To delete a contact from a user's account, you must get the contact's `id` and pass it as an argument to the `contacts.destroy()` function. You can use the same code from the [List contacts section](#list-contacts) to retrieve a contact's ID.

> **Warn:** 
> **The following code snippet deletes the contact you specify!** Make sure the contact is one that you actually want to delete.

The code sample below finds and deletes a specific contact from a user's account.

```ruby
require 'nylas'

nylas = Nylas::Client.new(
    api_key: "<NYLAS_API_KEY>"
)

status, _ = nylas.contacts.destroy(identifier: "<NYLAS_GRANT_ID>",
    contact_id: "<CONTACT_ID>")

puts status
```