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

# 发布商品

> 将一次性商品从测试环境发布到生产环境

将商品从测试环境发布到生产环境。这是一个**单向、仅限首次发布**的操作，将当前测试版本复制到生产环境。

```
POST /v1/actions/onetime-product/publish-product
```

**认证方式：** API Key

<Warning>
  此端点**不要**包含 `X-Environment` 请求头。发布始终是从 test 到 production 的单向操作。
</Warning>

## 请求体

| 字段   | 类型     | 必需 | 说明                            |
| ---- | ------ | -- | ----------------------------- |
| `id` | string | 是  | 商品 ID（Short ID 格式 `PROD_xxx`） |

## 请求示例

<CodeGroup>
  ```typescript SDK theme={"system"}
  const { product } = await client.onetimeProducts.publish({
    id: "PROD_3kF9mNpQrStUvWxYz1A2bC",
  });
  ```

  ```typescript TypeScript (fetch) theme={"system"}
  // Uses callApi() helper from authentication.mdx
  const result = await callApi("POST", "/v1/actions/onetime-product/publish-product", {
    id: "PROD_3kF9mNpQrStUvWxYz1A2bC",
  });
  console.log(result.data.product.status); // => "active"
  ```

  ```java Java theme={"system"}
  // Uses callApi() helper from authentication.mdx
  String body = """
      {"id":"PROD_3kF9mNpQrStUvWxYz1A2bC"}""";

  String response = callApi("POST", "/v1/actions/onetime-product/publish-product", body);
  System.out.println(response);
  ```

  ```python Python theme={"system"}
  # Uses call_api() helper from authentication.mdx
  result = call_api("POST", "/v1/actions/onetime-product/publish-product", {
      "id": "PROD_3kF9mNpQrStUvWxYz1A2bC",
  })
  print(result["data"]["product"]["status"])  # => "active"
  ```

  ```go Go theme={"system"}
  // Uses callAPI() helper from authentication.mdx
  body := map[string]interface{}{
      "id": "PROD_3kF9mNpQrStUvWxYz1A2bC",
  }
  result, err := callAPI("POST", "/v1/actions/onetime-product/publish-product", body)
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println(string(result))
  ```

  ```rust Rust theme={"system"}
  // Uses call_api() helper from authentication.mdx
  let body = serde_json::json!({
      "id": "PROD_3kF9mNpQrStUvWxYz1A2bC"
  });
  let result = call_api("POST", "/v1/actions/onetime-product/publish-product", &body).await?;
  println!("{}", result);
  ```

  ```c C theme={"system"}
  /* Uses call_api() helper from authentication.mdx */
  const char *body = "{\"id\":\"PROD_3kF9mNpQrStUvWxYz1A2bC\"}";
  char *response = call_api("POST", "/v1/actions/onetime-product/publish-product", body);
  printf("%s\n", response);
  free(response);
  ```

  ```cpp C++ theme={"system"}
  // Uses callApi() helper from authentication.mdx
  std::string body = R"({"id":"PROD_3kF9mNpQrStUvWxYz1A2bC"})";
  std::string response = callApi("POST", "/v1/actions/onetime-product/publish-product", body);
  std::cout << response << std::endl;
  ```

  ```bash cURL theme={"system"}
  MERCHANT_ID="MER_2aUyqjCzEIiEcYMKj7TZtw"
  TIMESTAMP=$(date +%s)
  BODY='{"id":"PROD_3kF9mNpQrStUvWxYz1A2bC"}'
  BODY_HASH=$(echo -n "$BODY" | openssl dgst -sha256 -binary | base64 -w 0)
  CANONICAL_REQUEST="POST
  /v1/actions/onetime-product/publish-product
  $TIMESTAMP
  $BODY_HASH"

  SIGNATURE=$(echo -n "$CANONICAL_REQUEST" | openssl dgst -sha256 -sign private_key.pem | base64 -w 0)

  curl -X POST "https://api.waffo.ai/v1/actions/onetime-product/publish-product" \
    -H "Content-Type: application/json" \
    -H "X-Merchant-Id: $MERCHANT_ID" \
    -H "X-Timestamp: $TIMESTAMP" \
    -H "X-Signature: $SIGNATURE" \
    -d "$BODY"
  ```

  ```bash wget theme={"system"}
  MERCHANT_ID="MER_2aUyqjCzEIiEcYMKj7TZtw"
  TIMESTAMP=$(date +%s)
  BODY='{"id":"PROD_3kF9mNpQrStUvWxYz1A2bC"}'
  BODY_HASH=$(echo -n "$BODY" | openssl dgst -sha256 -binary | base64 -w 0)
  CANONICAL_REQUEST="POST
  /v1/actions/onetime-product/publish-product
  $TIMESTAMP
  $BODY_HASH"

  SIGNATURE=$(echo -n "$CANONICAL_REQUEST" | openssl dgst -sha256 -sign private_key.pem | base64 -w 0)

  wget -qO- "https://api.waffo.ai/v1/actions/onetime-product/publish-product" \
    --header="Content-Type: application/json" \
    --header="X-Merchant-Id: $MERCHANT_ID" \
    --header="X-Timestamp: $TIMESTAMP" \
    --header="X-Signature: $SIGNATURE" \
    --post-data="$BODY"
  ```
