> 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/cu-xiao/promotion.opencontactsinvite.md).

# Promotion.openContactsInvite

> 函数式 API `contactsViral`也可以使用相同的功能。函数式 API 在不支持的应用版本中不会抛出错误，而是返回一个空的 cleanup 函数。

### 功能说明

提供可分享给朋友并获得奖励的功能。用户完成向朋友分享后，会通过事件传递奖励信息。

Toss应用 **5.223.0** 可在以上版本中使用。调用前 `Promotion.openContactsInvite.isSupported()`可通过它来确认是否支持。在不支持的版本中调用时， `UNSUPPORTED_APP_VERSION` 会发生错误。

返回的 cleanup 函数在分享功能结束后必须调用，以释放资源。

### 类型

```ts
Promotion.openContactsInvite(params: ContactsViralParams): () => void;
```

**参数**

```ts
type ContactsViralOption = {
  /** 共享奖励的 UUID 格式唯一 ID。可在 Appintos 控制台的迷你应用 > 分享奖励菜单中查看。 */
  moduleId: string;
};

type RewardFromContactsViralEvent = {
  type: "sendViral";
  data: {
    rewardAmount: number;
    rewardUnit: string;
  };
};

type ContactsViralSuccessEvent = {
  type: "close";
  data: {
    closeReason: "clickBackButton" | "noReward";
    sentRewardAmount?: number;
    sendableRewardsCount?: number;
    sentRewardsCount: number;
    rewardUnit?: string;
  };
};

type ContactsViralEvent =
  RewardFromContactsViralEvent | ContactsViralSuccessEvent;

type ContactsViralParams = {
  options: ContactsViralOption;
  onEvent: (event: ContactsViralEvent) => void;
  onError: (error: unknown) => void;
};
```

**响应**

返回应用桥接的 cleanup 函数。

### 错误代码

错误代码可 `onError`作为……接收，或在包裹调用的 `try/catch`在其中捕获的错误的 `error.code` 值。

| 代码                        | 说明                                                                                                                          |
| ------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `UNSUPPORTED_APP_VERSION` | 如果当前运行的 Toss App 版本不支持此功能，则调用后会立即发生。 `Promotion.openContactsInvite.isSupported()`请先通过它确认，发生时 `error.message`请向用户显示（更新提示文案）。 |

### 示例代码

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

function handleOpenContactsInvite(moduleId) {
  try {
    const cleanup = Promotion.openContactsInvite({
      options: { moduleId: moduleId.trim() },
      onEvent: (event) => {
        if (event.type === "sendViral") {
          console.log(
            "奖励发放:",
            event.data.rewardAmount,
            event.data.rewardUnit,
          );
        } else if (event.type === "close") {
          console.log("模块关闭:", event.data.closeReason);
          cleanup();
        }
      },
      onError: (error) => {
        console.error("发生错误:", error);
        cleanup?.();
      },
    });
  } catch (error) {
    console.error("执行中发生错误:", error);
  }
}

document
  .querySelector("#contacts-invite")
  .addEventListener("click", () => handleOpenContactsInvite("MODULE_ID"));
```


---

# 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/cu-xiao/promotion.opencontactsinvite.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.
