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

# 認證

> 使用 API 密鑰認證你的 API 請求

所有 API 請求必須透過在 `x-api-key` 請求標頭中傳遞 API 密鑰來進行認證。

## API 密鑰

啟潤支付使用 API 密鑰來認證請求。你可以在[商戶控制台](https://dashboard.kyren.top)中管理你的 API 密鑰。

當前後端實作接受生產 API 密鑰：

| 密鑰類型     | 前綴            | 用途          |
| -------- | ------------- | ----------- |
| **生產密鑰** | `kyren_live_` | 生產環境 — 真實付款 |

## 發送認證請求

在每個請求的 `x-api-key` 請求標頭中包含你的 API 密鑰：

```bash theme={null}
curl https://api.kyren.top/v1/products \
  -H "x-api-key: kyren_live_xxxxxxxxxxxxxxxxxxxx"
```

<Warning>
  **請妥善保管你的 API 密鑰。**

  * 切勿在客戶端程式碼（JavaScript、行動應用）中暴露 API 密鑰
  * 切勿將 API 密鑰提交到版本控制系統
  * 在伺服器上使用環境變數儲存密鑰
  * 如果密鑰洩露，請立即重新產生
</Warning>

## 環境

| 環境      | Base URL                | API 密鑰前綴                     |
| ------- | ----------------------- | ---------------------------- |
| 生產環境    | `https://api.kyren.top` | `kyren_live_`                |
| Staging | 由 Kyren 支援提供            | 僅使用 Kyren 為該 Staging 環境提供的憑證 |

<Info>
  目前公開生產 API 接受 `kyren_live_` 前綴的生產密鑰。
  商戶控制台目前不提供自助建立 `kyren_test_` 測試密鑰。
  只有在 Kyren 向你提供對應環境的 Base URL 和憑證時，才應使用 Staging 環境。
</Info>

## 重新產生 API 密鑰

如果你的 API 密鑰洩露，可以在控制台中重新產生：

1. 前往 **控制台 > 開發者**
2. 點擊要替換的密鑰旁邊的**重新產生**
3. 確認操作

<Warning>
  重新產生密鑰會立即使舊密鑰失效。請確保更新所有整合中的密鑰。
</Warning>

## 錯誤回應

認證失敗時，API 返回 `401 Unauthorized` 回應：

```json theme={null}
{
  "code": 401,
  "message": "Unauthorized"
}
```

常見原因：

* 缺少 `x-api-key` 請求標頭
* API 密鑰無效或已過期
* 使用了當前環境不接受的 API Key 前綴
