メインコンテンツへスキップ

Documentation Index

Fetch the complete documentation index at: https://docs.xhuoapi.ai/llms.txt

Use this file to discover all available pages before exploring further.

OpenAI Images Generations API は現在、クラシックな dall-e-3、文字レンダリング能力がより高い gpt-image-1、最新世代の gpt-image-2、および同一インターフェースで利用可能な nano-banana / nano-banana-2 / nano-banana-pro シリーズモデルをサポートしています。これらはすべてテキストの説明に基づいて高品質な画像を生成できます。 本ドキュメントでは、OpenAI Images Generations API の操作手順を主に紹介し、これを利用して OpenAI シリーズの画像生成機能を簡単に使う方法を解説します。

申請手順

OpenAI Images Generations API を使用するには、まず OpenAI Images Generations API ページで「Acquire」ボタンをクリックして、リクエストに必要な認証情報を取得してください: ログインまたは登録していない場合は、自動的にログインページにリダイレクトされ、登録およびログイン後に元のページに戻ります。 初回申請時には無料クレジットが付与され、この API を無料で利用可能です。

GPT-Image-2 モデル

gpt-image-2 は OpenAI がリリースした新世代の画像生成モデルで、dall-e-3gpt-image-1 と比較して以下の点で大幅に向上しています:
  • 指示遵守能力が強化:複雑な構図、数の指定、位置関係などの構造化指示を正確に理解可能。
  • 文字レンダリングがより鮮明:ポスター、メニュー、インフォグラフィック、ロゴなどのシーンで英数字の乱れがほぼ発生しません。
  • スタイル表現が豊富:映画風ポートレート、レトロポスター、児童向けイラスト、商品写真、インフォグラフィックなど多様なスタイルをネイティブサポート。
  • ネイティブで多比率+高解像度対応:5種類の比率(1:1、4:3、3:4、16:9、9:16)に対し、3段階の解像度(1K / 2K / 4K)をカバー。
呼び出し方法は他のモデルと完全に同じで、model フィールドに gpt-image-2 を指定するだけです。返却される urlplatform.cdn.xhuoapi.ai に永久ホスティングされた画像リンクで、ブラウザで直接開くかウェブページに埋め込むことができます。

対応する size の値と課金レベル

gpt-image-2size の形式のみ検証し、auto または空文字列でなければ WIDTHxHEIGHT(例:1024x10242048x1152800x600)の形式である必要があります。それ以外の形式は 400 エラーを返します。課金は以下の2段階です:
  • 1K 標準価格:下表のいずれかの1K推奨サイズ、または上流で一般的な1K出力の別名(1254x12541672x941941x1672)に該当する場合。これらは上流で1K帯の実際の返却サイズで、受け取って再度 size に指定しても価格は変わりません。
  • その他のサイズ(1.5倍):上記1K集合に含まれないサイズ全て。下表の2K / 4K推奨サイズや任意のカスタム WIDTHxHEIGHT も含みます。
上流のカスタムサイズに対する厳格な制約は、幅・高さともに16の倍数、長辺 ≤ 3840、総ピクセル数 ≤ 8,294,400 です。範囲外は上流で拒否され 4xx エラーとなります。
比率1K(標準価格)2K推奨(×1.5)4K推奨(×1.5)
1:11024x10242048x20482880x2880
4:31536x10242048x15363264x2448
3:41024x15361536x20482448x3264
16:91792x10242048x11523840x2160
9:161024x17921152x20482160x3840
size: "auto" を指定するか、size フィールドを省略すると、モデルがデフォルトサイズを自動選択し、1K標準価格で課金されます。 1K帯では上流の出力が厳密なピクセル一致を保証しません。例えば 1024x1024 を指定しても 1254x1254 が返ることがありますが、比率は維持されます。返却されたサイズを再度 size に指定しても1K価格です。 4Kの単一呼び出しは通常4~8分かかるため、後述の callback_url を用いた非同期コールバックの利用を推奨します。
n パラメータについて gpt-image-2 は現時点で n > 1 をサポートしていません。このパラメータは無視され、n=1 でも n=10 でも単一画像のみ返却され、課金も1枚分のみです。複数候補画像が必要な場合は、複数回のリクエストを並行して送信してください(異なる prompt または seed を指定することを推奨します。そうしないと類似画像が生成される可能性があります)。この制限は gpt-image-1 / gpt-image-1.5、および nano-banana / nano-banana-2 / nano-banana-pro シリーズにも適用されます。dall-e-2 は唯一 n > 1 をネイティブサポートするモデルで、dall-e-3n=1 のみ対応です。
以下に、様々な実例を通じて gpt-image-2 の性能を直感的にご紹介します。

