English | ็ฎไฝไธญๆ | ็น้ซไธญๆ | ๆฅๆฌ่ช | Espaรฑol | Franรงais | ะ ัััะบะธะน | Deutsch
Flux-API-Worker is an AI image generation service deployed on Cloudflare Workers. It utilizes Cloudflare's Flux model to generate images and provides an efficient API interface for handling requests. This service can be easily integrated into various applications, offering users powerful AI image generation capabilities. โจ๐ผ๏ธ๐
In the Worker's settings page, find the "Environment Variables" section and add the following variables:
Variable Name | Description | Type | Example | Default |
---|---|---|---|---|
API_KEY |
API authentication key ๐ | String | "your-complex-api-key-here" |
- |
CF_ACCOUNT_ID |
Cloudflare Account ID ๐ | String | "1a2b3c4d5e6f7g8h9i0j" |
- |
CF_API_TOKEN |
Cloudflare API Token ๐๏ธ | String | "your-cloudflare-api-token" |
- |
PROMPT_OPTIMIZATION |
Enable Prompt Optimization ๐ | String | "true" or "false" |
- |
EXTERNAL_API_BASE |
External API base URL ๐ | String | "https://api.external-service.com" |
- |
EXTERNAL_MODEL |
External optimization model name ๐ค | String | "gpt-3.5-turbo" |
- |
EXTERNAL_API_KEY |
External API access key ๐๏ธ | String | "your-external-api-key" |
- |
FLUX_NUM_STEPS |
Number of steps for Flux model ๐ถ | Integer | "4" |
4 |
IMAGE_EXPIRATION |
Image expiration time in KV (seconds) โณ | Integer | "1800" |
1800 |
Ensure these variables are correctly configured in your Cloudflare Worker's environment variables settings. For variables with default values, you can keep the default if no change is needed. ๐งโ
Note: For security, set a complex string for
API_KEY
. This will be used to validate the legitimacy of API calls. ๐๐ก๏ธ
FLUX_CF_KV
variable. ๐Accessing the Worker's root path (https://<your_worker_name>.<your_subdomain>.workers.dev/
) will display a welcome page confirming the API service is running. โ
๐
Main image generation endpoint:
https://<your_worker_name>.<your_subdomain>.workers.dev/v1/chat/completions
๐จโจ
Get available model information:
https://<your_worker_name>.<your_subdomain>.workers.dev/v1/models
This endpoint returns information about the currently used Flux model. ๐ค๐
Retrieve generated images:
https://<your_worker_name>.<your_subdomain>.workers.dev/image/{image_key}
๐ฅ๐ญ
Send a POST request to the chat completions endpoint in the following format:
{
"messages": [
{
"role": "user",
"content": "A cute cat 3:2"
}
],
"stream": false
}
The request headers must include:
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json
Important: Replace
YOUR_API_KEY
with theAPI_KEY
value you set in the environment variables. ๐๐
If you want to receive a streaming response, set the stream
parameter to true
:
{
"messages": [
{
"role": "user",
"content": "A cute cat 3:2"
}
],
"stream": true
}
Streaming responses will be returned in Server-Sent Events (SSE) format, allowing real-time generation progress updates. โก๐
Flux-API-Worker supports the following preset image sizes and aspect ratios:
To specify a particular size, simply add the corresponding ratio after the prompt, for example:
"A cute cat 16:9"
If no size is specified, the system will default to generating a 1:1 (1024x1024) image. ๐๏ธ๐ง
Flux-API-Worker supports CORS, allowing access to the API from web applications on different domains. This means you can call the API directly from frontend JavaScript applications without encountering cross-origin issues. ๐๐ซ๐ง
Flux-API-Worker can be easily integrated into various applications such as NextWeb, ChatBox, etc. When configuring in these applications:
Note: Flux-API-Worker has removed the context functionality, generating a new unique image with each call. ๐๐ผ๏ธ
Example of a non-streaming response:
{
"id": "chatcmpl-1234567890",
"created": 1677649420,
"model": "@cf/black-forest-labs/flux-1-schnell",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "๐จ Original prompt: A cute cat 3:2\n๐ฌ Prompt generation model: Original Prompt\n๐ Optimized prompt: A cute cat\n๐ Image specifications: 768x512\n๐ Image generation successful!\nHere's the result:\n\n![Generated Image](https://your-worker-url.workers.dev/image/12345)"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 100,
"total_tokens": 120
}
}
PROMPT_OPTIMIZATION
is set to 'true' and the external API is properly configured. ๐๐งYou can further optimize the API's functionality by modifying the Worker code, for example:
I hope this README helps you quickly deploy and use Flux-API-Worker. If you have any questions or need further assistance, please feel free to contact me. ๐๐จโ๐ป๐ฉโ๐ป
If you find this repo helpful, please give it a star. โญโญโญ Thank you!