Headless API Reference
If you prefer to build your own custom drag-and-drop UI, you can use the headless API Client provided by imgapi to securely interface with the edge servers.
The 3-Phase Protocol
To ensure security and prevent orphaned files, imgapi uses a strict 3-phase upload protocol:
Using the Client Library
In your main application, import the uploadToExtUtil function. You can copy the src/lib/client/media-client.ts file directly into your own project.
import { uploadToExtUtil, commitExtUtilAsset } from '$lib/client/imgapi';
async function handleImageDrop(file: File) {
// Phase 1 & 2: Token Request and Edge Stream (handled internally by uploadToExtUtil)
const result = await uploadToExtUtil({
file: file,
userId: 123,
assetType: 'article_inline',
projectId: 'my_project',
apiBaseUrl: 'https://imgapi.avadhya.in'
});
if (result.error) {
alert("Upload failed: " + result.error);
return;
}
const { object_key, cdn_url } = result;
console.log("Uploaded! URL:", cdn_url);
// Phase 3: Commit the asset to prevent orphan cleanup
await commitExtUtilAsset(object_key, 'articles', 'article_id_1', 123, 'https://imgapi.avadhya.in');
// Proceed with saving the cdn_url to your database
} REST API Details
The client library wraps these two core endpoints. Use this reference if you are building your own client from scratch in another language (like Python or Swift).
| Endpoint | Method | Purpose |
|---|---|---|
| /api/v1/media/upload-token | POST | Requests a short-lived, signed token for an upload. |
| /upload/[...key] | PUT | Streams the raw binary directly into Cloudflare R2 using the signed token. |
On-The-Fly Image Transformations
If you're retrieving images, you can apply native Cloudflare Edge resizing simply by passing query parameters to the URL. This allows you to dynamically resize, crop, and optimize images without storing multiple copies.
https://imgapi.avadhya.in/upload/my_asset.jpg?w=300&h=300&blur=50&q=80| Parameter | Effect | Example |
|---|---|---|
| w | Sets the maximum width in pixels. | w=800 |
| h | Sets the maximum height in pixels. | h=600 |
| q | Sets the JPEG/WebP compression quality (1-100). | q=75 |
| blur | Applies a gaussian blur (useful for placeholders). | blur=20 |
Webhook Integration
You can configure a Webhook URL in your Project Settings via the Dashboard. When an upload is successfully committed, imgapi will instantly fire an asynchronous POST request to your URL.
Payload Schema
| Field | Type | Description |
|---|---|---|
| event | string | Always "upload.committed" for successful uploads. |
| object_key | string | The raw internal storage path (e.g., uploads/123/my_blog_v1/image.png). |
| cdn_url | string | The public, resolvable URL of the image. Save this in your database! |
| committed_at | string (ISO) | Timestamp of when the upload was finalized. |
// Webhook Payload Example
{
"event": "upload.committed",
"object_key": "uploads/123/my_blog_v1/image.png",
"cdn_url": "https://cdn.example.com/uploads/123/my_blog_v1/image.png",
"committed_at": "2023-10-01T12:00:00Z"
}