シーン1:映画風ポートレート

プロンプトに映画用語(35mmフィルム、浅い被写界深度、ネオン光など)を使い、雰囲気や質感を精密にコントロール可能です。 Python サンプルコード:
import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "gpt-image-2",
    "prompt": "A cinematic portrait of a young woman standing in a convenience store at night, illuminated by soft pink and cyan neon signs through the window. Shot on 35mm film, shallow depth of field, slight grain, melancholic mood.",
    "size": "1024x1536"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
返却例:
{
  "success": true,
  "task_id": "ab58a5df-6f46-4874-bff6-93169e2849a3",
  "created": 1777048800,
  "data": [
    {
      "revised_prompt": "A cinematic portrait of a young woman standing in a convenience store at night, illuminated by soft pink and cyan neon signs through the window. Shot on 35mm film, shallow depth of field, slight grain, melancholic mood.",
      "url": "https://platform.cdn.xhuoapi.ai/gpt-image/ab58a5df-6f46-4874-bff6-93169e2849a3_0.png"
    }
  ]
}
生成画像例:

シーン2:レトロ旅行ポスター(文字レンダリング付き)

gpt-image-2 はレイアウトやフォントレンダリングが安定しており、ポスター、メニュー、カードなど文字入りデザインに最適です。
payload = {
    "model": "gpt-image-2",
    "prompt": "A vintage travel poster of the Amalfi Coast, Italy. Stylized art-deco illustration of cliffside lemon-yellow houses cascading down to a turquoise sea, with a small white sailboat in the harbor. Bold typography at the top reads AMALFI and at the bottom ITALIA 1958. Limited color palette: cream, sea-blue, lemon yellow, terracotta. Slight paper-grain texture.",
    "size": "1024x1536"
}
返却された url の画像:

Art Deco ポスターの視覚スタイルを正確に再現し、タイトル文字 AMALFIITALIA 1958 が鮮明かつ正確にレンダリングされています。

シーン3:複雑な構図と数の指定

以下のプロンプトは「数量」と「位置」などの構造化指示の遵守能力をテストします。
payload = {
    "model": "gpt-image-2",
    "prompt": "A wooden bookshelf consisting of three shelves: On the top shelf, there should be one book. On the second shelf, there should be three books. On the bottom shelf, there should be seven books. Soft warm lighting, photorealistic, cozy library atmosphere.",
    "size": "1024x1024"
}
生成画像例:

3段の本棚にそれぞれ1冊、3冊、7冊の本が正確に配置されており、dall-e-3 時代には安定して実現が難しかった成果です。

シーン4:イラスト風(横長)

アートメディアやムードキーワードを指定して、スタイライズされたイラストを生成可能です。
payload = {
    "model": "gpt-image-2",
    "prompt": "A soft, poetic children's book illustration of a small fox reading a book under a glowing mushroom in a moonlit forest. Watercolor and pencil texture, gentle pastel colors, dreamy atmosphere, hand-drawn feel.",
    "size": "1536x1024"
}
生成された横長イラスト:

非同期およびコールバック

gpt-image-2 の単一呼び出しは通常60~90秒かかります。長時間の接続を維持したくない場合は、後述の callback_url による非同期コールバック機構を利用可能で、呼び出し手順は他モデルと同一です。

Nano Banana シリーズモデル

