Nbility logoNbility Docs

Search documentation

Search guides and API reference content

概述

使用图片生成接口根据文本提示词生成图片。

POST /v1/images/generations
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

支持的模型

模型说明
gpt-image-2Image-2 图片生成模型
gpt-image-2-vipImage-2 图片生成模型

请求参数

<ParamTable params={[ { name: "model", type: "string", required: false, default: "gpt-image-2", description: '模型名称,可选值:gpt-image-2gpt-image-2-vip' }, { name: "prompt", type: "string", required: true, description: "所需图像的文本描述,最大长度为 1000 个字符" }, { name: "n", type: "integer", required: false, description: "要生成的图像数,必须介于 1 和 10 之间。gpt-image-2 系列会按上游实际能力返回结果,当前兼容渠道通常每次返回 1 张图片,请以响应中的 data.length 为准" }, { name: "size", type: "string", required: false, description: "生成图像的大小:auto(默认)、1024x1024(square)、1536x1024(landscape)、1024x1536(portrait)、2048x2048(2K square)、2048x1152(2K landscape)、3840x2160(4K landscape)、2160x3840(4K portrait)" }, { name: "quality", type: "string", required: false, description: "图像质量,可选值:auto(默认)、lowmediumhigh" }, { name: "response_format", type: "string", required: true, description: '返回格式,必须是 urlb64_json。未传时按 url 处理' }, ]} />

最大边长必须小于或等于 3840 像素,边长必须是 16px 的倍数,长宽比不得超过 3:1,总像素数必须至少为 655,360 且不超过 8,294,400。

Response format and image count

  • response_format: "url" returns hosted image URLs in data[].url.
  • response_format: "b64_json" returns base64 image data in data[].b64_json.
  • When an upstream provider returns base64 data or a data:image/...;base64,... URL internally, the gateway normalizes the client response according to response_format.
  • Base64 image results may still be cached internally for logs and downloads, but the client response remains b64_json when response_format is set to b64_json.
  • The n parameter is accepted for OpenAI-compatible requests, but the actual number of returned images depends on the upstream provider. For gpt-image-2 compatible channels, requests commonly return one image even when n is greater than 1.

Header parameters

<ParamTable params={[ { name: "X-New-Api-Async-Task", type: "string", required: false, description: "Set to true or 1 to submit image generation as an async task. The API returns a task_id immediately; use async tasks to check status and results. Omit it to keep the default synchronous image response." }, ]} />

请求示例

cURL

curl https://api.nbility.dev/v1/images/generations \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-New-Api-Async-Task: true" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "A panda riding a bicycle with a backpack that says I love China",
    "n": 1,
    "size": "1024x1024",
    "response_format": "url"
  }'

Python

import requests

response = requests.post(
    "https://api.nbility.dev/v1/images/generations",
    headers={
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
        "X-New-Api-Async-Task": "true",
    },
    json={
        "model": "gpt-image-2",
        "prompt": "A panda riding a bicycle with a backpack that says I love China",
        "n": 1,
        "size": "1024x1024",
        "response_format": "url",
    },
)
print(response.json())

Node.js

const response = await fetch('https://api.nbility.dev/v1/images/generations', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json',
    'X-New-Api-Async-Task': 'true',
  },
  body: JSON.stringify({
    model: 'gpt-image-2',
    prompt: 'A panda riding a bicycle with a backpack that says I love China',
    n: 1,
    size: '1024x1024',
    response_format: 'url',
  }),
});

console.log(await response.json());

Go

package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
)

func main() {
	body := []byte(`{
		"model":"gpt-image-2",
		"prompt":"A panda riding a bicycle with a backpack that says I love China",
		"n":1,
		"size":"1024x1024",
		"response_format":"url"
	}`)

	req, _ := http.NewRequest("POST", "https://api.nbility.dev/v1/images/generations", bytes.NewBuffer(body))
	req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-New-Api-Async-Task", "true")
	resp, _ := http.DefaultClient.Do(req)
	defer resp.Body.Close()
	data, _ := io.ReadAll(resp.Body)
	fmt.Println(string(data))
}

Java

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var body = """
            {
              \"model\": \"gpt-image-2\",
              \"prompt\": \"A panda riding a bicycle with a backpack that says I love China\",
              \"n\": 1,
              \"size\": \"1024x1024\",
              \"response_format\": \"url\"
            }
            """;

        var client = HttpClient.newHttpClient();
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.nbility.dev/v1/images/generations"))
            .header("Authorization", "Bearer YOUR_API_KEY")
            .header("Content-Type", "application/json")
            .header("X-New-Api-Async-Task", "true")
            .POST(HttpRequest.BodyPublishers.ofString(body))
            .build();
        var response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Async response example

When X-New-Api-Async-Task: true is set, the API returns task information:

{
  "code": "success",
  "message": "success",
  "data": {
    "task_id": "task_xxx",
    "status": "submitted"
  }
}

Async task query

Use GET /v1/images/tasks/:task_id to query an async image generation or image edit task created with X-New-Api-Async-Task: true.

GET /v1/images/tasks/{task_id}
Authorization: Bearer YOUR_API_KEY

cURL

curl https://api.nbility.dev/v1/images/tasks/task_xxx \
  -H "Authorization: Bearer YOUR_API_KEY"

Python

import requests

response = requests.get(
    "https://api.nbility.dev/v1/images/tasks/task_xxx",
    headers={"Authorization": "Bearer YOUR_API_KEY"},
)
print(response.json())

Node.js