</CodeGroup>

## 成功响应 (200)

```json theme={"system"}
{
  "data": {
    "product": {
      "id": "PROD_3kF9mNpQrStUvWxYz1A2bC",
      "storeId": "STO_2aUyqjCzEIiEcYMKj7TZtw",
      "name": "Premium Template Pack",
      "description": "50 premium design templates for your next project.",
      "prices": {
        "USD": { "amount": "49.00", "taxCategory": "digital_goods" },
        "EUR": { "amount": "45.00", "taxCategory": "digital_goods" }
      },
      "media": [
        { "type": "image", "url": "https://example.com/templates-preview.png", "alt": "Template preview" }
      ],
      "successUrl": "https://example.com/thank-you",
      "metadata": { "category": "design", "fileCount": "50" },
      "status": "active",
      "createdAt": "2026-01-15T10:30:00.000Z",
      "updatedAt": "2026-01-15T12:00:00.000Z"
    }
  }
}
```

## 响应字段

与 [创建商品响应](/api-reference/endpoints/onetime-products/create-product#response-fields) 相同。

<Note>
  仅支持**首次发布**。商品拥有生产版本后，此端点不能再对同一商品使用。首次发布后如需更新生产版本，请使用 [更新商品](/api-reference/endpoints/onetime-products/update-product) 端点并携带 `X-Environment: prod` 请求头。
</Note>

## 错误响应

> **重试策略**：4xx 一律不要重试 — 修正请求后重发。5xx 指数退避重试（起步 5s，最多 3 次）。

| 状态码 | `errors[0].message`               | 含义                   | 推荐处理                                                                                                        |
| --- | --------------------------------- | -------------------- | ----------------------------------------------------------------------------------------------------------- |
| 400 | `Missing required field: id`      | 未提供 `id`             | 修正请求体后重发                                                                                                    |
| 400 | `Invalid ID format`               | 提供的 Short ID 无法解码    | 修正 `id` 格式后重发                                                                                               |
| 400 | `No test version found`           | 商品在测试环境尚无版本          | 先通过 [Create Product](/api-reference/endpoints/onetime-products/create-product) 创建商品                         |
| 400 | `Test version is not active`      | 当前测试版本状态为 `inactive` | 先通过 [Update Status](/api-reference/endpoints/onetime-products/update-status) 激活测试版本                         |
| 400 | `Already published to production` | 商品已存在生产版本            | 后续变更使用 [Update Product](/api-reference/endpoints/onetime-products/update-product) 并附带 `X-Environment: prod` |
| 404 | `Product not found`               | 商品不存在或不可访问           | 验证 `id` 是否属于当前商户                                                                                            |
| 500 | `Internal server error`           | 服务端意外失败              | 指数退避重试（起步 5s，最多 3 次）                                                                                        |
