跳轉到主要內容
POST
/
v1
/
images
/
generations
curl -X POST "https://api.lemondata.cc/v1/images/generations" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "dall-e-3",
    "prompt": "A white cat sitting on a windowsill watching rain",
    "size": "1024x1024",
    "quality": "standard",
    "n": 1
  }'
{
  "created": 1706000000,
  "data": [
    {
      "url": "https://...",
      "revised_prompt": "A fluffy white cat with bright eyes sitting peacefully on a wooden windowsill, watching raindrops stream down the glass window..."
    }
  ]
}

請求主體

model
string
預設值:"dall-e-3"
使用的模型(例如 dall-e-3flux-promidjourney)。
prompt
string
必填
描述所需圖像的文字。
n
integer
預設值:"1"
要生成的圖像數量(1-4,取決於模型)。
size
string
預設值:"1024x1024"
圖像尺寸。選項因模型而異:
  • DALL-E 3: 1024x10241792x10241024x1792
  • 其他模型: 512x5121024x1024
quality
string
預設值:"standard"
圖像品質(standardhd)。僅限 DALL-E 3。
response_format
string
預設值:"url"
回應格式:urlb64_json
style
string
預設值:"vivid"
DALL-E 3 的風格:vividnatural
user
string
終端使用者的唯一識別碼。

回應

同步回應(DALL-E、Flux Schnell 等)

created
integer
建立時間的 Unix 時間戳。
data
array
生成的圖像陣列。每個物件包含:
  • url (string): 生成圖像的 URL
  • b64_json (string): Base64 編碼的圖像(如果請求)
  • revised_prompt (string): 使用的提示詞(DALL-E 3)

異步回應(Midjourney、Flux Pro、Ideogram 等)

某些模型需要更長的處理時間,會回傳異步回應:
created
integer
建立時間的 Unix 時間戳。
task_id
string
用於輪詢的唯一任務識別碼。
status
string
初始狀態:pending
poll_url
string
用於輪詢結果的相對 URL(例如 /v1/images/generations/{task_id})。
data
array
包含佔位資料的陣列。url 在完成前為空。
當收到 status: "pending" 時,使用取得圖像狀態端點輪詢結果。
curl -X POST "https://api.lemondata.cc/v1/images/generations" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "dall-e-3",
    "prompt": "A white cat sitting on a windowsill watching rain",
    "size": "1024x1024",
    "quality": "standard",
    "n": 1
  }'
{
  "created": 1706000000,
  "data": [
    {
      "url": "https://...",
      "revised_prompt": "A fluffy white cat with bright eyes sitting peacefully on a wooden windowsill, watching raindrops stream down the glass window..."
    }
  ]
}

可用模型

模型類型特點
dall-e-3同步最佳品質,提示詞增強
dall-e-2同步更快,更實惠
flux-pro異步照片級真實感,高品質
flux-schnell同步非常快
midjourney異步藝術風格
ideogram-v3異步最佳文字渲染
stable-diffusion-3同步開源,可自訂
異步模型回傳 status: "pending",需要輪詢。請參閱取得圖像狀態了解如何取得結果。

處理異步回應

對於異步模型,檢查回應是否包含 status: "pending"
import requests
import time

def generate_image(prompt, model="midjourney"):
    # 建立圖像請求
    response = requests.post(
        "https://api.lemondata.cc/v1/images/generations",
        headers={"Authorization": "Bearer sk-your-api-key"},
        json={"model": model, "prompt": prompt}
    )
    data = response.json()

    # 檢查是否為異步
    if data.get("status") == "pending":
        task_id = data["task_id"]
        print(f"異步任務已啟動: {task_id}")

        # 輪詢結果
        while True:
            status_resp = requests.get(
                f"https://api.lemondata.cc/v1/images/generations/{task_id}",
                headers={"Authorization": "Bearer sk-your-api-key"}
            )
            status_data = status_resp.json()

            if status_data["status"] == "completed":
                return status_data["data"][0]["url"]
            elif status_data["status"] == "failed":
                raise Exception(status_data.get("error", "生成失敗"))

            time.sleep(3)
    else:
        # 同步回應
        return data["data"][0]["url"]

# 使用方式
url = generate_image("a beautiful sunset over mountains", model="midjourney")
print(f"生成的圖像: {url}")

主體

application/json
prompt
string
必填

描述所需圖像的文字。

model
string

使用的模型(例如 dall-e-3flux-promidjourney )。

n
integer

要生成的圖像數量(1-4,取決於模型)。

size
string

圖像尺寸。選項因模型而異: DALL-E 3: 1024x10241792x10241024x1792 其他模型: 512x5121024x1024

quality
string

圖像品質( standardhd )。僅限 DALL-E 3。

response_format
string

回應格式: urlb64_json

style
string

DALL-E 3 的風格: vividnatural

user
string

終端使用者的唯一識別碼。

回應

200 - application/json

Response 200

created
integer

建立時間的 Unix 時間戳。

data
object[]

包含佔位資料的陣列。 url 在完成前為空。

task_id
string

用於輪詢的唯一任務識別碼。

status
string

初始狀態: pending

poll_url
string

用於輪詢結果的相對 URL(例如 /v1/images/generations/{task_id} )。