<br />

After you've completed the steps in [Get Ready to Use the People API](https://developers.google.com/people/v1/getting-started), you are ready to read, copy, and search "Other contacts" data.

The following code samples demonstrate how to send a few simple requests. For a full list of methods, see the [reference documentation](https://developers.google.com/people/api/rest).

## List the user's "Other contacts"

To
[get a list of people in the user's "Other contacts"](https://developers.google.com/people/api/rest/v1/otherContacts/list),
use the following code:

### Protocol

```
GET /v1/otherContacts?readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com
```

### Java

```java
ListOtherContactsResponse response = peopleService.otherContacts().list()
    .setReadMask("metadata,names,emailAddresses")
    .execute();

List<Person> otherContacts = response.getOtherContacts();
```

## List the user's "Other contacts" that have changed

### Java

```java
// Initial request
ListOtherContactsResponse fullSyncResponse = peopleService.otherContacts().list()
    .setReadMask("metadata,names,emailAddresses")
    .setRequestSyncToken(true)
    .execute();
// Fetch all the pages
while (fullSyncResponse.getNextPageToken() != null) {
  fullSyncResponse = peopleService.otherContacts().list()
      .setReadMask("metadata,names,emailAddresses")
      .setRequestSyncToken(true)
      .setPageToken(fullSyncResponse.getNextPageToken())
      .execute();
}

// Some time passes

// Fetch incremental changes using the sync token returned in the last fullSyncResponse.
try {
  ListOtherContactsResponse incrementalSyncResponse = peopleService.otherContacts().list()
      .setReadMask("metadata,names,emailAddresses")
      .setSyncToken(fullSyncResponse.getNextSyncToken())
      .execute();
  for (Person person : incrementalSyncResponse.getOtherContacts()) {
    handlePerson(person);
  }
  
  // Fetch all the pages
  while (!incrementalSyncResponse.getNextPageToken().isEmpty()) {
    incrementalSyncResponse = peopleService.otherContacts().list()
        .setReadMask("metadata,names,emailAddresses")
        .setSyncToken(fullSyncResponse.getNextSyncToken())
        .setPageToken(incrementalSyncResponse.getNextPageToken())
        .execute();
    for (Person person : incrementalSyncResponse.getOtherContacts()) {
      handlePerson(person);
    }
  }
} catch (GoogleJsonResponseException e) {
  if (e.getStatusCode() == 410) {
    // Sync token expired. Make full sync request.
  }
}

void handlePerson(Person person) {
  if (person.getMetadata().getDeleted()) {
    // Handle deleted person
  } else {
    // Handle changed person
  }
}
```

> [!NOTE]
> **Note:** Writes may have a propagation delay of several minutes for sync requests. Incremental syncs are not intended for read-after-write use cases.

More details about sync behavior at [ListOtherContacts](https://developers.google.com/people/api/rest/v1/otherContacts/list).

## Copy an "Other contact" to "myContacts" group

To
[copy an "Other contact" to "myContacts" group](https://developers.google.com/people/api/rest/v1/otherContacts/copyOtherContactToMyContactsGroup),
use the following code:

### Protocol

```
POST /v1/resource_name:copyOtherContactToMyContactsGroup?copyMask=names,emailAddresses,phoneNumbers HTTP/1.1
Host: people.googleapis.com
```

### Java

```java
Person copiedContact = peopleService
    .otherContacts()
    .copyOtherContactToMyContactsGroup(
        "resource_name",
        new CopyOtherContactToMyContactsGroupRequest()
            .setCopyMask("names,emailAddresses,phoneNumbers"))
    .execute();
```

## Search the user's "Other contacts"

To
[search all of the user's "Other contacts"](https://developers.google.com/people/api/rest/v1/otherContacts/search),
use the following code:

### Protocol

```
// Warmup cache
GET /v1/otherContacts:search?query=&readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com

// Send search request after several seconds
GET /v1/otherContacts:search?query=query&readMask=names,emailAddresses HTTP/1.1
Host: people.googleapis.com
```

### Java

```java
// Warmup cache
SearchResponse response = peopleService.otherContacts().search()
    .setQuery("")
    .setReadMask("names,emailAddresses")
    .execute();

// Wait a few seconds
Thread.sleep(5);

// Send search request
SearchResponse response = peopleService.otherContacts().search()
    .setQuery("query")
    .setReadMask("names,emailAddresses")
    .execute();
```

> [!IMPORTANT]
> **Important:** Search uses a lazy cache that is updated after a request. Clients should first send a warmup search request with an empty query to make sure the cache has the latest data.

> [!NOTE]
> **Note:** Search does a prefix match of the query with the fields on a person. For example, a person with name "foo name" matches queries such as "f", "fo", "foo", "foo n", "nam", etc., but not "oo n".