API Endpoint
- URL:
https://image-gen.droplinked.workers.dev/ - Method:
POST - Content-Type:
application/json
Authentication
An API key is required for access. Contact Droplinked support to obtain your API key. Include it in the request headers as follows:- Header:
api-key - Value:
<YOUR_API_KEY>(provided by Droplinked)
Request Format
The API accepts a JSON payload with the following fields:Request Body
| Field | Type | Required | Description |
|---|---|---|---|
prompt | String | Yes | The main description of the image content (e.g., “a man smoking cigar”). |
style | String | No | The artistic style (e.g., “realistic”, “cartoon”). Optional. |
background | String | No | The background setting (e.g., “party”, “forest”). Optional. |
aspect_ratio | String | Yes | The image aspect ratio (e.g., “1x1”, “16x9”). Supported values listed below. |
Supported Aspect Ratios
| Aspect Ratio | Dimensions (Width x Height) |
|---|---|
1x1 | 1024 x 1024 |
3x4 | 768 x 1024 |
4x3 | 1024 x 768 |
16x9 | 1920 x 1080 |
9x16 | 1080 x 1920 |
Example Request Body
Response Format
- Content-Type:
text/plain - Response: A base64-encoded string representing the generated image, which can be decoded and used as an image file (e.g., PNG).
Example Response
Guidelines for Image Generation
The API constructs the full prompt and generates the image as follows:- Prompt Construction:
- Starts with the
promptvalue. - Appends
style(if provided) as “in a [style] style”. - Appends
background(if provided) as “with a background of [background]”. - Example: “a man smoking cigar in a realistic style with a background of party”.
- Starts with the
- Image Dimensions: Set based on the specified
aspect_ratio(see table above). - Model: Uses the Flux.1 Schnell model (
@cf/black-forest-labs/flux-1-schnell) for generation. - Output: Returns a base64 string, processed in chunks to handle large data efficiently.
JavaScript Example
Below is an example of how to use the Image Generation API in JavaScript with thefetch API, including decoding the base64 response:
Error Handling
The API provides detailed error responses in JSON format:- 405 Method Not Allowed: If the request method isn’t
POST.- Example:
{ "error": "Method not allowed. Use POST." }
- Example:
- 401 Unauthorized: If the API key is missing or invalid.
- Example:
{ "error": "Authentication failed. Invalid API key." }
- Example:
- 400 Bad Request: If the JSON is invalid or required fields (
prompt,aspect_ratio) are missing.- Example:
{ "error": "Missing required fields: prompt or aspect_ratio." }
- Example:
- 400 Invalid Aspect Ratio: If
aspect_ratioisn’t supported.- Example:
{ "error": "Invalid aspect ratio specified.", "received": "2x3" }
- Example:
- 500 Internal Server Error: If image generation or database access fails.
- Example:
{ "error": "Failed to generate image.", "details": "..." }
- Example:
Example Error Output
Best Practices
- Prompt Specificity: Use clear, descriptive prompts for better results (e.g., “a woman in a red dress” vs. “a person”).
- Optional Fields: Include
styleandbackgroundonly when needed to avoid cluttering the prompt. - Aspect Ratio: Choose an aspect ratio that matches your display needs.
- Base64 Handling: Decode the response client-side to render or save the image (e.g., as PNG).
- Error Checking: Handle errors gracefully with try-catch or
.catchblocks.
Worker Code Insights
- Base64 Conversion: The
uint8ArrayToBase64function processes large image data in chunks (8192 bytes) to avoid stack overflow. - Authentication: API keys are validated against a database (
env.key_DB). - Model: Uses Flux.1 Schnell for fast, high-quality image generation.
- Debugging: Responses include a header (
X-Debug-Version) for version tracking.