Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/tunnel/api.md
1# Tunnel API23## Cloudflare API Access45**Base URL**: `https://api.cloudflare.com/client/v4`67**Authentication**:8```bash9Authorization: Bearer ${CF_API_TOKEN}10```1112## TypeScript SDK1314Install: `npm install cloudflare`1516```typescript17import Cloudflare from 'cloudflare';1819const cf = new Cloudflare({20apiToken: process.env.CF_API_TOKEN,21});2223const accountId = process.env.CF_ACCOUNT_ID;24```2526## Create Tunnel2728### cURL29```bash30curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels" \31-H "Authorization: Bearer ${CF_API_TOKEN}" \32-H "Content-Type: application/json" \33--data '{34"name": "my-tunnel",35"tunnel_secret": "<base64-secret>"36}'37```3839### TypeScript40```typescript41const tunnel = await cf.zeroTrust.tunnels.create({42account_id: accountId,43name: 'my-tunnel',44tunnel_secret: Buffer.from(crypto.randomBytes(32)).toString('base64'),45});4647console.log(`Tunnel ID: ${tunnel.id}`);48```4950## List Tunnels5152### cURL53```bash54curl -X GET "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels" \55-H "Authorization: Bearer ${CF_API_TOKEN}"56```5758### TypeScript59```typescript60const tunnels = await cf.zeroTrust.tunnels.list({61account_id: accountId,62});6364for (const tunnel of tunnels.result) {65console.log(`${tunnel.name}: ${tunnel.id}`);66}67```6869## Get Tunnel Info7071### cURL72```bash73curl -X GET "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}" \74-H "Authorization: Bearer ${CF_API_TOKEN}"75```7677### TypeScript78```typescript79const tunnel = await cf.zeroTrust.tunnels.get(tunnelId, {80account_id: accountId,81});8283console.log(`Status: ${tunnel.status}`);84console.log(`Connections: ${tunnel.connections?.length || 0}`);85```8687## Update Tunnel Config8889### cURL90```bash91curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}/configurations" \92-H "Authorization: Bearer ${CF_API_TOKEN}" \93-H "Content-Type: application/json" \94--data '{95"config": {96"ingress": [97{"hostname": "app.example.com", "service": "http://localhost:8000"},98{"service": "http_status:404"}99]100}101}'102```103104### TypeScript105```typescript106const config = await cf.zeroTrust.tunnels.configurations.update(107tunnelId,108{109account_id: accountId,110config: {111ingress: [112{ hostname: 'app.example.com', service: 'http://localhost:8000' },113{ service: 'http_status:404' },114],115},116}117);118```119120## Delete Tunnel121122### cURL123```bash124curl -X DELETE "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}" \125-H "Authorization: Bearer ${CF_API_TOKEN}"126```127128### TypeScript129```typescript130await cf.zeroTrust.tunnels.delete(tunnelId, {131account_id: accountId,132});133```134135## Token-Based Tunnels (Config Source: Cloudflare)136137Token-based tunnels store config in Cloudflare dashboard instead of local files.138139### Via Dashboard1401. **Zero Trust** > **Networks** > **Tunnels**1412. **Create a tunnel** > **Cloudflared**1423. Configure routes in dashboard1434. Copy token1445. Run on origin:145```bash146cloudflared service install <TOKEN>147```148149### Via Token150```bash151# Run with token (no config file needed)152cloudflared tunnel --no-autoupdate run --token ${TUNNEL_TOKEN}153154# Docker155docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token ${TUNNEL_TOKEN}156```157158### Get Tunnel Token (TypeScript)159```typescript160// Get tunnel to retrieve token161const tunnel = await cf.zeroTrust.tunnels.get(tunnelId, {162account_id: accountId,163});164165// Token available in tunnel.token (only for config source: cloudflare)166const token = tunnel.token;167```168169## DNS Routes API170171```bash172# Create DNS route173curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}/connections" \174-H "Authorization: Bearer ${CF_API_TOKEN}" \175--data '{"hostname": "app.example.com"}'176177# Delete route178curl -X DELETE "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}/connections/{route_id}" \179-H "Authorization: Bearer ${CF_API_TOKEN}"180```181182## Private Network Routes API183184```bash185# Add IP route186curl -X POST "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}/routes" \187-H "Authorization: Bearer ${CF_API_TOKEN}" \188--data '{"ip_network": "10.0.0.0/8"}'189190# List IP routes191curl -X GET "https://api.cloudflare.com/client/v4/accounts/{account_id}/tunnels/{tunnel_id}/routes" \192-H "Authorization: Bearer ${CF_API_TOKEN}"193```194