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/workers-playground/api.md
1# Workers Playground API23## Handler45```javascript6export default {7async fetch(request, env, ctx) {8// request: Request, env: {} (empty in playground), ctx: ExecutionContext9return new Response('Hello');10}11};12```1314## Request1516```javascript17const method = request.method; // "GET", "POST"18const url = new URL(request.url); // Parse URL19const headers = request.headers; // Headers object20const body = await request.json(); // Read body (consumes stream)21const clone = request.clone(); // Clone before reading body2223// Query params24url.searchParams.get('page'); // Single value25url.searchParams.getAll('tag'); // Array2627// Cloudflare metadata28request.cf.country; // "US"29request.cf.colo; // "SFO"30```3132## Response3334```javascript35// Text36return new Response('Hello', { status: 200 });3738// JSON39return Response.json({ data }, { status: 200, headers: {...} });4041// Redirect42return Response.redirect('/new-path', 301);4344// Modify existing45const modified = new Response(response.body, response);46modified.headers.set('X-Custom', 'value');47```4849## ExecutionContext5051```javascript52// Background work (after response sent)53ctx.waitUntil(fetch('https://logs.example.com', { method: 'POST', body: '...' }));54return new Response('OK'); // Returns immediately55```5657## Fetch5859```javascript60const response = await fetch('https://api.example.com');61const data = await response.json();6263// With options64await fetch(url, {65method: 'POST',66headers: { 'Content-Type': 'application/json' },67body: JSON.stringify({ name: 'Alice' })68});69```7071## Cache7273```javascript74const cache = caches.default;7576// Check cache77let response = await cache.match(request);78if (!response) {79response = await fetch(origin);80await cache.put(request, response.clone()); // Clone before put!81}82return response;83```8485## Crypto8687```javascript88crypto.randomUUID(); // UUID v489crypto.getRandomValues(new Uint8Array(16));9091// SHA-256 hash92const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(data));93```9495## Limits (Playground = Free Plan)9697| Resource | Limit |98|----------|-------|99| CPU time | 10ms (Free plan; Paid: 30s default, 5min max) |100| Subrequests | 50 |101| Memory | 128 MB |102