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/observability/api.md
1## API Reference23### GraphQL Analytics API45**Endpoint**: `https://api.cloudflare.com/client/v4/graphql`67**Query Workers Metrics**:8```graphql9query {10viewer {11accounts(filter: { accountTag: $accountId }) {12workersInvocationsAdaptive(13limit: 10014filter: {15datetime_geq: "2025-01-01T00:00:00Z"16datetime_leq: "2025-01-31T23:59:59Z"17scriptName: "my-worker"18}19) {20sum {21requests22errors23subrequests24}25quantiles {26cpuTimeP5027cpuTimeP9928wallTimeP5029wallTimeP9930}31}32}33}34}35```3637### Analytics Engine SQL API3839**Endpoint**: `https://api.cloudflare.com/client/v4/accounts/{account_id}/analytics_engine/sql`4041**Authentication**: `Authorization: Bearer <API_TOKEN>` (Account Analytics Read permission)4243**Common Queries**:4445```sql46-- List all datasets47SHOW TABLES;4849-- Time-series aggregation (5-minute buckets)50SELECT51intDiv(toUInt32(timestamp), 300) * 300 AS time_bucket,52blob1 AS endpoint,53SUM(_sample_interval) AS total_requests,54AVG(double1) AS avg_response_time_ms55FROM api_metrics56WHERE timestamp >= NOW() - INTERVAL '24' HOUR57GROUP BY time_bucket, endpoint58ORDER BY time_bucket DESC;5960-- Top customers by usage61SELECT62index1 AS customer_id,63SUM(_sample_interval * double1) AS total_api_calls,64AVG(double2) AS avg_response_time_ms65FROM api_usage66WHERE timestamp >= NOW() - INTERVAL '7' DAY67GROUP BY customer_id68ORDER BY total_api_calls DESC69LIMIT 100;7071-- Error rate analysis72SELECT73blob1 AS error_type,74COUNT(*) AS occurrences,75MAX(timestamp) AS last_seen76FROM error_tracking77WHERE timestamp >= NOW() - INTERVAL '1' HOUR78GROUP BY error_type79ORDER BY occurrences DESC;80```8182### Console Logging API8384**Methods**:85```typescript86// Standard methods (all appear in Workers Logs)87console.log('info message');88console.info('info message');89console.warn('warning message');90console.error('error message');91console.debug('debug message');9293// Structured logging (recommended)94console.log({95level: 'info',96user_id: '123',97action: 'checkout',98amount: 99.99,99currency: 'USD'100});101```102103**Log Levels**: All console methods produce logs; use structured fields for filtering:104```typescript105console.log({106level: 'error',107message: 'Payment failed',108error_code: 'CARD_DECLINED'109});110```111112### Analytics Engine Binding Types113114```typescript115interface AnalyticsEngineDataset {116writeDataPoint(event: AnalyticsEngineDataPoint): void;117}118119interface AnalyticsEngineDataPoint {120// Indexed strings (use for filtering/grouping)121indexes?: string[];122123// Non-indexed strings (metadata, IDs, URLs)124blobs?: string[];125126// Numeric values (counts, durations, amounts)127doubles?: number[];128}129```130131**Field Limits**:132- Max 20 indexes133- Max 20 blobs134- Max 20 doubles135- Max 25 `writeDataPoint` calls per request136137### Tail Consumer Event Type138139```typescript140interface TraceItem {141event: TraceEvent;142logs: TraceLog[];143exceptions: TraceException[];144scriptName?: string;145}146147interface TraceEvent {148outcome: 'ok' | 'exception' | 'exceededCpu' | 'exceededMemory' | 'unknown';149cpuTime: number; // microseconds150wallTime: number; // microseconds151}152153interface TraceLog {154timestamp: number;155level: 'log' | 'info' | 'debug' | 'warn' | 'error';156message: any; // string or structured object157}158159interface TraceException {160name: string;161message: string;162timestamp: number;163}164```