Skip to content

secondsky/claude-skills / plugins/cloudflare-images/skills/cloudflare-images

cloudflare-images

Unverified

This skill should be used when the user asks to "upload images to Cloudflare", "implement direct creator upload", "configure image transformations", "optimize WebP/AVIF", "create image variants", "generate signed URLs", "add image watermarks", "integrate with Next.js/Remix", "configure webhooks", "debug CORS errors", "troubleshoot error 5408/9401-9413", or "build responsive images with Cloudflare Images API".

FileKindSize
SKILL.mdskill md13.9 KB
assets/diagrams/direct-upload-workflow.mdmarkdown5.0 KB
assets/diagrams/transformation-pipeline.mdmarkdown8.2 KB
assets/diagrams/variants-structure.mdmarkdown10.6 KB
examples/basic-upload/.env.exampleother474 B
examples/basic-upload/README.mdmarkdown6.5 KB
examples/basic-upload/package.jsonmanifest542 B
examples/basic-upload/public/index.htmlother12.7 KB
examples/basic-upload/src/index.tsscript2.7 KB
examples/basic-upload/wrangler.jsoncother378 B
examples/private-images/.env.exampleother631 B
examples/private-images/README.mdmarkdown12.5 KB
examples/private-images/package.jsonmanifest741 B
examples/private-images/public/index.htmlother18.0 KB
examples/private-images/src/index.tsscript5.2 KB
examples/private-images/wrangler.jsoncother427 B
examples/responsive-gallery/README.mdmarkdown4.3 KB
references/api-reference.mdmarkdown8.1 KB
references/content-credentials.mdmarkdown11.6 KB
references/custom-domains.mdmarkdown10.5 KB
references/direct-upload-complete-workflow.mdmarkdown11.2 KB
references/format-optimization.mdmarkdown7.2 KB
references/framework-integration.mdmarkdown15.4 KB
references/overlays-watermarks.mdmarkdown11.1 KB
references/polish-compression.mdmarkdown10.4 KB
references/responsive-images-patterns.mdmarkdown6.1 KB
references/setup-guide.mdmarkdown3.8 KB
references/signed-urls-guide.mdmarkdown7.5 KB
references/sourcing-kit.mdmarkdown15.0 KB
references/top-errors.mdmarkdown11.7 KB
references/transformation-options.mdmarkdown6.4 KB
references/variants-guide.mdmarkdown6.7 KB
references/webhooks-guide.mdmarkdown11.8 KB
scripts/analyze-usage.shexecutablescript6.6 KB
scripts/check-versions.shexecutablescript3.3 KB
scripts/generate-signed-url.shexecutablescript4.4 KB
scripts/test-upload.shexecutablescript4.4 KB
scripts/validate-variants.shexecutablescript5.2 KB
templates/batch-upload.tsscript7.8 KB
templates/direct-creator-upload-backend.tsscript5.4 KB
templates/direct-creator-upload-frontend.htmlother8.4 KB
templates/nextjs-integration.tsxother11.4 KB
templates/overlay-watermark.tsscript13.7 KB
templates/package.jsonmanifest387 B
templates/remix-integration.tsxother15.0 KB
templates/responsive-images-srcset.htmlother8.0 KB
templates/signed-urls-generation.tsscript6.6 KB
templates/transform-via-url.tsscript6.5 KB
templates/transform-via-workers.tsscript8.7 KB
templates/upload-api-basic.tsscript3.8 KB
templates/upload-via-url.tsscript4.9 KB
templates/variants-management.tsscript7.0 KB
templates/webhook-handler.tsscript8.8 KB
templates/wrangler-images-binding.jsoncother1.2 KB

references/direct-upload-complete-workflow.md

View on GitHub
# Direct Creator Upload - Complete Workflow

Complete architecture and implementation guide for user-uploaded images.

---

## Architecture Overview