nano-banana シリーズは Gemini ベースの画像生成モデルで、同じ /openai/images/generations インターフェースから利用可能です。エンドポイントを切り替える必要はなく、model を下表のいずれかに変更するだけで使えます。
モデル課金(クレジット / 回)適用シーン
nano-banana0.14一般的な画像生成、最速かつ最安価
nano-banana-20.28品質とディテールが明確に向上
nano-banana-pro0.35シリーズのフラッグシップ、構図・ディテール・文字表現が最良
重要:対応パラメータ範囲 Nano Banana は適合レイヤーを介して OpenAI プロトコルに接続しており、gpt-image-* と比べて以下のパラメータのみサポートします:modelpromptsize
  • size は下表のように内部の aspect_ratio にマッピングされ、未記載サイズは 1:1 にフォールバックします:
    • 1024x1024 / 512x512 / 256x2561:1
    • 1792x102416:9
    • 1024x17929:16
  • nqualitystyleresponse_formatbackgroundoutput_format などのパラメータは非対応で、指定しても無視されます。
  • 返却構造は OpenAI 形式(data[].url)に準拠しますが、created は常に 0b64_json は返さず、revised_prompt は常に元の prompt と同一です。

基本呼び出し例

import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "nano-banana",
    "prompt": "a small red apple on a white table, photoreal",
    "size": "1024x1024"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
返却例:
{
  "created": 0,
  "data": [
    {
      "url": "https://platform.cdn.xhuoapi.ai/nanobanana/6870b330-65c4-436c-bb80-819fdae7a7a4.png",
      "revised_prompt": "a small red apple on a white table, photoreal"
    }
  ]
}
生成画像は返却された url から直接アクセス可能です:

フラッグシップモデル nano-banana-pro へのアップグレード

modelnano-banana-pro に変更するだけで、他のパラメータは同じまま利用可能です:
payload = {
    "model": "nano-banana-pro",
    "prompt": "abstract painting",
    "size": "1024x1024"
}
返却例:
{
  "created": 0,
  "data": [
    {
      "url": "https://platform.cdn.xhuoapi.ai/nanobanana/6227fcc9-3442-4aa3-a76c-4a4441a99649.png",
      "revised_prompt": "abstract painting"
    }
  ]
}

非同期コールバック

callback_url による非同期コールバックは nano-banana シリーズでも有効で、呼び出し手順は他モデルと同様です。詳細は後述の 非同期コールバック 節をご参照ください。

基本的な使用方法

インターフェース上で必要な内容を入力します。例:

初回利用時は少なくとも3つの項目を入力します。1つは authorization で、ドロップダウンリストから選択可能です。もう1つは model で、OpenAI DALL-E の公式モデルカテゴリから選択します。ここでは主に1種類のモデルを利用可能で、詳細は提供されたモデル情報を参照してください。最後は prompt で、生成したい画像の説明文を入力します。 右側には対応する呼び出しコードが自動生成され、コピーして実行したり、「Try」ボタンでテスト可能です。

