gpt-image-2
Text to Image
POST
/
v1
/
images
/
generations
Authorization
- Auth Type:
Bearer Auth(In:header) - Format:
Authorization: Bearer <YOUR_API_KEY> - Description: Use
Bearer <YOUR_API_KEY>. Format:Authorization: Bearer sk-xxxxxx. - API Key: where API Key is your AGCloud API KEY
gpt-image-2. Send a JSON request to https://aiandgpu.com/v1/images/generations.
Request Examples
curl --location 'https://aiandgpu.com/v1/images/generations' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2",
"prompt": "Draw a watercolor illustration of a red panda reading under a maple tree.",
"n": 1,
"size": "1024x1024",
"quality": "medium",
"output_format": "png"
}'
import base64
import requests
response = requests.post(
"https://aiandgpu.com/v1/images/generations",
headers={"Authorization": "Bearer <YOUR_API_KEY>"},
json={
"model": "gpt-image-2",
"prompt": "Draw a watercolor illustration of a red panda reading under a maple tree.",
"n": 1,
"size": "1024x1024",
"quality": "medium",
"output_format": "png",
},
timeout=600,
)
response.raise_for_status()
data = response.json()
with open("generated.png", "wb") as image_file:
image_file.write(base64.b64decode(data["data"][0]["b64_json"]))
// Node.js 18+; save as an .mjs file.
import { writeFile } from "node:fs/promises";
const response = await fetch("https://aiandgpu.com/v1/images/generations", {
method: "POST",
headers: {
Authorization: "Bearer <YOUR_API_KEY>",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gpt-image-2",
prompt: "Draw a watercolor illustration of a red panda reading under a maple tree.",
n: 1,
size: "1024x1024",
quality: "medium",
output_format: "png",
}),
});
if (!response.ok) throw new Error(await response.text());
const data = await response.json();
await writeFile("generated.png", Buffer.from(data.data[0].b64_json, "base64"));
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
func main() {
body, err := json.Marshal(map[string]any{
"model": "gpt-image-2",
"prompt": "Draw a watercolor illustration of a red panda reading under a maple tree.",
"n": 1,
"size": "1024x1024",
"quality": "medium",
"output_format": "png",
})
if err != nil { panic(err) }
req, err := http.NewRequest("POST", "https://aiandgpu.com/v1/images/generations", bytes.NewReader(body))
if err != nil { panic(err) }
req.Header.Set("Authorization", "Bearer <YOUR_API_KEY>")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: 10 * time.Minute}
res, err := client.Do(req)
if err != nil { panic(err) }
defer res.Body.Close()
result, err := io.ReadAll(res.Body)
if err != nil { panic(err) }
if res.StatusCode >= 400 { panic(string(result)) }
fmt.Println(string(result))
}
// Java 11+; save as GenerateImage.java.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class GenerateImage {
public static void main(String[] args) throws Exception {
String body = "{\"model\":\"gpt-image-2\","
+ "\"prompt\":\"Draw a watercolor illustration of a red panda reading under a maple tree.\","
+ "\"n\":1,\"size\":\"1024x1024\",\"quality\":\"medium\",\"output_format\":\"png\"}";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://aiandgpu.com/v1/images/generations"))
.timeout(Duration.ofMinutes(10))
.header("Authorization", "Bearer <YOUR_API_KEY>")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() >= 400) throw new RuntimeException(response.body());
System.out.println(response.body());
}
}
<?php
$curl = curl_init("https://aiandgpu.com/v1/images/generations");
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_TIMEOUT => 600,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <YOUR_API_KEY>",
"Content-Type: application/json",
],
CURLOPT_POSTFIELDS => json_encode([
"model" => "gpt-image-2",
"prompt" => "Draw a watercolor illustration of a red panda reading under a maple tree.",
"n" => 1,
"size" => "1024x1024",
"quality" => "medium",
"output_format" => "png",
]),
]);
$response = curl_exec($curl);
if ($response === false) throw new RuntimeException(curl_error($curl));
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($status >= 400) throw new RuntimeException($response);
echo $response;
// .NET 6+
using System;
using System.Net.Http;
using System.Net.Http.Json;
using var client = new HttpClient { Timeout = TimeSpan.FromMinutes(10) };
client.DefaultRequestHeaders.Add("Authorization", "Bearer <YOUR_API_KEY>");
using var response = await client.PostAsJsonAsync(
"https://aiandgpu.com/v1/images/generations",
new {
model = "gpt-image-2",
prompt = "Draw a watercolor illustration of a red panda reading under a maple tree.",
n = 1,
size = "1024x1024",
quality = "medium",
output_format = "png"
}
);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
data[].b64_json, not image URLs. Do not send response_format or style; describe the desired style in prompt.
Request Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
model | string | ✅ | - | Use gpt-image-2. |
prompt | string | ✅ | - | Image description; maximum 32,000 characters. |
n | integer | - | 1 | Image count, 1–10. |
size | string | - | auto | auto or WIDTHxHEIGHT; see constraints below. |
quality | string | - | auto | auto, low, medium, or high. |
background | string | - | auto | auto, opaque, or transparent (preview for GPT Image 2). Transparency requires PNG or WebP. |
moderation | string | - | auto | auto or low (less restrictive filtering). |
output_format | string | - | png | png, jpeg, or webp. |
output_compression | integer | - | 100 | 0–100; JPEG/WebP only. |
stream | boolean | - | false | Enable server-sent events (SSE). |
partial_images | integer | - | - | 0–3 previews when streaming; 0 returns only the final image. |
safety_identifier | string | - | - | Hashed end-user identifier; maximum 128 characters. |
user | string | - | - | Deprecated fallback for safety_identifier. |
Size and streaming notes
Forgpt-image-2, both dimensions must be multiples of 16, each at most 3840 pixels. The longer edge must not exceed three times the shorter edge, and total pixels must be between 655,360 and 8,294,400. Examples include 1024x1024, 1536x1024, 1024x1536, and 1536x864. Resolutions above 2560x1440 are experimental.
With stream: true, consume SSE events instead of parsing a single JSON response. Partial images use image_generation.partial_image; the final image uses image_generation.completed. Image bytes are Base64-encoded in b64_json. Fewer previews than requested may arrive if generation completes early; each preview adds 100 image output tokens.
See the OpenAI image generation guide for sizing and streaming details.