> ## Documentation Index
> Fetch the complete documentation index at: https://www.ayrshare.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 建立受眾

> 建立由網站訪客、互動用戶、類似受眾或顧客名單組成的 Facebook 自訂受眾，用於推廣時納入或排除。

在 Facebook 廣告帳戶上建立自訂受眾。支援四種類型，透過 `type` 選擇：

<ul>
  <li>`website`：Meta 像素在你的網站上偵測到的人。</li>
  <li>`engagement`：與 Facebook 粉絲專頁、Instagram 帳戶或影片互動過的人。</li>
  <li>`lookalike`：與你已有受眾相似的人。</li>
  <li>`customerList`：你自己的人員名單，透過[受眾成員](https://www.ayrshare.com/docs/apis/ads/facebook/audience-members.md)以雜湊識別碼填充。</li>
</ul>

新受眾以與[受眾](https://www.ayrshare.com/docs/apis/ads/facebook/get-audiences.md)相同的結構在 `audience` 下傳回，`status.ready: false`。Meta 會在接下來幾個小時內填充它；列出受眾以查看 `status.ready` 何時變為 `true`，然後在[推廣貼文](https://www.ayrshare.com/docs/apis/ads/facebook/boost-post.md#audiences)時將其 `id` 用於 `targeting.audiences` 或 `targeting.excludedAudiences`。

> [!NOTE]
>
> 在建立或使用任何受眾之前，廣告帳戶中擁有角色的人員必須在 `https://business.facebook.com/ads/manage/customaudiences/tos/?act=<ad account id>` 接受一次 Meta 的 Custom Audience 條款。Ayrshare 無法代你接受。在此之前，此端點會傳回帶有 `code: 101` 的 `400`，`details` 會指出帳戶以及接受條款的頁面。[廣告帳戶](https://www.ayrshare.com/docs/apis/ads/facebook/get-ad-accounts.md)會在 `tosAccepted.customAudienceTos` 中顯示是否已接受。

> [!WARNING]
>
> Meta 不允許在住房、就業或金融廣告中使用類似受眾。屬於這些[特殊廣告類別](https://www.ayrshare.com/docs/apis/ads/facebook/boost-post.md#param-special-ad-categories)之一的推廣可以納入或排除網站、互動和顧客名單受眾，但不能使用類似受眾。

## 請求頭參數

**Authorization** `string` *required*

[API Key](https://www.ayrshare.com/docs/apis/overview.md#authorization) of the Primary Profile.

Format: `Authorization: Bearer API_KEY`
**Profile-Key** `string`

[Profile Key](https://www.ayrshare.com/docs/apis/overview.md#profile-key-format) of a User Profile.

Format: `Profile-Key: PROFILE_KEY`

## 請求體參數

每種 `type` 都接受以下參數：

**accountId** `string` *required*

要在其上建立受眾的廣告帳戶，來自[廣告帳戶](https://www.ayrshare.com/docs/apis/ads/facebook/get-ad-accounts.md)，帶或不帶 `act_` 前綴皆可。

**name** `string` *required*

在 Ads Manager 中顯示的名稱。

**type** `string` *required*

`website`、`engagement`、`lookalike` 或 `customerList`。每種類型只接受下方列出的自身欄位。來自其他類型的欄位會傳回帶有 `code: 101` 的 `400` 並指出該欄位。

**idempotencyKey** `string`

為每個新受眾提供一個新的唯一值，例如 UUID。如果你以相同的金鑰重複請求，Ayrshare 會傳回第一次的回應，並標記 `idempotentReplayed: true`，不會建立第二個受眾。你也可以改為在 `Idempotency-Key` 標頭中傳送該金鑰；如果兩者都傳送，它們必須一致。將金鑰用於不同的請求，或在第一個請求仍在處理時重複使用該金鑰，會傳回 `409`。`503` 表示無法保留該金鑰且未建立任何內容：請稍後以相同的金鑰重試。

### 網站受眾

**pixelId** `string` *required*

以其事件建立受眾的 Meta 像素。

**retentionDays** `number` *required*

訪客在最後一次造訪後保留在受眾中的天數，1 到 180。

**urlContains** `array`

僅保留造訪包含以下任一字串之 URL 的紀錄，例如 `["/pricing"]`。

**event** `string`

僅保留觸發此像素事件的訪客，例如 `Purchase` 或自訂事件名稱。省略則計算每次造訪。

### 互動受眾

**source** `string` *required*

互動發生的位置：`page`（Facebook 粉絲專頁）、`instagram`（Instagram 帳戶）或 `video`。

**sourceIds** `array` *required*

用於計算互動的粉絲專頁 ID、Instagram 帳戶 ID 或影片 ID，以字串形式提供。

**event** `string` *required*

計算的互動類型。這些值是 Meta 自己的名稱，取決於 `source`：

<ul>
  <li>`page`: `page_engaged`, `page_visited`, `page_liked`, `page_messaged`, `page_cta_clicked`, `page_or_post_save`, `page_post_interaction`</li>
  <li>`instagram`: `ig_business_profile_all`, `ig_business_profile_engaged`, `ig_business_profile_visit`, `ig_business_profile_ad_saved`, `ig_user_messaged_business`, `ig_ad_like`, `ig_ad_comment`, `ig_ad_share`, `ig_ad_save`, `ig_ad_cta_click`, `ig_ad_carousel_swipe`, `ig_organic_like`, `ig_organic_comment`, `ig_organic_share`, `ig_organic_save`, `ig_organic_swipe`, `ig_organic_carousel_swipe`</li>
  <li>`video`: `video_watched`, `video_completed`, `video_view_10s`, `video_view_15s`, `video_view_25_percent`, `video_view_50_percent`, `video_view_75_percent`</li>
</ul>

任何其他值都會傳回帶有 `code: 101` 的 `400`，並列出該來源可用的值。

**retentionDays** `number` *required*

人員在最後一次互動後保留在受眾中的天數：`page` 和 `instagram` 為 1 到 730，`video` 為 1 到 365。

### 類似受眾

**seedAudienceId** `string` *required*

用於尋找相似人員的受眾，來自[受眾](https://www.ayrshare.com/docs/apis/ads/facebook/get-audiences.md)。Meta 需要其中至少有 100 人。

**ratio** `number` *required*

類似受眾的規模，以推廣所鎖定國家人口的整數百分比表示：1（最接近比對）到 20（最廣觸及）。

**allowInternationalSeeds** `boolean`

當某個國家的來源人員太少時，允許 Meta 從其他國家取得來源人員。預設為 `false`。

類似受眾沒有自己的國家：人員來自[推廣的 `targeting.locations`](https://www.ayrshare.com/docs/apis/ads/facebook/boost-post) 指定的國家。傳送 `country` 會傳回帶有 `code: 101` 的 `400`。Meta 需要幾個小時來填充類似受眾。

### 顧客名單受眾

**customerFileSource** `string` (default: USER\_PROVIDED\_ONLY)

名單資料的來源，使用 Meta 的用語：`USER_PROVIDED_ONLY`（由本人提供給你）、`PARTNER_PROVIDED_ONLY`（由合作夥伴提供給你）或 `BOTH_USER_AND_PARTNER_PROVIDED`。

顧客名單建立時為空。使用[受眾成員](https://www.ayrshare.com/docs/apis/ads/facebook/audience-members.md)新增人員。雜湊識別碼仍然是個人資料：上傳名單前請閱讀該頁面。

```bash cURL theme={"system"}
curl \
-H "Authorization: Bearer API_KEY" \
-H "Content-Type: application/json" \
-X POST https://api.ayrshare.com/api/ads/facebook/audiences \
-d '{"accountId": "1234567890", "name": "Site visitors 30d", "type": "website", "pixelId": "987654321098765", "retentionDays": 30, "urlContains": ["/pricing"]}'
```

```javascript JavaScript theme={"system"}
const API_KEY = "API_KEY";

fetch("https://api.ayrshare.com/api/ads/facebook/audiences", {
      method: "POST",
      headers: {
        "Authorization": `Bearer ${API_KEY}`,
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
    "accountId": "1234567890",
    "name": "Site visitors 30d",
    "type": "website",
    "pixelId": "987654321098765",
    "retentionDays": 30,
    "urlContains": [
      "/pricing"
    ]
  })
    })
      .then((res) => res.json())
      .then((json) => console.log(json))
      .catch(console.error);
```

```python Python theme={"system"}
import requests

headers = {'Authorization': 'Bearer API_KEY', 'Content-Type': 'application/json'}
payload = {
  "accountId": "1234567890",
  "name": "Site visitors 30d",
  "type": "website",
  "pixelId": "987654321098765",
  "retentionDays": 30,
  "urlContains": [
    "/pricing"
  ]
}

r = requests.post('https://api.ayrshare.com/api/ads/facebook/audiences', json=payload, headers=headers)

print(r.json())
```

```json 200: Success theme={"system"}
{
  "status": "success",
  "audience": {
    "id": "23851234567890123",
    "name": "Site visitors 30d",
    "type": "website",
    "size": null,
    "status": { "ready": false, "code": 300, "description": "This audience is still being populated." },
    "created": "2026-10-29T10:05:00.000Z"
  }
}
```

```json 400: Field from another type theme={"system"}
{
  "action": "request",
  "status": "error",
  "code": 101,
  "message": "Missing or incorrect parameters. Please verify with the docs. https://www.ayrshare.com/docs/apis",
  "details": "ratio is not a recognised field."
}
```

```json 400: Terms not accepted theme={"system"}
{
  "action": "request",
  "status": "error",
  "code": 101,
  "message": "Missing or incorrect parameters. Please verify with the docs. https://www.ayrshare.com/docs/apis",
  "details": "Creating a custom audience needs the Custom Audience terms accepted on ad account act_123456789. A person with a role on the account accepts them at https://business.facebook.com/ads/manage/customaudiences/tos/?act=123456789"
}
```

```json 400: Meta refused the request theme={"system"}
{
  "action": "ads",
  "status": "error",
  "code": 369,
  "message": "Unable to create the audience. Please try again.",
  "details": "Meta's reason"
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.