Python サンプルコード:
import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
呼び出し後の返却例:
{
  "created": 1721626477,
  "data": [
    {
      "revised_prompt": "A delightful image showcasing a young sea otter, who is born brown, with wide charming eyes. It is delightfully lying on its back, paddling in the calm sea waters. Its dense, velvety fur appears wet and shimmering, capturing the essence of its habitat. The small creature curiously plays with a sea shell with its small paws, looking absolutely innocent and charming in its natural environment.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/5d98aa7c-80c6-4523-b571-fc606ad455b9/generated_00.png?se=2024-07-23T05%3A34%3A48Z&sig=GAz%2Bi3%2BkHOQwAMhxcv22tBM%2FaexrxPgT9V0DbNrL4ik%3D&ske=2024-07-23T08%3A41%3A10Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T08%3A41%3A10Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}
返却結果の主なフィールド:
  • created:今回の画像生成タスクのID。タスクを一意に識別します。
  • data:画像生成結果の情報を含みます。
data は生成された画像の詳細情報を含み、その中の url は生成画像の詳細リンクです。以下のように表示されます。

画像品質パラメータ quality

画像生成結果の詳細パラメータの設定方法を紹介します。画像品質パラメータ quality は2種類あります。1つは standard で標準的な画像を生成し、もう1つは hd でより細かいディテールと高い一貫性を持つ画像を生成します。 以下は qualitystandard に設定した例です:

右側に対応する呼び出しコードが生成され、コピーして実行したり「Try」ボタンでテスト可能です。

Python サンプルコード:
import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "quality": "standard"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
返却例:
{
  "created": 1721636023,
  "data": [
    {
      "revised_prompt": "A cute baby sea otter is lying playfully on its back in the water, with its fur looking glossy and soft. One of its tiny paws is reaching out curiously, and it has an expression of pure joy and warmth on its face as it looks up to the sky. Its body is surrounded by bubbles from its playful twirling in the water. A gentle breeze is playing with its fur making it look more charming. The scene portrays the tranquility and charm of marine life.",
      "url": "https://cdn.xhuoapi.ai/j5v15b.png"
    }
  ]
}
qualitystandard の画像例:

同様の操作で qualityhd に設定すると、以下のようなより細密な画像が得られます:

hdstandard よりも細かいディテールと高い一貫性を持つ画像を生成します。

画像サイズパラメータ size

生成画像のサイズも設定可能です。以下は 1024 * 1024 に設定した例です:

右側に対応する呼び出しコードが生成され、コピーして実行したり「Try」ボタンでテスト可能です。

Python サンプルコード:
import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "size": "1024x1024"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
返却例:
{
  "created": 1721636652,
  "data": [
    {
      "revised_prompt": "A delightful depiction of a baby sea otter. The small mammal is captured in its natural habitat in the ocean, floating on its back. It has thick brown fur that is sleek and wet from the sea water. Its eyes are closed as if it is enjoying a moment of deep relaxation. The water around it is calm, reflecting the peacefulness of the scene. The background should hint at a diverse marine ecosystem, with visible strands of kelp floating on the surface, suggesting the baby otter's preferred environment.",
      "url": "https://cdn.xhuoapi.ai/o4pvvx.png"
    }
  ]
}
1024 * 1024 サイズの生成画像例:

同様に 1792 * 1024 サイズに設定すると、以下のような画像が得られます: 画像サイズが明確に異なることがわかります。さらに多様なサイズ設定も可能で、詳細は公式ドキュメントをご参照ください。

画像スタイルパラメータ style

画像スタイルパラメータ style は2種類あります。1つは vivid でより鮮やかな画像を生成し、もう1つは natural でより自然な画像を生成します。 以下は stylevivid に設定した例です:

右側に対応する呼び出しコードが生成され、コピーして実行したり「Try」ボタンでテスト可能です。

Python サンプルコード:
import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "style": "vivid"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
返却例:
{
  "created": 1721637086,
  "data": [
    {
      "revised_prompt": "A baby sea otter with soft, shiny fur and sparkling eyes floating playfully on calm ocean waters. This adorable creature is trippingly frolicking amidst small, gentle waves under a bright, clear, sunny sky. The tranquility of the sea contrasts subtly with the delightful energy of this young otter. The critter gamely clings to a tiny piece of driftwood, its small paws adorably enveloping the floating object.",
      "url": "https://cdn.xhuoapi.ai/e0rpc3.png"
    }
  ]
}
vivid スタイルの生成画像例:

同様に stylenatural に設定すると、以下のような画像が得られます:

vividnatural よりもより生き生きとしたリアルな画像を生成します。

画像リンク形式パラメータ response_format

最後に画像リンクの形式パラメータ response_format は2種類あります。1つは b64_json で画像リンクを Base64 エンコードし、もう1つは url で通常の画像リンクを返します。 以下は response_formaturl に設定した例です:

右側に対応する呼び出しコードが生成され、コピーして実行したり「Try」ボタンでテスト可能です。

Python サンプルコード:
import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "response_format": "url"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
返却例:
{
  "created": 1721637575,
  "data": [
    {
      "revised_prompt": "A charming depiction of a baby sea otter. The otter is seen resting serenely on its back amidst the gentle, blue ocean waves. The baby otter's fur is an endearing mix of soft greyish brown shades, glinting subtly in the muted sunlight. Its small paws are touching, lifted slightly towards the sky as if playing with an unseen object. Its round, expressive eyes are wide in curiosity, sparking with life and innocence. Use a realistic style to evoke the otter's natural habitat and its adorably fluffy exterior.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/87792c5f-8b6d-412e-81dd-f1a1baa19bd2/generated_00.png?se=2024-07-23T08%3A39%3A47Z&sig=zzRAn30TqIKHdLVqZPUUuSJdjCYpoJdaGU6BeoA76Jo%3D&ske=2024-07-23T13%3A32%3A13Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T13%3A32%3A13Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}
response_formaturl の場合の画像リンクは 画像 URL で直接アクセス可能です。画像例:

同様に response_formatb64_json に設定すると、Base64 エンコードされた画像リンクが返却されます。例:
{
  "created": 1721638071,
  "data": [
    {
      "b64_json": "iVBORw0..............v//AQEAAP4AAAD+AAADAQAAAwEEA/4D//8Q/Pbw64mKbVTFoQAAAABJRU5ErkJggg==",
      "revised_prompt": "A charming image of a young baby sea otter. The otter is gently floating on a calm blue sea, basking in the warm, golden rays of sunlight streaming down from a clear sky above. The otter's fur is a rich chocolate brown, and it looks incredibly soft and fluffy. The otter's eyes are bright and expressive, filled with childlike curiosity and joy. It has small, pricked ears and a button-like nose which adds to its overall cuteness. In the sea around it, twinkling droplets of water can be seen, pepped up by the sunlight, the sight is certainly a delightful one."
    }
  ]
}

非同期コールバック

OpenAI Images Generations API は画像生成に時間がかかる場合があり、API が長時間応答しないと HTTP リクエストが接続を維持し続け、システムリソースを消費します。そのため、本 API は非同期コールバックもサポートしています。 全体の流れは、クライアントがリクエスト時に callback_url フィールドを指定し、API は即座に task_id を含む結果を返します。タスク完了後、生成結果が POST JSON 形式で指定された callback_url に送信され、task_id によりタスク結果を紐づけられます。 以下に具体的な操作例を示します。 まず、Webhook コールバックは HTTP リクエストを受け取るサービスで、開発者は自身で構築した HTTP サーバーの URL を指定してください。ここではデモ用に公開 Webhook サイト https://webhook.site/ を使用します。サイトを開くと Webhook URL が取得できます: この URL をコピーし、Webhook として利用します。例:https://webhook.site/3d32690d-6780-4187-a65c-870061e8c8ab 次に、callback_url フィールドに上記 URL を指定し、その他パラメータを設定します。例:
import requests

url = "https://api.xhuoapi.ai/v1/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "callback_url": "https://webhook.site/3d32690d-6780-4187-a65c-870061e8c8ab"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
実行すると即座に以下のような結果が得られます:
{
  "task_id": "6a97bf49-df50-4129-9e46-119aa9fca73c"
}
しばらく待つと、Webhook URL で生成画像の結果が確認できます。内容例:
{
  "success": true,
  "task_id": "6a97bf49-df50-4129-9e46-119aa9fca73c",
  "trace_id": "9b4b1ff3-90f2-470f-b082-1061ec2948cc",
  "data": {
    "created": 1721626477,
    "data": [
      {
        "revised_prompt": "A delightful image showcasing a young sea otter...",
        "url": "https://dalleprodsec.blob.core.windows.net/private/images/..."
      }
    ]
  }
}
結果には task_id が含まれ、data は同期呼び出しと同様の画像生成結果を含みます。task_id によりタスクの関連付けが可能です。

エラー処理

API 呼び出し時にエラーが発生した場合、API は対応するエラーコードとメッセージを返します。例:
  • 400 token_mismatched:不正なリクエスト、パラメータ不足または無効の可能性。
  • 400 api_not_implemented:不正なリクエスト、パラメータ不足または無効の可能性。
  • 401 invalid_token:認証エラー、トークンが無効または欠落。
  • 429 too_many_requests:リクエスト過多、レート制限超過。
  • 500 api_error:サーバ内部エラー。

エラー応答例

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

結論

本ドキュメントを通じて、OpenAI Images Generations API を利用して公式 OpenAI DALL-E の画像生成機能を簡単に使う方法を理解いただけました。API の連携や利用に役立てていただければ幸いです。ご不明点があれば、いつでも技術サポートチームまでお問い合わせください。