> For the complete documentation index, see [llms.txt](https://developers-apps-in-toss.toss.im/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/she-bei/device.getcontacts.md).

# Device.getContacts

> 函数式 API `fetchContacts`也能用它实现相同功能。

### 功能说明

按页获取用户的联系人列表。 `size`·`offset`并按其进行分页， `query.contains`可以通过它按姓名包含内容进行搜索。 `offset`在首次调用时使用 `0`，之后使用响应的 `nextOffset`使用。

如果没有读取联系人权限 `FetchContactsPermissionError`会发生。 `Device.getContacts.getPermission()` / `Device.getContacts.openPermissionDialog()`可以通过它检查·请求权限。

### 类型

```ts
Device.getContacts(options: FetchContactsOptions): Promise<ContactResult>;
```

**参数**

```ts
type FetchContactsOptions = {
  size: number;
  offset: number;
  query?: {
    contains?: string;
  };
};
```

**响应**

```ts
type ContactEntity = {
  name: string;
  phoneNumber: string;
};

type ContactResult = {
  result: ContactEntity[];
  nextOffset: number | null;
  done: boolean;
};
```

### 错误

错误会以类的形式抛出 — `error instanceof FetchContactsPermissionError`通过它识别。该类可在 `@apps-in-toss/web-framework`中导入。

| 错误类                            | 说明                                                               |
| ------------------------------ | ---------------------------------------------------------------- |
| `FetchContactsPermissionError` | 当读取联系人权限被拒绝时。 `Device.getContacts.openPermissionDialog()`可以重新请求。 |

### 示例代码

```js
import {
  Device,
  FetchContactsPermissionError,
} from "@apps-in-toss/web-framework";

let nextOffset = 0;
let done = false;

async function loadNextContacts() {
  if (done) {
    return;
  }

  try {
    const response = await Device.getContacts({
      size: 10,
      offset: nextOffset ?? 0,
      query: { contains: "김" },
    });

    for (const contact of response.result) {
      console.log(`${contact.name}: ${contact.phoneNumber}`);
    }

    nextOffset = response.nextOffset;
    done = response.done;
  } catch (error) {
    if (error instanceof FetchContactsPermissionError) {
      console.warn("没有读取联系人权限。");
      return;
    }
    console.error(error);
  }
}

document
  .getElementById("load-contacts")
  .addEventListener("click", loadNextContacts);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers-apps-in-toss.toss.im/documentation/api-and-sdk-zh/sdk/domains-api/she-bei/device.getcontacts.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
