1. 创建 API 密钥
登录客户门户的「Reseller API」并创建一个密钥。仅启用您实际需要的权限范围(默认为只读)。密钥只显示一次,请妥善保存于您的密钥管理工具中。
2. 存储凭据
将 key 与 secret 存储为环境变量。切勿写入代码,切勿提交到代码仓库。
# .env 或 shell 配置文件
export KH_KEY="kh_live_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
export KH_SECRET="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
3. 发送第一个请求
列出可用产品。签名基于方法、路径、时间戳、随机数与请求体哈希生成。
TS=$(date +%s)
NONCE=$(openssl rand -hex 16)
BODY_SHA256=$(printf '' | openssl dgst -sha256 -hex | awk '{print $2}')
SIG_INPUT=$(printf 'GET\n/v1/products\n%s\n%s\n%s' "$TS" "$NONCE" "$BODY_SHA256")
SIG=$(printf '%s' "$SIG_INPUT" | openssl dgst -sha256 -hmac "$KH_SECRET" -hex | awk '{print $2}')
curl https://www.kernelhost.com/cp/kh_reseller_api/v1/products \
-H "KH-Key: $KH_KEY" \
-H "KH-Timestamp: $TS" \
-H "KH-Nonce: $NONCE" \
-H "KH-Signature: $SIG"
4. 提交订单
重要:POST /v1/orders 必须包含 Idempotency-Key。在网络重试时使用同一个 Key 将返回相同的响应,不会产生重复订单。
BODY='{"product_id":42,"billing_cycle":"monthly"}'
BODY_SHA256=$(printf '%s' "$BODY" | openssl dgst -sha256 -hex | awk '{print $2}')
TS=$(date +%s)
NONCE=$(openssl rand -hex 16)
IDEM=$(openssl rand -hex 16)
SIG_INPUT=$(printf 'POST\n/v1/orders\n%s\n%s\n%s' "$TS" "$NONCE" "$BODY_SHA256")
SIG=$(printf '%s' "$SIG_INPUT" | openssl dgst -sha256 -hmac "$KH_SECRET" -hex | awk '{print $2}')
curl -X POST https://www.kernelhost.com/cp/kh_reseller_api/v1/orders \
-H "KH-Key: $KH_KEY" \
-H "KH-Timestamp: $TS" \
-H "KH-Nonce: $NONCE" \
-H "KH-Signature: $SIG" \
-H "Idempotency-Key: $IDEM" \
-H "Content-Type: application/json" \
--data "$BODY"
5. 配置 Webhook(可选)
为每个密钥注册一个 HTTPS URL,以异步接收事件(order.paid、service.provisioned、invoice.created)。载荷签名遵循 Stripe 方案。

