Skip to content

完整示例 ​

下面的示例演示一个安全的自动化流程:读取可用套餐 → 创建网站 → 验证状态 → 提交缓存预热。示例使用占位符,请替换环境变量。

cURL 流程 ​

bash
export KUNTT_API_KEY='你的API密钥'
export KUNTT_BASE='https://cdn.1888.news/api/open/v1'

# 1. 找到可用套餐
curl --fail-with-body "$KUNTT_BASE/packages" \
  -H "Authorization: Bearer $KUNTT_API_KEY"

# 2. 创建网站
curl --fail-with-body -X POST "$KUNTT_BASE/sites" \
  -H "Authorization: Bearer $KUNTT_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "domain":"cdn.example.com",
    "packageId":"你的套餐ID",
    "origins":[{"address":"origin.example.com","weight":1}],
    "originProtocol":"https",
    "httpsPort":443
  }'

# 3. 修改源站(把 SITE_ID 替换成上一步返回的 ID)
curl --fail-with-body -X PATCH "$KUNTT_BASE/sites/SITE_ID/origin" \
  -H "Authorization: Bearer $KUNTT_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "origins":[{"address":"203.0.113.10","weight":1}],
    "originProtocol":"http",
    "httpPort":80
  }'

# 4. 提交静态资源预热
curl --fail-with-body -X POST "$KUNTT_BASE/cache/tasks" \
  -H "Authorization: Bearer $KUNTT_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "type":"preheat",
    "urls":["https://cdn.example.com/assets/app.js"]
  }'

PowerShell 示例 ​

powershell
$base = 'https://cdn.1888.news/api/open/v1'
$headers = @{ Authorization = "Bearer $env:KUNTT_API_KEY" }

$account = Invoke-RestMethod -Method Get -Uri "$base/account" -Headers $headers
$sites = Invoke-RestMethod -Method Get -Uri "$base/sites?limit=100" -Headers $headers

$body = @{
  domain = 'cdn.example.com'
  packageId = '你的套餐ID'
  origins = @(@{ address = 'origin.example.com'; weight = 1 })
  originProtocol = 'follow'
} | ConvertTo-Json -Depth 5

$created = Invoke-RestMethod -Method Post -Uri "$base/sites" -Headers ($headers + @{'Content-Type'='application/json'}) -Body $body
$created.data.id

JavaScript 错误处理 ​

js
async function request(path, init = {}) {
  const response = await fetch(`${process.env.KUNTT_BASE}${path}`, {
    ...init,
    headers: {
      Authorization: `Bearer ${process.env.KUNTT_API_KEY}`,
      'Content-Type': 'application/json',
      ...init.headers
    }
  });
  const result = await response.json();
  if (response.status === 429) {
    const wait = response.headers.get('Retry-After') || '60';
    throw new Error(`限流,请 ${wait} 秒后重试`);
  }
  if (!response.ok || result.code !== 0) {
    throw new Error(`${result.code}: ${result.msg}; requestId=${result.requestId}`);
  }
  return result.data;
}

幂等策略

创建网站前先用 GET /sites 检查域名是否已存在;发布系统重试时也应再次检查,避免网络超时导致重复创建。

基于 VitePress 构建