> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiJmNDI0MTBkZC0xMzFmLTRlYzEtYmYxYy1jZjVkNDczZjViZTciLCJleHAiOjE3Nzg0OTM3MDksImlhdCI6MTc3ODQ5MzQwOX0.AvyEvebPlvoFCEQUYz2bu306nb7IK94qHWZdb3P5yzo
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 使用受众筛选您的 API

<Warning title="团队版、专业版和企业版功能">
  此功能仅适用于[团队版（文档）、专业版（SDK）和企业版计划](https://buildwithfern.com/pricing)。要开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。
</Warning>

受众是对事件驱动 API 进行分段以服务不同消费者的有用工具。常见的受众示例包括 `public` 和 `beta`。

<Info>
  记住在指定受众后要筛选您的 SDK 和文档。如果**未指定受众**，将不会进行任何筛选。

  <AccordionGroup>
    <Accordion title="SDK">
      以下示例配置 SDK 筛选到 `public` 受众：

      ```yaml title="generators.yml" {3-4}
      groups:
        sdks:
          audiences:
            - public
          generators:
            - name: fernapi/fern-typescript-sdk
              version: 0.8.8
      ```
    </Accordion>

    <Accordion title="文档">
      以下示例配置文档筛选到 `public` 受众：

      ```yaml title="docs.yml" {3-4}
      navigation:
        - api: API Reference
          audiences:
            - public
      ```
    </Accordion>
  </AccordionGroup>
</Info>

## 筛选操作

在操作中添加 `x-fern-audiences` 来控制特定受众包含哪些操作：

```yaml title="asyncapi.yml" {5-6, 12-13}
operations:
  sendPublicNotification:
    action: send
    channel:
      $ref: '#/channels/public~1notifications'
    x-fern-audiences:
      - public
  sendBetaAlert:
    action: send
    channel:
      $ref: '#/channels/beta~1alerts'
    x-fern-audiences:
      - beta
```

## 筛选通道

您还可以按受众筛选整个通道：

```yaml title="asyncapi.yml" {6-7, 13-14}
channels:
  public/events:
    address: public/events
    messages:
      PublicEvent:
        $ref: '#/components/messages/PublicEvent'
    x-fern-audiences:
      - public
  internal/events:
    address: internal/events
    messages:
      InternalEvent:
        $ref: '#/components/messages/InternalEvent'
    x-fern-audiences:
      - internal
```

## 筛选消息模式

将特定消息模式筛选到不同受众：

```yaml title="asyncapi.yml" {5-6, 13-14}
components:
  messages:
    PublicUserEvent:
      contentType: application/json
      payload:
        $ref: '#/components/schemas/PublicUser'
      x-fern-audiences:
        - public
    AdminUserEvent:
      contentType: application/json
      payload:
        $ref: '#/components/schemas/AdminUser'
      x-fern-audiences:
        - admin
  schemas:
    PublicUser:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        email:
          type: string
      x-fern-audiences:
        - public
    AdminUser:
      allOf:
        - $ref: '#/components/schemas/PublicUser'
        - type: object
          properties:
            role:
              type: string
            permissions:
              type: array
              items:
                type: string
      x-fern-audiences:
        - admin
```

## 筛选模式属性

您可以筛选模式内的单个属性：

```yaml title="asyncapi.yml" {9-10, 13-14}
components:
  schemas:
    UserEvent:
      type: object
      properties:
        id:
          type: string
        email:
          type: string
          x-fern-audiences:
            - internal
        publicName:
          type: string
          x-fern-audiences:
            - public
        internalNotes:
          type: string
          x-fern-audiences:
            - internal
```

这使您能够为不同受众提供相同事件模式的不同视图，向每个消费者只显示相关信息。