[ PROMPT_NODE_24370 ]
Tunnel API 参考
[ SKILL_DOCUMENTATION ]
# 隧道 API
## Cloudflare API 访问
**基础 URL**: `https://api.cloudflare.com/client/v4`
**认证**:
bash
Authorization: Bearer ${CF_API_TOKEN}
## TypeScript SDK
安装:`npm install cloudflare`
typescript
import Cloudflare from 'cloudflare';
const cf = new Cloudflare({
apiToken: process.env.CF_API_TOKEN,
});
const accountId = process.env.CF_ACCOUNT_ID;
## 创建隧道
### cURL
bash
curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels"
-H "Authorization: Bearer ${CF_API_TOKEN}"
-H "Content-Type: application/json"
--data '{
"name": "my-tunnel",
"tunnel_secret": ""
}'
### TypeScript
typescript
const tunnel = await cf.zeroTrust.tunnels.create({
account_id: accountId,
name: 'my-tunnel',
tunnel_secret: Buffer.from(crypto.randomBytes(32)).toString('base64'),
});
console.log(`Tunnel ID: ${tunnel.id}`);
## 列出隧道
### cURL
bash
curl -X GET "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels"
-H "Authorization: Bearer ${CF_API_TOKEN}"
### TypeScript
typescript
const tunnels = await cf.zeroTrust.tunnels.list({
account_id: accountId,
});
for (const tunnel of tunnels.result) {
console.log(`${tunnel.name}: ${tunnel.id}`);
}
## 获取隧道信息
### cURL
bash
curl -X GET "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}"
-H "Authorization: Bearer ${CF_API_TOKEN}"
### TypeScript
typescript
const tunnel = await cf.zeroTrust.tunnels.get(tunnelId, {
account_id: accountId,
});
console.log(`Status: ${tunnel.status}`);
console.log(`Connections: ${tunnel.connections?.length || 0}`);
## 更新隧道配置
### cURL
bash
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}/configurations"
-H "Authorization: Bearer ${CF_API_TOKEN}"
-H "Content-Type: application/json"
--data '{
"config": {
"ingress": [
{"hostname": "app.example.com", "service": "http://localhost:8000"},
{"service": "http_status:404"}
]
}
}'
### TypeScript
typescript
const config = await cf.zeroTrust.tunnels.configurations.update(
tunnelId,
{
account_id: accountId,
config: {
ingress: [
{ hostname: 'app.example.com', service: 'http://localhost:8000' },
{ service: 'http_status:404' },
],
},
}
);
## 删除隧道
### cURL
bash
curl -X DELETE "h