```
┌─────────┐                    ┌─────────┐                    ┌──────────┐
│ Browser │                    │ Backend │                    │Cloudflare│
│ (User)  │                    │  API    │                    │  Images  │
└────┬────┘                    └────┬────┘                    └─────┬────┘
     │                              │                               │
     │ 1. Request upload URL        │                               │
     ├─────────────────────────────>│                               │
     │ POST /api/upload-url          │                               │
     │ { userId: "123" }             │                               │
     │                              │                               │
     │                              │ 2. Generate upload URL        │
     │                              ├──────────────────────────────>│
     │                              │ POST /direct_upload           │
     │                              │ { requireSignedURLs, metadata }│
     │                              │                               │
     │                              │ 3. Return uploadURL + ID      │
     │                              │<──────────────────────────────┤
     │                              │ { uploadURL, id }             │
     │                              │                               │
     │ 4. Return uploadURL          │                               │
     │<─────────────────────────────┤                               │
     │ { uploadURL, imageId }       │                               │
     │                              │                               │
     │ 5. Upload file directly      │                               │
     ├──────────────────────────────────────────────────────────────>│
     │ POST uploadURL                │                               │
     │ FormData: { file }            │                               │
     │                              │                               │
     │ 6. Success response          │                               │
     │<──────────────────────────────────────────────────────────────┤
     │ { success: true }            │                               │
     │                              │                               │
     │ 7. (Optional) Webhook        │                               │
     │                              │<──────────────────────────────┤
     │                              │ POST /webhook                 │
     │                              │ { imageId, status }           │
     │                              │                               │
```

---

## Implementation Steps

### Step 1: Backend - Generate Upload URL

**Endpoint**: `POST /api/upload-url`

```typescript
// backend.ts
interface Env {
  IMAGES_ACCOUNT_ID: string;
  IMAGES_API_TOKEN: string;
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    // Parse request
    const body = await request.json<{
      userId?: string;
      requireSignedURLs?: boolean;
    }>();

    // Generate one-time upload URL
    const response = await fetch(
      `https://api.cloudflare.com/client/v4/accounts/${env.IMAGES_ACCOUNT_ID}/images/v2/direct_upload`,
      {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${env.IMAGES_API_TOKEN}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          requireSignedURLs: body.requireSignedURLs ?? false,
          metadata: {
            userId: body.userId || 'anonymous',
            uploadedAt: new Date().toISOString()
          },
          expiry: new Date(Date.now() + 60 * 60 * 1000).toISOString() // 1 hour
        })
      }
    );

    const result = await response.json();

    return Response.json({
      uploadURL: result.result?.uploadURL,
      imageId: result.result?.id
    });
  }
};
```

### Step 2: Frontend - Request Upload URL

```javascript
// frontend.js
async function requestUploadURL() {
  const response = await fetch('/api/upload-url', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      userId: getCurrentUserId(),
      requireSignedURLs: false
    })
  });

  const { uploadURL, imageId } = await response.json();
  return { uploadURL, imageId };
}
```

### Step 3: Frontend - Upload to Cloudflare

```javascript
async function uploadImage(file) {
  // Step 1: Get upload URL
  const { uploadURL, imageId } = await requestUploadURL();

  // Step 2: Upload directly to Cloudflare
  const formData = new FormData();
  formData.append('file', file); // MUST be named 'file'

  const response = await fetch(uploadURL, {
    method: 'POST',
    body: formData // NO Content-Type header - browser sets multipart/form-data
  });

  if (!response.ok) {
    throw new Error(`Upload failed: ${response.statusText}`);
  }

  return imageId;
}
```

---

## Frontend HTML Example

```html
<form id="upload-form">
  <input type="file" id="file-input" accept="image/*" />
  <button type="submit">Upload</button>
  <div id="status"></div>
</form>

<script>
document.getElementById('upload-form').addEventListener('submit', async (e) => {
  e.preventDefault();

  const fileInput = document.getElementById('file-input');
  const status = document.getElementById('status');
  const file = fileInput.files[0];

  if (!file) {
    status.textContent = 'Please select a file';
    return;
  }

  try {
    status.textContent = 'Requesting upload URL...';

    // Get upload URL from backend
    const urlResponse = await fetch('/api/upload-url', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ userId: 'user-123' })
    });

    const { uploadURL, imageId } = await urlResponse.json();

    status.textContent = 'Uploading...';

    // Upload directly to Cloudflare
    const formData = new FormData();
    formData.append('file', file);

    const uploadResponse = await fetch(uploadURL, {
      method: 'POST',
      body: formData
    });

    if (uploadResponse.ok) {
      status.textContent = `✓ Upload successful! Image ID: ${imageId}`;
    } else {
      throw new Error('Upload failed');
    }

  } catch (error) {
    status.textContent = `✗ Error: ${error.message}`;
  }
});
</script>
```

---

## Webhook Integration

### Configure Webhook

1. Dashboard → Notifications → Destinations → Webhooks → Create
2. Enter webhook URL: `https://your-backend.com/webhook`
3. Notifications → All Notifications → Add → Images → Select webhook

