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

# 流媒体蓝图

> 跟踪视频、音频、直播和实时数据传输计费的流媒体数据消耗。

## 用例

探索流媒体蓝图支持的常见场景：

<CardGroup cols={2}>
  <Card title="Video Platforms" icon="video">
    按视频带宽消耗和流媒体质量向客户计费。
  </Card>

  <Card title="Music Streaming" icon="music">
    按订阅层级跟踪每位用户的音频流媒体使用情况。
  </Card>

  <Card title="Live Events" icon="signal-stream">
    监控直播消耗并按带宽使用量收费。
  </Card>

  <Card title="Real-Time Data" icon="wave-pulse">
    跟踪物联网及遥测应用的实时数据传输。
  </Card>
</CardGroup>

<Info>
  非常适合视频/音频流媒体平台、直播服务和实时数据应用。
</Info>

## 快速开始

跟踪客户消耗的流媒体字节：

<Steps>
  <Step title="Install the SDK">
    ```bash theme={null}
    npm install @dodopayments/ingestion-blueprints
    ```
  </Step>

  <Step title="Get Your API Keys">
    * **Dodo Payments API Key**: 从 [Dodo Payments Dashboard](https://app.dodopayments.com/developer/api-keys) 获取
  </Step>

  <Step title="Create a Meter">
    在你的 [Dodo Payments Dashboard](https://app.dodopayments.com/) 中创建一个计量器：

    * **Event Name**: `stream_consumption`（或你偏好的名称）
    * **Aggregation Type**: `sum` 以跟踪总流式传输字节数
    * **Over Property**: `bytes` 以根据带宽使用量计费
  </Step>

  <Step title="Track Stream Usage">
    <CodeGroup>
      ```javascript Video Streaming theme={null}
      import { Ingestion, trackStreamBytes } from '@dodopayments/ingestion-blueprints';

      const ingestion = new Ingestion({
        apiKey: process.env.DODO_PAYMENTS_API_KEY,
        environment: 'test_mode',
        eventName: 'stream_consumption'
      });

      // Track video stream consumption
      await trackStreamBytes(ingestion, {
        customerId: 'customer_123',
        bytes: 10485760, // 10MB
        metadata: {
          stream_type: 'video',
        }
      });
      ```
    </CodeGroup>
  </Step>
</Steps>

## 配置

### 数据摄取配置

<ParamField path="apiKey" type="string" required>
  来自仪表板的 Dodo Payments API 密钥。
</ParamField>

<ParamField path="environment" type="string" required>
  环境模式：`test_mode` 或 `live_mode`。
</ParamField>

<ParamField path="eventName" type="string" required>
  与计量器配置匹配的事件名称。
</ParamField>

### 跟踪流媒体字节选项

<ParamField path="customerId" type="string" required>
  用于计费归属的客户 ID。
</ParamField>

<ParamField path="bytes" type="number">
  流中消耗的字节数。按基于带宽的计费，这是必需的。
</ParamField>

<ParamField path="metadata" type="object">
  有关流的可选元数据，如流类型、质量、sessionId 等。
</ParamField>

## 最佳实践

<Tip>
  **按块跟踪**：对于长流，按块跟踪消耗，而不是等待整个流完成。
</Tip>

<Warning>
  **准确的字节计数**：如果按总带宽计费，请确保字节计数包含所有开销（头部、协议开销）。
</Warning>
