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/api/api.md
1# API Reference23## Client Initialization45### TypeScript67```typescript8import Cloudflare from 'cloudflare';910const client = new Cloudflare({11apiToken: process.env.CLOUDFLARE_API_TOKEN,12});13```1415### Python1617```python18from cloudflare import Cloudflare1920client = Cloudflare(api_token=os.environ.get("CLOUDFLARE_API_TOKEN"))2122# For async:23from cloudflare import AsyncCloudflare24client = AsyncCloudflare(api_token=os.environ["CLOUDFLARE_API_TOKEN"])25```2627### Go2829```go30import (31"github.com/cloudflare/cloudflare-go/v4"32"github.com/cloudflare/cloudflare-go/v4/option"33)3435client := cloudflare.NewClient(36option.WithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN")),37)38```3940## Authentication4142### API Token (Recommended)4344**Create token**: Dashboard → My Profile → API Tokens → Create Token4546```bash47export CLOUDFLARE_API_TOKEN='your-token-here'4849curl "https://api.cloudflare.com/client/v4/zones" \50--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"51```5253**Token scopes**: Always use minimal permissions (zone-specific, time-limited).5455### API Key (Legacy)5657```bash58curl "https://api.cloudflare.com/client/v4/zones" \59--header "X-Auth-Email: [email protected]" \60--header "X-Auth-Key: $CLOUDFLARE_API_KEY"61```6263**Not recommended:** Full account access, cannot scope permissions.6465## Auto-Pagination6667All SDKs support automatic pagination for list operations.6869```typescript70// TypeScript: for await...of71for await (const zone of client.zones.list()) {72console.log(zone.id);73}74```7576```python77# Python: iterator protocol78for zone in client.zones.list():79print(zone.id)80```8182```go83// Go: ListAutoPaging84iter := client.Zones.ListAutoPaging(ctx, cloudflare.ZoneListParams{})85for iter.Next() {86zone := iter.Current()87fmt.Println(zone.ID)88}89```9091## Error Handling9293```typescript94try {95const zone = await client.zones.get({ zone_id: 'xxx' });96} catch (err) {97if (err instanceof Cloudflare.NotFoundError) {98// 40499} else if (err instanceof Cloudflare.RateLimitError) {100// 429 - SDK auto-retries with backoff101} else if (err instanceof Cloudflare.APIError) {102console.log(err.status, err.message);103}104}105```106107**Common Error Types:**108- `AuthenticationError` (401) - Invalid token109- `PermissionDeniedError` (403) - Insufficient scope110- `NotFoundError` (404) - Resource not found111- `RateLimitError` (429) - Rate limit exceeded112- `InternalServerError` (≥500) - Cloudflare error113114## Zone Management115116```typescript117// List zones118const zones = await client.zones.list({119account: { id: 'account-id' },120status: 'active',121});122123// Create zone124const zone = await client.zones.create({125account: { id: 'account-id' },126name: 'example.com',127type: 'full', // or 'partial'128});129130// Update zone131await client.zones.edit('zone-id', {132paused: false,133});134135// Delete zone136await client.zones.delete('zone-id');137```138139```go140// Go: requires cloudflare.F() wrapper141zone, err := client.Zones.New(ctx, cloudflare.ZoneNewParams{142Account: cloudflare.F(cloudflare.ZoneNewParamsAccount{143ID: cloudflare.F("account-id"),144}),145Name: cloudflare.F("example.com"),146Type: cloudflare.F(cloudflare.ZoneNewParamsTypeFull),147})148```149150## DNS Management151152```typescript153// Create DNS record154await client.dns.records.create({155zone_id: 'zone-id',156type: 'A',157name: 'subdomain.example.com',158content: '192.0.2.1',159ttl: 1, // auto160proxied: true, // Orange cloud161});162163// List DNS records (with auto-pagination)164for await (const record of client.dns.records.list({165zone_id: 'zone-id',166type: 'A',167})) {168console.log(record.name, record.content);169}170171// Update DNS record172await client.dns.records.update({173zone_id: 'zone-id',174dns_record_id: 'record-id',175type: 'A',176name: 'subdomain.example.com',177content: '203.0.113.1',178proxied: true,179});180181// Delete DNS record182await client.dns.records.delete({183zone_id: 'zone-id',184dns_record_id: 'record-id',185});186```187188```python189# Python example190client.dns.records.create(191zone_id="zone-id",192type="A",193name="subdomain.example.com",194content="192.0.2.1",195ttl=1,196proxied=True,197)198```199200## See Also201202- [configuration.md](./configuration.md) - SDK configuration, environment variables203- [patterns.md](./patterns.md) - Real-world patterns and workflows204- [gotchas.md](./gotchas.md) - Rate limits, troubleshooting205