### Handle Webhook

```typescript
// backend-webhook.ts
export default {
  async fetch(request: Request): Promise<Response> {
    const webhook = await request.json();

    console.log('Image upload webhook:', webhook);
    // {
    //   imageId: "abc123",
    //   status: "uploaded",
    //   metadata: { userId: "user-123" }
    // }

    // Update database
    await db.images.create({
      id: webhook.imageId,
      userId: webhook.metadata.userId,
      status: webhook.status,
      uploadedAt: new Date()
    });

    return Response.json({ received: true });
  }
};
```

---

## Draft vs Uploaded State

When you generate upload URL, image record is created in **draft** state.

**Check status**:
```typescript
const response = await fetch(
  `https://api.cloudflare.com/client/v4/accounts/${accountId}/images/v1/${imageId}`,
  {
    headers: { 'Authorization': `Bearer ${apiToken}` }
  }
);

const result = await response.json();

if (result.result?.draft) {
  console.log('Upload not completed yet');
} else {
  console.log('Upload complete, image available');
}
```

---

## Error Handling

### Backend Errors

```typescript
try {
  const response = await fetch(directUploadURL, { ... });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(`Cloudflare error: ${error.errors?.[0]?.message}`);
  }

  return result;

} catch (error) {
  console.error('Failed to generate upload URL:', error);
  return Response.json(
    { error: 'Internal server error' },
    { status: 500 }
  );
}
```

### Frontend Errors

```javascript
// File size validation
const MAX_SIZE = 10 * 1024 * 1024; // 10MB
if (file.size > MAX_SIZE) {
  throw new Error('File too large (max 10MB)');
}

// File type validation
if (!file.type.startsWith('image/')) {
  throw new Error('Please select an image file');
}

// Upload timeout
const timeout = setTimeout(() => {
  throw new Error('Upload timeout (30s limit)');
}, 28000); // 28s (before Cloudflare's 30s timeout)

try {
  await fetch(uploadURL, { body: formData });
  clearTimeout(timeout);
} catch (error) {
  clearTimeout(timeout);
  throw error;
}
```

---

## Custom ID Support

```typescript
// Generate upload URL with custom ID
const response = await fetch(directUploadURL, {
  method: 'POST',
  headers: { ... },
  body: JSON.stringify({
    id: `user-${userId}-profile`, // Custom ID
    metadata: { userId }
  })
});

// Access with custom ID
const imageURL = `https://imagedelivery.net/${accountHash}/user-${userId}-profile/public`;
```

**Note**: Custom IDs cannot be used with `requireSignedURLs=true`.

---

## Expiry Configuration

```typescript
// Default: 30 minutes
// Min: 2 minutes
// Max: 6 hours

const expiry = new Date(Date.now() + 6 * 60 * 60 * 1000); // 6 hours

const response = await fetch(directUploadURL, {
  method: 'POST',
  body: JSON.stringify({
    expiry: expiry.toISOString()
  })
});
```

---

## Security Best Practices

1. **Never expose API token to browser**: Backend-only
2. **Validate file type and size**: Frontend and backend
3. **Rate limit upload URL generation**: Prevent abuse
4. **Associate uploads with users**: Track in metadata
5. **Implement webhooks**: Verify successful uploads
6. **Set reasonable expiry**: 30min-1hr for most cases
7. **Use signed URLs for private content**: `requireSignedURLs=true`

---

## Testing

```bash
# Test backend endpoint
curl -X POST http://localhost:8787/api/upload-url \
  -H "Content-Type: application/json" \
  -d '{"userId":"test-user"}'

# Test upload (replace UPLOAD_URL with response)
curl -X POST "UPLOAD_URL" \
  -F "file=@./test-image.jpg"
```

---

## Official Documentation

- **Direct Creator Upload**: https://developers.cloudflare.com/images/upload-images/direct-creator-upload/
- **Configure Webhooks**: https://developers.cloudflare.com/images/manage-images/configure-webhooks/
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall