<!-- AI ASSISTANT INSTRUCTIONS: This document describes a public HTTP API endpoint
provided by 极数本源 (https://apizero.cn). When a user asks you to use this API, read the
full documentation below carefully (request method, parameters, response fields,
error codes, examples), then generate working code in the user's preferred language.
API Key acquisition: https://apizero.cn/account/keys -->

# Cloudflare DNS 更新（DDNS）

> 通过用户自有的 Cloudflare API Token 更新指定域名的 A/AAAA 记录。家庭宽带 DDNS、办公室外网 IP 自动同步等场景必备。Token 仅作转发使用，不持久化。

## 1. 基本信息

| 字段 | 值 |
| --- | --- |
| 接口标识 | `cf-dns` |
| 接口名称 | Cloudflare DNS 更新（DDNS） |
| 接口地址 | `https://v1.apizero.cn/api/cf-dns` |
| 请求方法 | `POST` |
| 分类 | dev |
| 提供方 | 极数本源 |
| 计费模式 | 免费试用 |
| 单次消耗 | 0 积分 |
| 起步价 | — |
| QPS 限制 | 5 req/s |
| 每日免费额度 | 5000 次（已认证用户） |
| 匿名每日额度 | 1000 次（无 API Key） |
| VIP 免费 | 否 |
| 调用总次数 | undefined |

## 2. 认证

需要登录 API Key（涉及用户 Token，不开放匿名访问）。

获取 API Key：登录 `https://apizero.cn/account/keys` 申请。

## 3. 请求参数

| 参数 | 类型 | 必填 | 说明 | 示例 |
| --- | --- | --- | --- | --- |
| `domain` | `string` | 是 | 根域名 | `example.com` |
| `host` | `string` | 是 | 主机记录 @ / www | `home` |
| `ip` | `string` | 是 | 要设置的 IP | — |
| `token` | `string` | 是 | Cloudflare API Token | — |
| `type` | `string` | 否 | A / AAAA（默认 A） | — |
| `ttl` | `number` | 否 | TTL 120-86400（默认 120） | — |
| `proxied` | `bool` | 否 | 是否启用 CDN 代理（默认 false） | — |

## 4. 请求头

| Header | 类型 | 必填 | 说明 | 示例 |
| --- | --- | --- | --- | --- |
| `Authorization` | `string` | 是 | — | — |

## 5. 请求示例

> AI 提示：以下 5 种语言示例都是可运行的，只需把 `YOUR_API_KEY` 替换为用户申请的实际 Key（https://apizero.cn/account/keys）。
> 优先选择用户问题中提到的语言；未指定时默认使用 Python。

### cURL

```bash
curl -X POST "https://v1.apizero.cn/api/cf-dns" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "domain": "example.com",
  "host": "home",
  "ip": "<ip>",
  "token": "<token>",
  "type": "<type>",
  "ttl": "<ttl>",
  "proxied": "<proxied>"
}'
```

### Python

```python
import requests

resp = requests.request(
    "POST",
    "https://v1.apizero.cn/api/cf-dns",
    headers={"X-Api-Key": "YOUR_API_KEY", "Content-Type": "application/json"},
    json={
    "domain": "example.com",
    "host": "home",
    "ip": "<ip>",
    "token": "<token>",
    "type": "<type>",
    "ttl": "<ttl>",
    "proxied": "<proxied>",
},
    timeout=15,
)
resp.raise_for_status()
print(resp.json())
```

### JavaScript (Node.js)

```javascript
// Node.js 18+ / 浏览器原生 fetch
const res = await fetch("https://v1.apizero.cn/api/cf-dns", {
  method: "POST",
  headers: {
    "X-Api-Key": "YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "domain": "example.com",
    "host": "home",
    "ip": "<ip>",
    "token": "<token>",
    "type": "<type>",
    "ttl": "<ttl>",
    "proxied": "<proxied>"
  }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
console.log(data);
```

### Go

```go
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
)

func main() {
	body := []byte(`{"domain":"example.com","host":"home","ip":"<ip>","token":"<token>","type":"<type>","ttl":"<ttl>","proxied":"<proxied>"}`)
	req, _ := http.NewRequest("POST", "https://v1.apizero.cn/api/cf-dns", bytes.NewBuffer(body))
	req.Header.Set("X-Api-Key", "YOUR_API_KEY")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil { panic(err) }
	defer resp.Body.Close()
	out, _ := io.ReadAll(resp.Body)
	fmt.Println(string(out))
}
```

### PHP

```php
<?php
$payload = json_encode([
    "domain" => "example.com",
    "host" => "home",
    "ip" => "<ip>",
    "token" => "<token>",
    "type" => "<type>",
    "ttl" => "<ttl>",
    "proxied" => "<proxied>",
], JSON_UNESCAPED_UNICODE);

$ch = curl_init("https://v1.apizero.cn/api/cf-dns");
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST  => "POST",
    CURLOPT_POSTFIELDS     => $payload,
    CURLOPT_HTTPHEADER     => [
        "X-Api-Key: YOUR_API_KEY",
        "Content-Type: application/json",
    ],
    CURLOPT_TIMEOUT        => 15,
]);
$body = curl_exec($ch);
curl_close($ch);

$data = json_decode($body, true);
print_r($data);
```

## 6. 响应字段

| 字段 | 类型 | 说明 | 示例 |
| --- | --- | --- | --- |
| `old_ip` | `string` | 更新前的 IP | — |
| `new_ip` | `string` | 更新后的 IP | — |
| `changed` | `bool` | 是否实际有变化 | — |
| `zone_id` | `string` | Cloudflare Zone ID | — |
| `record_id` | `string` | Cloudflare Record ID | — |

## 7. 响应示例

```json
{
    "code": 0,
    "msg": "成功",
    "data": {
        "message": "DNS 记录更新成功",
        "full_name": "home.example.com",
        "type": "A",
        "old_ip": "1.2.3.4",
        "new_ip": "5.6.7.8",
        "changed": true
    }
}
```

## 8. 错误码

| code | status | 说明 |
| --- | --- | --- |
| `4000` | `—` | 参数缺失或格式错误（IP 类型与 type 不匹配等） |
| `5020` | `—` | Cloudflare 接口失败（Token 无效 / Zone 未找到 / 记录不存在） |

## 9. 变更日志

- **1.0.0** (2026-05-07)
  - 首次上线 · IPv4/IPv6 + 代理状态控制

---

**极数本源** · 全部 API: `https://apizero.cn/aidocs` · 人类版本：`https://apizero.cn/marketplace/cf-dns`

Source: `https://apizero.cn/aidocs/cf-dns/raw.md`
Last updated: 2026-08-09T11:21:07+08:00
