> 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/yong-hu/user.getconsenteddata.md).

# User.getConsentedData

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

### 功能说明

请求基于用户同意的数据。如果需要同意，会打开条款网页视图，同意完成后返回从服务器查询到的数据。

Toss应用 **5.264.0** 可在以上版本中使用。调用前 `User.getConsentedData.isSupported()`可通过其确认是否支持。函数式 API `getConsentedUserData`在不支持的版本中 `undefined`会返回，并且 `User.getConsentedData`是 `UNSUPPORTED_APP_VERSION` 会抛出错误。

### 类型

```ts
User.getConsentedData(options: GetConsentedUserDataOptions): Promise<ConsentedUserData>;
```

**参数**

```ts
interface GetConsentedUserDataOptions {
  /** 服务器在查找用户数据提供同意文和数据组合时使用的 key。例：`cud_delivery` */
  consentedUserDataKey: string;
  /** 即使处于 `USER_DECLINED` 状态，是否还要再次打开条款网页视图。默认值为 `false`。 */
  shouldRequestAgreementWhenUserDeclined?: boolean;
}
```

**响应**

```ts
type ConsentedUserDataKey =
  | "USER_NAME"
  | "USER_GENDER"
  | "USER_NATIONALITY"
  | "USER_BIRTHDAY"
  | "USER_PHONE"
  | "USER_ADDRESS"
  | "USER_EMAIL"
  | "USER_CONSUMPTION_HISTORY";

type ConsentedUserData = Partial<Record<ConsentedUserDataKey, string>>;
```

只会包含与所请求项目对应的键。

### 错误

错误代码是 catch 到的错误的 `error.code` 值。

| 代码                                     | 说明                                                                                                                   |
| -------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `UNSUPPORTED_APP_VERSION`              | 如果当前运行的 Toss App 版本不支持此功能，则调用后会立即发生。 `User.getConsentedData.isSupported()`请先通过它确认，发生时 `error.message`请向用户显示（更新提示文案）。 |
| `USER_DECLINED`                        | 用户明确拒绝，且没有重新请求选项，或者在条款 WebView 中拒绝同意的情况。                                                                             |
| `UNAVAILABLE`                          | 服务器无法判断用户是否同意的情况。                                                                                                    |
| `TERMS_NOT_SET`                        | Mini App 中未设置用户数据提供同意文的情况。                                                                                           |
| `INVALID_REQUEST`                      | `consentedUserDataKey`为空，或者 `termsUrl`不存在，或者不是 HTTPS 公司域名的情况。                                                        |
| `CANCELED`                             | 条款 WebView 在发送结果之前被关闭的情况。                                                                                            |
| `CONSENTED_USER_DATA_AGREEMENT_FAILED` | 处理条款 WebView 时发生错误的情况。                                                                                               |
| `CONSENTED_USER_DATA_INVALID_DATA`     | `PROVIDED`但 `data`不存在，或者同意成功后重新查询结果 `PROVIDED`不是该值的情况。                                                               |

### 示例代码

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

async function fetchDeliveryUserData() {
  try {
    const data = await User.getConsentedData({
      consentedUserDataKey: "cud_delivery",
    });

    console.log("用户姓名：", data?.USER_NAME);
    console.log("电话号码：", data?.USER_PHONE);
  } catch (error) {
    console.error("同意数据查询失败：", error);
  }
}
```


---

# 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/yong-hu/user.getconsenteddata.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.
