跳轉到主要內容

請求本文

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 timestamp。
data
array
產生圖片的陣列。每個物件包含:
  • url(string):產生圖片的 URL
  • b64_json(string):Base64 編碼的圖片(若有要求)
  • revised_prompt(string):實際使用的 prompt(DALL-E 3)

非同步回應(Midjourney、Flux Pro、Ideogram 等)

某些模型需要較長的處理時間,並會返回非同步回應:
created
integer
建立時間的 Unix timestamp。
task_id
string
用於輪詢的唯一任務識別碼。
status
string
初始狀態:pending
poll_url
string
用於輪詢結果的相對 URL(例如:/v1/tasks/{id})。
data
array
包含預留資料的陣列。在完成之前,url 會是空的。
當你收到 status: "pending" 時,請使用 Get Image Status endpoint 輪詢結果。
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..."
    }
  ]
}

可用模型

ModelTypeFeatures
dall-e-3同步最佳品質、prompt 強化
dall-e-2同步更快、更實惠
flux-pro非同步擬真風格、高品質
flux-schnell同步非常快速
midjourney非同步藝術風格
ideogram-v3非同步最佳文字渲染
stable-diffusion-3同步開源、可自訂
非同步模型 會返回 status: "pending",並需要進行輪詢。請參閱 Get Image Status 以了解如何取得結果。

處理非同步回應

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

def generate_image(prompt, model="midjourney"):
    # Create image request
    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()

    # Check if async
    if data.get("status") == "pending":
        task_id = data["task_id"]
        poll_url = data.get("poll_url")
        print(f"Async task started: {task_id}")

        # Poll for result
        while True:
            status_resp = requests.get(
                f"https://api.lemondata.cc{poll_url}" if poll_url else f"https://api.lemondata.cc/v1/tasks/{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", "Generation failed"))

            time.sleep(3)
    else:
        # Sync response
        return data["data"][0]["url"]

# Usage
url = generate_image("a beautiful sunset over mountains", model="midjourney")
print(f"Generated image: {url}")