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/images/configuration.md
1# Configuration23## Wrangler Integration45### Workers Binding Setup67Add to `wrangler.toml`:89```toml10name = "my-image-worker"11main = "src/index.ts"12compatibility_date = "2024-01-01"1314[images]15binding = "IMAGES"16```1718Access in Worker:1920```typescript21interface Env {22IMAGES: ImageBinding;23}2425export default {26async fetch(request: Request, env: Env): Promise<Response> {27return await env.IMAGES28.input(imageBuffer)29.transform({ width: 800 })30.output()31.response();32}33};34```3536### Upload via Script3738Wrangler doesn't have built-in Images commands, use REST API:3940```typescript41// scripts/upload-image.ts42import fs from 'fs';43import FormData from 'form-data';4445async function uploadImage(filePath: string) {46const accountId = process.env.CLOUDFLARE_ACCOUNT_ID!;47const apiToken = process.env.CLOUDFLARE_API_TOKEN!;4849const formData = new FormData();50formData.append('file', fs.createReadStream(filePath));5152const response = await fetch(53`https://api.cloudflare.com/client/v4/accounts/${accountId}/images/v1`,54{55method: 'POST',56headers: {57'Authorization': `Bearer ${apiToken}`,58},59body: formData,60}61);6263const result = await response.json();64console.log('Uploaded:', result);65}6667uploadImage('./photo.jpg');68```6970### Environment Variables7172Store account hash for URL construction:7374```toml75[vars]76IMAGES_ACCOUNT_HASH = "your-account-hash"77ACCOUNT_ID = "your-account-id"78```7980Access in Worker:8182```typescript83const imageUrl = `https://imagedelivery.net/${env.IMAGES_ACCOUNT_HASH}/${imageId}/public`;84```8586## Variants Configuration8788Variants are named presets for transformations.8990### Create Variant (Dashboard)91921. Navigate to Images → Variants932. Click "Create Variant"943. Set name (e.g., `thumbnail`)954. Configure: `width=200,height=200,fit=cover`9697### Create Variant (API)9899```bash100curl -X POST \101https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v1/variants \102-H "Authorization: Bearer {api_token}" \103-H "Content-Type: application/json" \104-d '{105"id": "thumbnail",106"options": {107"width": 200,108"height": 200,109"fit": "cover"110},111"neverRequireSignedURLs": true112}'113```114115### Use Variant116117```118https://imagedelivery.net/{account_hash}/{image_id}/thumbnail119```120121### Common Variant Presets122123```json124{125"thumbnail": {126"width": 200,127"height": 200,128"fit": "cover"129},130"avatar": {131"width": 128,132"height": 128,133"fit": "cover",134"gravity": "face"135},136"hero": {137"width": 1920,138"height": 1080,139"fit": "cover",140"quality": 90141},142"mobile": {143"width": 640,144"fit": "scale-down",145"quality": 80,146"format": "avif"147}148}149```150151## Authentication152153### API Token (Recommended)154155Generate at: Dashboard → My Profile → API Tokens156157Required permissions:158- Account → Cloudflare Images → Edit159160```bash161curl -H "Authorization: Bearer {api_token}" \162https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v1163```164165### API Key (Legacy)166167```bash168curl -H "X-Auth-Email: {email}" \169-H "X-Auth-Key: {api_key}" \170https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v1171```172173## Signed URLs174175For private images, enable signed URLs:176177```bash178# Upload with signed URLs required179curl -X POST \180https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v1 \181-H "Authorization: Bearer {api_token}" \182-F [email protected] \183-F requireSignedURLs=true184```185186Generate signed URL:187188```typescript189import { createHmac } from 'crypto';190191function signUrl(imageId: string, variant: string, expiry: number, key: string): string {192const path = `/${imageId}/${variant}`;193const toSign = `${path}${expiry}`;194const signature = createHmac('sha256', key)195.update(toSign)196.digest('hex');197198return `https://imagedelivery.net/{hash}${path}?exp=${expiry}&sig=${signature}`;199}200201// Sign URL valid for 1 hour202const signedUrl = signUrl('image-id', 'public', Date.now() + 3600, env.SIGNING_KEY);203```204205## Local Development206207```bash208npx wrangler dev --remote209```210211Must use `--remote` for Images binding access.212