curl --request POST \
--url https://api.example.com/v1/images/generations \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"prompt": "<string>",
"model": "<string>",
"background": "<string>",
"moderation": "<string>",
"n": 123,
"output_compression": 123,
"output_format": "<string>",
"partial_images": 123,
"response_format": "<string>",
"quality": "<string>",
"size": "<string>",
"stream": true,
"style": "<string>",
"user": "<string>"
}
'import requests
url = "https://api.example.com/v1/images/generations"
payload = {
"prompt": "<string>",
"model": "<string>",
"background": "<string>",
"moderation": "<string>",
"n": 123,
"output_compression": 123,
"output_format": "<string>",
"partial_images": 123,
"response_format": "<string>",
"quality": "<string>",
"size": "<string>",
"stream": True,
"style": "<string>",
"user": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({
prompt: '<string>',
model: '<string>',
background: '<string>',
moderation: '<string>',
n: 123,
output_compression: 123,
output_format: '<string>',
partial_images: 123,
response_format: '<string>',
quality: '<string>',
size: '<string>',
stream: true,
style: '<string>',
user: '<string>'
})
};
fetch('https://api.example.com/v1/images/generations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/v1/images/generations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'prompt' => '<string>',
'model' => '<string>',
'background' => '<string>',
'moderation' => '<string>',
'n' => 123,
'output_compression' => 123,
'output_format' => '<string>',
'partial_images' => 123,
'response_format' => '<string>',
'quality' => '<string>',
'size' => '<string>',
'stream' => true,
'style' => '<string>',
'user' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/v1/images/generations"
payload := strings.NewReader("{\n \"prompt\": \"<string>\",\n \"model\": \"<string>\",\n \"background\": \"<string>\",\n \"moderation\": \"<string>\",\n \"n\": 123,\n \"output_compression\": 123,\n \"output_format\": \"<string>\",\n \"partial_images\": 123,\n \"response_format\": \"<string>\",\n \"quality\": \"<string>\",\n \"size\": \"<string>\",\n \"stream\": true,\n \"style\": \"<string>\",\n \"user\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/v1/images/generations")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"prompt\": \"<string>\",\n \"model\": \"<string>\",\n \"background\": \"<string>\",\n \"moderation\": \"<string>\",\n \"n\": 123,\n \"output_compression\": 123,\n \"output_format\": \"<string>\",\n \"partial_images\": 123,\n \"response_format\": \"<string>\",\n \"quality\": \"<string>\",\n \"size\": \"<string>\",\n \"stream\": true,\n \"style\": \"<string>\",\n \"user\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/v1/images/generations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"prompt\": \"<string>\",\n \"model\": \"<string>\",\n \"background\": \"<string>\",\n \"moderation\": \"<string>\",\n \"n\": 123,\n \"output_compression\": 123,\n \"output_format\": \"<string>\",\n \"partial_images\": 123,\n \"response_format\": \"<string>\",\n \"quality\": \"<string>\",\n \"size\": \"<string>\",\n \"stream\": true,\n \"style\": \"<string>\",\n \"user\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"created": 123,
"background": "<string>",
"data": [
{
"b64_json": "<string>",
"revised_prompt": "<string>",
"output_format": "<string>",
"z_index": 123,
"name": "<string>",
"bounding_box": {
"absolute": [
123
],
"normalized": [
123
]
},
"url": "<string>",
"size": "<string>",
"error": {
"code": "<string>",
"message": "<string>",
"type": "<string>",
"param": "<string>"
},
"description": "<string>"
}
],
"output_format": "<string>",
"quality": "<string>",
"size": "<string>",
"usage": {
"output_tokens_details": {
"image_tokens": 123,
"text_tokens": 123
},
"input_tokens": 123,
"output_tokens": 123,
"total_tokens": 123,
"input_tokens_details": {
"image_tokens": 123,
"text_tokens": 123
}
}
}OpenAI image generation
Create image generation requests compatible with the OpenAI Images protocol, supporting multiple sizes, quality levels, and output formats
curl --request POST \
--url https://api.example.com/v1/images/generations \
--header 'Authorization: <authorization>' \
--header 'Content-Type: application/json' \
--data '
{
"prompt": "<string>",
"model": "<string>",
"background": "<string>",
"moderation": "<string>",
"n": 123,
"output_compression": 123,
"output_format": "<string>",
"partial_images": 123,
"response_format": "<string>",
"quality": "<string>",
"size": "<string>",
"stream": true,
"style": "<string>",
"user": "<string>"
}
'import requests
url = "https://api.example.com/v1/images/generations"
payload = {
"prompt": "<string>",
"model": "<string>",
"background": "<string>",
"moderation": "<string>",
"n": 123,
"output_compression": 123,
"output_format": "<string>",
"partial_images": 123,
"response_format": "<string>",
"quality": "<string>",
"size": "<string>",
"stream": True,
"style": "<string>",
"user": "<string>"
}
headers = {
"Authorization": "<authorization>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: '<authorization>', 'Content-Type': 'application/json'},
body: JSON.stringify({
prompt: '<string>',
model: '<string>',
background: '<string>',
moderation: '<string>',
n: 123,
output_compression: 123,
output_format: '<string>',
partial_images: 123,
response_format: '<string>',
quality: '<string>',
size: '<string>',
stream: true,
style: '<string>',
user: '<string>'
})
};
fetch('https://api.example.com/v1/images/generations', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/v1/images/generations",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'prompt' => '<string>',
'model' => '<string>',
'background' => '<string>',
'moderation' => '<string>',
'n' => 123,
'output_compression' => 123,
'output_format' => '<string>',
'partial_images' => 123,
'response_format' => '<string>',
'quality' => '<string>',
'size' => '<string>',
'stream' => true,
'style' => '<string>',
'user' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Authorization: <authorization>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/v1/images/generations"
payload := strings.NewReader("{\n \"prompt\": \"<string>\",\n \"model\": \"<string>\",\n \"background\": \"<string>\",\n \"moderation\": \"<string>\",\n \"n\": 123,\n \"output_compression\": 123,\n \"output_format\": \"<string>\",\n \"partial_images\": 123,\n \"response_format\": \"<string>\",\n \"quality\": \"<string>\",\n \"size\": \"<string>\",\n \"stream\": true,\n \"style\": \"<string>\",\n \"user\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "<authorization>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/v1/images/generations")
.header("Authorization", "<authorization>")
.header("Content-Type", "application/json")
.body("{\n \"prompt\": \"<string>\",\n \"model\": \"<string>\",\n \"background\": \"<string>\",\n \"moderation\": \"<string>\",\n \"n\": 123,\n \"output_compression\": 123,\n \"output_format\": \"<string>\",\n \"partial_images\": 123,\n \"response_format\": \"<string>\",\n \"quality\": \"<string>\",\n \"size\": \"<string>\",\n \"stream\": true,\n \"style\": \"<string>\",\n \"user\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/v1/images/generations")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = '<authorization>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"prompt\": \"<string>\",\n \"model\": \"<string>\",\n \"background\": \"<string>\",\n \"moderation\": \"<string>\",\n \"n\": 123,\n \"output_compression\": 123,\n \"output_format\": \"<string>\",\n \"partial_images\": 123,\n \"response_format\": \"<string>\",\n \"quality\": \"<string>\",\n \"size\": \"<string>\",\n \"stream\": true,\n \"style\": \"<string>\",\n \"user\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"created": 123,
"background": "<string>",
"data": [
{
"b64_json": "<string>",
"revised_prompt": "<string>",
"output_format": "<string>",
"z_index": 123,
"name": "<string>",
"bounding_box": {
"absolute": [
123
],
"normalized": [
123
]
},
"url": "<string>",
"size": "<string>",
"error": {
"code": "<string>",
"message": "<string>",
"type": "<string>",
"param": "<string>"
},
"description": "<string>"
}
],
"output_format": "<string>",
"quality": "<string>",
"size": "<string>",
"usage": {
"output_tokens_details": {
"image_tokens": 123,
"text_tokens": 123
},
"input_tokens": 123,
"output_tokens": 123,
"total_tokens": 123,
"input_tokens_details": {
"image_tokens": 123,
"text_tokens": 123
}
}
}Features
- Compatible with the OpenAI Images API request structure, so existing integration code migrates seamlessly
- Supports batch generation through the
nparameter to return multiple images at once - Supports
png,jpeg, andwebpoutput formats withoutput_compressioncontrol - Supports background settings via
background(transparent/opaque/auto) - Supports both
urlandb64_jsonresponse formats - Supports streaming (SSE) responses with intermediate results pushed via
partial_images - Supports generation parameters such as
quality,size, andstyle
Authentication
Include theAuthorization header in the format Bearer YOUR_API_KEY.
Supported image models
Available image models in the Model Square, please refer to Model List.Quick example
import requests
url = "https://api.haitoken.ai/v1/images/generations"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"model": "gpt-image-2",
"prompt": "An orange cat wearing an astronaut helmet floating in space with Earth in the background",
"size": "1024x1024",
"quality": "high",
"n": 1
}
response = requests.post(url, json=data, headers=headers)
print(response.json())
const response = await fetch('https://api.haitoken.ai/v1/images/generations', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-image-2',
prompt: 'An orange cat wearing an astronaut helmet floating in space with Earth in the background',
size: '1024x1024',
quality: 'high',
n: 1
})
});
const data = await response.json();
console.log(data);
curl -X POST 'https://api.haitoken.ai/v1/images/generations' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-image-2",
"prompt": "An orange cat wearing an astronaut helmet floating in space with Earth in the background",
"size": "1024x1024",
"quality": "high",
"n": 1
}'
{
"created": 1787875200,
"data": [
{
"url": "https://example.com/images/generated-1.png",
"revised_prompt": "An orange cat wearing a transparent astronaut helmet floating in space with a blue Earth in the background"
}
],
"size": "1024x1024",
"quality": "high",
"usage": {
"input_tokens": 12,
"output_tokens": 4160,
"total_tokens": 4172,
"input_tokens_details": {
"image_tokens": 0,
"text_tokens": 12
},
"output_tokens_details": {
"image_tokens": 4160,
"text_tokens": 0
}
}
}
Next steps
- See OpenAI image edit to edit existing images
- See Seedream image generation for Seedream extension parameters
- See the model list for available image models
Body
Text prompt
Model name
Background of the generated image: "transparent" / "opaque" / "auto"; defaults to "auto"
Content moderation mode
Number of images to generate
Output compression level
Output image format, e.g. "png", "jpeg", "webp"
Number of intermediate result images pushed per update in streaming responses
Response format, "url" or "b64_json"
Image quality, e.g. "high", "medium", "low", and "auto"; defaults to "auto"
Image size, e.g. "1024x1024"
Whether to use streaming (SSE) responses
Image style, e.g. "vivid", "natural" (DALL-E 3 feature)
End-user identifier for monitoring and abuse detection
Response
Image generation timestamp (Unix seconds)
Background image data echo
Generated image data list
Show child attributes
Show child attributes
Output image format echo
Image quality echo
Image size echo
Token and image usage statistics
Show child attributes
Show child attributes