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

# 通过Webhook进行Amazon数据交付

> 配置 webhook 交付，在收集完成后立即在您的 HTTP 端点接收抓取的亚马逊数据。

本指南展示如何设置 Webhook 传递，使您的服务器在采集任务完成时自动接收抓取的亚马逊数据。

## 前置条件

* 一个[Bright Data 账户](https://www.bright.cn/cp/start)，拥有有效的 API key
* 熟悉[异步请求工作流](/datasets/scrapers/amazon/async-requests)
* 一个可公开访问的 HTTP 端点（或测试工具，如 [webhook.site](https://webhook.site)）

## Webhook 如何工作

当您使用 `endpoint` URL 触发异步采集时，Bright Data 会在任务完成后向您的端点发送 `POST` 请求，包含抓取的数据。无需轮询。

```
Your app --> POST /trigger (with webhook URL) --> Bright Data scrapes --> POST to your webhook
```

## 第 1 步：设置测试 webhook

为了测试，使用 [webhook.site](https://webhook.site) 获取临时公共 URL：

1. 在浏览器中打开 [webhook.site](https://webhook.site)
2. 复制显示的唯一 URL（例如，`https://webhook.site/abc-123-def`）
3. 保持页面打开以监控传入的请求

## 第 2 步：使用 webhook URL 触发采集

将 `endpoint` 查询参数添加到异步 `/trigger` 请求中：

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST \
    "https://api.brightdata.com/datasets/v3/trigger?dataset_id=gd_l7q7dkf244hwjntr0&format=json&endpoint=https://webhook.site/abc-123-def&uncompressed_webhook=true" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '[
      {"url": "https://www.amazon.com/dp/B0CHHSFMRL"},
      {"url": "https://www.amazon.com/dp/B09V3KXJPB"}
    ]'
  ```

  ```python Python theme={null}
  import requests

  WEBHOOK_URL = "https://webhook.site/abc-123-def"

  response = requests.post(
      "https://api.brightdata.com/datasets/v3/trigger",
      params={
          "dataset_id": "gd_l7q7dkf244hwjntr0",
          "format": "json",
          "webhook": WEBHOOK_URL,
          "uncompressed_webhook": "true",
      },
      headers={
          "Authorization": "Bearer YOUR_API_KEY",
          "Content-Type": "application/json",
      },
      json=[
          {"url": "https://www.amazon.com/dp/B0CHHSFMRL"},
          {"url": "https://www.amazon.com/dp/B09V3KXJPB"},
      ],
  )

  print("Snapshot ID:", response.json()["snapshot_id"])
  ```

  ```javascript Node.js theme={null}
  const WEBHOOK_URL = "https://webhook.site/abc-123-def";

  const response = await fetch(
    `https://api.brightdata.com/datasets/v3/trigger?dataset_id=gd_l7q7dkf244hwjntr0&format=json&endpoint=${encodeURIComponent(WEBHOOK_URL)}&uncompressed_webhook=true`,
    {
      method: "POST",
      headers: {
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
      },
      body: JSON.stringify([
        { url: "https://www.amazon.com/dp/B0CHHSFMRL" },
        { url: "https://www.amazon.com/dp/B09V3KXJPB" },
      ]),
    }
  );

  const data = await response.json();
  console.log("Snapshot ID:", data.snapshot_id);
  ```
</CodeGroup>

关键参数：

| 参数                     | 说明                                |
| :--------------------- | :-------------------------------- |
| `endpoint`             | 接收 `POST` 负载的 HTTP 端点 URL         |
| `uncompressed_webhook` | 设置为 `true` 以接收未压缩的 JSON（默认为 gzip） |
| `format`               | 输出格式：`json`、`ndjson` 或 `csv`      |

## 第 3 步：验证传递

采集完成后（通常对于几个产品需要 30-60 秒），检查您的 webhook.site 页面。您应该会看到一个包含抓取数据的 `POST` 请求。

负载是与从直接 API 下载相同的 JSON 数组：

```json theme={null}
[
  {
    "title": "Apple AirPods Pro (2nd Generation)",
    "asin": "B0CHHSFMRL",
    "price": 189.99,
    "currency": "USD",
    "rating": 4.7,
    "reviews_count": 85420,
    "seller_name": "Amazon.com",
    "brand": "Apple"
  },
  {
    "title": "Echo Dot (5th Gen)",
    "asin": "B09V3KXJPB",
    "price": 49.99,
    "currency": "USD",
    "rating": 4.6,
    "reviews_count": 127800,
    "seller_name": "Amazon.com",
    "brand": "Amazon"
  }
]
```

## 生产环境 webhook 设置

对于生产环境，将 `endpoint` URL 指向您自己的服务器端点。

### Express.js 处理程序

```javascript server.js theme={null}
const express = require("express");
const app = express();

app.use(express.json({ limit: "100mb" }));

app.post("/webhook/amazon", (req, res) => {
  const products = req.body;
  console.log(`Received ${products.length} products`);

  for (const product of products) {
    console.log(`- ${product.title} ($${product.price})`);
  }

  res.status(200).json({ received: true });
});

app.listen(3000, () => console.log("Webhook server running on port 3000"));
```

### Flask 处理程序

```python server.py theme={null}
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route("/webhook/amazon", methods=["POST"])
def handle_webhook():
    products = request.get_json()
    print(f"Received {len(products)} products")

    for product in products:
        print(f"- {product['title']} (${product.get('price')})")

    return jsonify({"received": True}), 200

if __name__ == "__main__":
    app.run(port=3000)
```

<Warning>
  在 30 秒内返回 `200` 状态码以确认收到。如果您的端点失败或超时，Bright Data 会重试传递。
</Warning>

## 带授权的 Webhook

如果您的端点需要身份验证，添加 `webhook_header_Authorization` 参数：

```bash theme={null}
curl -X POST \
  "https://api.brightdata.com/datasets/v3/trigger?dataset_id=gd_l7q7dkf244hwjntr0&format=json&endpoint=https://your-server.com/webhook&webhook_header_Authorization=Bearer+YOUR_SECRET_TOKEN" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '[{"url": "https://www.amazon.com/dp/B0CHHSFMRL"}]'
```

## Webhook IP 白名单

如果您的服务器使用 IP 白名单，添加以下 Bright Data webhook 源 IP：

```
54.175.27.69
34.225.9.175
100.28.38.247
100.29.18.195
52.72.185.255
35.174.112.248
54.165.183.124
3.91.140.7
52.202.75.37
98.82.225.117
100.27.150.189
18.214.10.85
35.169.71.210
44.194.183.74
```

## 故障排除

<Accordion title="Webhook 未收到数据？">
  * 验证 URL 是否可公开访问（不是 `localhost`）
  * 检查您的端点是否在 30 秒内返回 `200` 状态码
  * 验证如果您有防火墙规则，上述 webhook IP 是否已白名单
</Accordion>

<Accordion title="接收压缩数据？">
  如果您省略 `uncompressed_webhook=true`，数据会以 gzip 压缩形式到达。将 `uncompressed_webhook=true` 添加到您的触发 URL，或在您的服务器上解压负载。
</Accordion>

<Accordion title="负载对您的服务器来说太大？">
  大型采集可以产生高达 1 GB 的负载。在 Express.js 中设置 `express.json({ limit: "100mb" })` 或在您的框架中设置等效选项。如果您需要处理非常大的数据集，改用 [S3 传递](/datasets/scrapers/amazon/data-delivery/amazon-s3)。
</Accordion>

## 后续步骤

<CardGroup cols={2}>
  <Card title="传递到 Amazon S3" icon="bucket" href="/datasets/scrapers/amazon/data-delivery/amazon-s3">
    直接将结果存储在您的 S3 存储桶中。
  </Card>

  <Card title="所有传递选项" icon="truck" href="/datasets/scrapers/scrapers-library/delivery-options">
    Snowflake、Azure、GCS 等。
  </Card>
</CardGroup>