const response = await fetch('https://api.nbility.dev/v1/images/tasks/task_xxx', {
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
  },
});

console.log(await response.json());

The response includes task status and, after completion, image result fields such as image_urls and result_url when available.

{
  "code": "success",
  "message": "success",
  "data": {
    "task_id": "task_xxx",
    "status": "succeeded",
    "status_raw": "SUCCESS",
    "action": "generations",
    "progress": "100%",
    "submit_time": 1745711800,
    "start_time": 1745711802,
    "finish_time": 1745711868,
    "result_url": "https://domain.com/cdn/20250427/uj1ojiZ9c1tR4PPbeV5Ts0pEu357py.png",
    "image_urls": [
      "https://domain.com/cdn/20250427/uj1ojiZ9c1tR4PPbeV5Ts0pEu357py.png"
    ]
  }
}

响应示例

{
  "created": 1745711868,
  "data": [
    {
      "revised_prompt": "A panda riding a bicycle with a backpack that says I love China",
      "url": "https://domain.com/cdn/20250427/uj1ojiZ9c1tR4PPbeV5Ts0pEu357py.png"
    }
  ]
}

gpt-image-2 图片编辑

概述

使用图片编辑接口基于原图、遮罩和提示词生成编辑后的图片。

POST /v1/images/edits
Authorization: Bearer YOUR_API_KEY
Content-Type: multipart/form-data

支持的模型

模型说明
gpt-image-2Image-2 图片编辑模型
gpt-image-2-vipImage-2 图片生成模型

请求参数

<ParamTable params={[ { name: "model", type: "string", required: false, default: "gpt-image-2", description: '模型名称,可选值:gpt-image-2gpt-image-2-vip' }, { name: "image", type: "file", required: true, description: "要编辑的图像。必须是有效的 PNG 文件,小于 4MB,并且是方形的;如果未提供遮罩,图像必须具有透明度,将用作遮罩" }, { name: "mask", type: "file", required: false, description: "附加遮罩图像。完全透明区域表示需要编辑的位置;必须是有效的 PNG 文件,小于 4MB,并且尺寸与原始 image 相同" }, { name: "prompt", type: "string", required: true, description: "所需图像的文本描述,最大长度为 1000 个字符" }, { name: "n", type: "string", required: false, description: "要生成的图像数,必须介于 1 和 10 之间" }, { name: "size", type: "string", required: false, description: '生成图像的大小:auto(默认)、1024x1024(square)、1536x1024(landscape)、1024x1536(portrait)、2048x2048(2K square)、2048x1152(2K landscape)、3840x2160(4K landscape)、2160x3840(4K portrait)' }, { name: "quality", type: "string", required: false, description: "图像质量,可选值:auto(默认)、lowmediumhigh" }, { name: "response_format", type: "string", required: false, description: '返回格式,必须是 urlb64_json' }, { name: "user", type: "string", required: false, description: "代表最终用户的唯一标识符,可以帮助监控和检测滥用行为" }, ]} />

最大边长必须小于或等于 3840 像素,边长必须是 16px 的倍数,长宽比不得超过 3:1,总像素数必须至少为 655,360 且不超过 8,294,400。

请求示例

cURL

curl https://api.nbility.dev/v1/images/edits \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F 'model="gpt-image-2"' \
  -F 'image=@"input.png"' \
  -F 'prompt="Merge the two images naturally into one composition"' \
  -F 'n="1"' \
  -F 'size="1024x1024"' \
  -F 'response_format="url"'

Python

import requests

with open("input.png", "rb") as image_file:
    response = requests.post(
        "https://api.nbility.dev/v1/images/edits",
        headers={"Authorization": "Bearer YOUR_API_KEY"},
        data={
            "model": "gpt-image-2",
            "prompt": "Merge the two images naturally into one composition",
            "n": "1",
            "size": "1024x1024",
            "response_format": "url",
        },
        files={"image": image_file},
    )
print(response.json())

Node.js

import fs from 'node:fs';
import FormData from 'form-data';

const form = new FormData();
form.append('model', 'gpt-image-2');
form.append('image', fs.createReadStream('input.png'));
form.append('prompt', 'Merge the two images naturally into one composition');
form.append('n', '1');
form.append('size', '1024x1024');
form.append('response_format', 'url');

const response = await fetch('https://api.nbility.dev/v1/images/edits', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    ...form.getHeaders(),
  },
  body: form,
});

console.log(await response.json());

Go

package main

import (
	"bytes"
	"fmt"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	w := multipart.NewWriter(&buf)
	w.WriteField("model", "gpt-image-2")
	w.WriteField("prompt", "Merge the two images naturally into one composition")
	w.WriteField("n", "1")
	w.WriteField("size", "1024x1024")
	w.WriteField("response_format", "url")

	fw, _ := w.CreateFormFile("image", "input.png")
	f, _ := os.Open("input.png")
	io.Copy(fw, f)
	f.Close()
	w.Close()

	req, _ := http.NewRequest("POST", "https://api.nbility.dev/v1/images/edits", &buf)
	req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
	req.Header.Set("Content-Type", w.FormDataContentType())
	resp, _ := http.DefaultClient.Do(req)
	defer resp.Body.Close()
	data, _ := io.ReadAll(resp.Body)
	fmt.Println(string(data))
}

响应示例

{
  "created": 1589478378,
  "data": [
    {
      "url": "https://domain.com/cdn/20250427/uj1ojiZ9c1tR4PPbeV5Ts0pEu357py.png"
    }
  ]
}