resources/images/methods/generate/index.md +0 −372 deleted
File Deleted View Diff
1## Create image
2
3**post** `/images/generations`
4
5Creates an image given a prompt. [Learn more](/docs/guides/images).
6
7### Body Parameters
8
9- `prompt: string`
10
11 A text description of the desired image(s). The maximum length is 32000 characters for the GPT image models, 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`.
12
13- `background: optional "transparent" or "opaque" or "auto"`
14
15 Allows to set transparency for the background of the generated image(s).
16 This parameter is only supported for the GPT image models. Must be one of
17 `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
18 model will automatically determine the best background for the image.
19
20 If `transparent`, the output format needs to support transparency, so it
21 should be set to either `png` (default value) or `webp`.
22
23 - `"transparent"`
24
25 - `"opaque"`
26
27 - `"auto"`
28
29- `model: optional string or ImageModel`
30
31 The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults to `dall-e-2` unless a parameter specific to the GPT image models is used.
32
33 - `string`
34
35 - `ImageModel = "gpt-image-1.5" or "dall-e-2" or "dall-e-3" or 2 more`
36
37 - `"gpt-image-1.5"`
38
39 - `"dall-e-2"`
40
41 - `"dall-e-3"`
42
43 - `"gpt-image-1"`
44
45 - `"gpt-image-1-mini"`
46
47- `moderation: optional "low" or "auto"`
48
49 Control the content-moderation level for images generated by the GPT image models. Must be either `low` for less restrictive filtering or `auto` (default value).
50
51 - `"low"`
52
53 - `"auto"`
54
55- `n: optional number`
56
57 The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
58
59- `output_compression: optional number`
60
61 The compression level (0-100%) for the generated images. This parameter is only supported for the GPT image models with the `webp` or `jpeg` output formats, and defaults to 100.
62
63- `output_format: optional "png" or "jpeg" or "webp"`
64
65 The format in which the generated images are returned. This parameter is only supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
66
67 - `"png"`
68
69 - `"jpeg"`
70
71 - `"webp"`
72
73- `partial_images: optional number`
74
75 The number of partial images to generate. This parameter is used for
76 streaming responses that return partial images. Value must be between 0 and 3.
77 When set to 0, the response will be a single image sent in one streaming event.
78
79 Note that the final image may be sent before the full number of partial images
80 are generated if the full image is generated more quickly.
81
82- `quality: optional "standard" or "hd" or "low" or 3 more`
83
84 The quality of the image that will be generated.
85
86 - `auto` (default value) will automatically select the best quality for the given model.
87 - `high`, `medium` and `low` are supported for the GPT image models.
88 - `hd` and `standard` are supported for `dall-e-3`.
89 - `standard` is the only option for `dall-e-2`.
90
91 - `"standard"`
92
93 - `"hd"`
94
95 - `"low"`
96
97 - `"medium"`
98
99 - `"high"`
100
101 - `"auto"`
102
103- `response_format: optional "url" or "b64_json"`
104
105 The format in which generated images with `dall-e-2` and `dall-e-3` are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated. This parameter isn't supported for the GPT image models, which always return base64-encoded images.
106
107 - `"url"`
108
109 - `"b64_json"`
110
111- `size: optional string or "auto" or "1024x1024" or "1536x1024" or 5 more`
112
113 The size of the generated images. For `gpt-image-2` and `gpt-image-2-2026-04-21`, arbitrary resolutions are supported as `WIDTHxHEIGHT` strings, for example `1536x864`. Width and height must both be divisible by 16 and the requested aspect ratio must be between 1:3 and 3:1. Resolutions above `2560x1440` are experimental, and the maximum supported resolution is `3840x2160`. The requested size must also satisfy the model's current pixel and edge limits. The standard sizes `1024x1024`, `1536x1024`, and `1024x1536` are supported by the GPT image models; `auto` is supported for models that allow automatic sizing. For `dall-e-2`, use one of `256x256`, `512x512`, or `1024x1024`. For `dall-e-3`, use one of `1024x1024`, `1792x1024`, or `1024x1792`.
114
115 - `string`
116
117 - `"auto" or "1024x1024" or "1536x1024" or 5 more`
118
119 The size of the generated images. For `gpt-image-2` and `gpt-image-2-2026-04-21`, arbitrary resolutions are supported as `WIDTHxHEIGHT` strings, for example `1536x864`. Width and height must both be divisible by 16 and the requested aspect ratio must be between 1:3 and 3:1. Resolutions above `2560x1440` are experimental, and the maximum supported resolution is `3840x2160`. The requested size must also satisfy the model's current pixel and edge limits. The standard sizes `1024x1024`, `1536x1024`, and `1024x1536` are supported by the GPT image models; `auto` is supported for models that allow automatic sizing. For `dall-e-2`, use one of `256x256`, `512x512`, or `1024x1024`. For `dall-e-3`, use one of `1024x1024`, `1792x1024`, or `1024x1792`.
120
121 - `"auto"`
122
123 - `"1024x1024"`
124
125 - `"1536x1024"`
126
127 - `"1024x1536"`
128
129 - `"256x256"`
130
131 - `"512x512"`
132
133 - `"1792x1024"`
134
135 - `"1024x1792"`
136
137- `stream: optional boolean`
138
139 Generate the image in streaming mode. Defaults to `false`. See the
140 [Image generation guide](/docs/guides/image-generation) for more information.
141 This parameter is only supported for the GPT image models.
142
143- `style: optional "vivid" or "natural"`
144
145 The style of the generated images. This parameter is only supported for `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images.
146
147 - `"vivid"`
148
149 - `"natural"`
150
151- `user: optional string`
152
153 A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](/docs/guides/safety-best-practices#end-user-ids).
154
155### Returns
156
157- `ImagesResponse object { created, background, data, 4 more }`
158
159 The response from the image generation endpoint.
160
161 - `created: number`
162
163 The Unix timestamp (in seconds) of when the image was created.
164
165 - `background: optional "transparent" or "opaque"`
166
167 The background parameter used for the image generation. Either `transparent` or `opaque`.
168
169 - `"transparent"`
170
171 - `"opaque"`
172
173 - `data: optional array of Image`
174
175 The list of generated images.
176
177 - `b64_json: optional string`
178
179 The base64-encoded JSON of the generated image. Returned by default for the GPT image models, and only present if `response_format` is set to `b64_json` for `dall-e-2` and `dall-e-3`.
180
181 - `revised_prompt: optional string`
182
183 For `dall-e-3` only, the revised prompt that was used to generate the image.
184
185 - `url: optional string`
186
187 When using `dall-e-2` or `dall-e-3`, the URL of the generated image if `response_format` is set to `url` (default value). Unsupported for the GPT image models.
188
189 - `output_format: optional "png" or "webp" or "jpeg"`
190
191 The output format of the image generation. Either `png`, `webp`, or `jpeg`.
192
193 - `"png"`
194
195 - `"webp"`
196
197 - `"jpeg"`
198
199 - `quality: optional "low" or "medium" or "high"`
200
201 The quality of the image generated. Either `low`, `medium`, or `high`.
202
203 - `"low"`
204
205 - `"medium"`
206
207 - `"high"`
208
209 - `size: optional "1024x1024" or "1024x1536" or "1536x1024"`
210
211 The size of the image generated. Either `1024x1024`, `1024x1536`, or `1536x1024`.
212
213 - `"1024x1024"`
214
215 - `"1024x1536"`
216
217 - `"1536x1024"`
218
219 - `usage: optional object { input_tokens, input_tokens_details, output_tokens, 2 more }`
220
221 For `gpt-image-1` only, the token usage information for the image generation.
222
223 - `input_tokens: number`
224
225 The number of tokens (images and text) in the input prompt.
226
227 - `input_tokens_details: object { image_tokens, text_tokens }`
228
229 The input tokens detailed information for the image generation.
230
231 - `image_tokens: number`
232
233 The number of image tokens in the input prompt.
234
235 - `text_tokens: number`
236
237 The number of text tokens in the input prompt.
238
239 - `output_tokens: number`
240
241 The number of output tokens generated by the model.
242
243 - `total_tokens: number`
244
245 The total number of tokens (images and text) used for the image generation.
246
247 - `output_tokens_details: optional object { image_tokens, text_tokens }`
248
249 The output token details for the image generation.
250
251 - `image_tokens: number`
252
253 The number of image output tokens generated by the model.
254
255 - `text_tokens: number`
256
257 The number of text output tokens generated by the model.
258
259### Example
260
261```http
262curl https://api.openai.com/v1/images/generations \
263 -H 'Content-Type: application/json' \
264 -H "Authorization: Bearer $OPENAI_API_KEY" \
265 -d '{
266 "prompt": "A cute baby sea otter",
267 "background": "transparent",
268 "moderation": "low",
269 "n": 1,
270 "output_compression": 100,
271 "output_format": "png",
272 "partial_images": 1,
273 "quality": "medium",
274 "response_format": "url",
275 "style": "vivid",
276 "user": "user-1234"
277 }'
278```
279
280#### Response
281
282```json
283{
284 "created": 0,
285 "background": "transparent",
286 "data": [
287 {
288 "b64_json": "b64_json",
289 "revised_prompt": "revised_prompt",
290 "url": "https://example.com"
291 }
292 ],
293 "output_format": "png",
294 "quality": "low",
295 "size": "1024x1024",
296 "usage": {
297 "input_tokens": 0,
298 "input_tokens_details": {
299 "image_tokens": 0,
300 "text_tokens": 0
301 },
302 "output_tokens": 0,
303 "total_tokens": 0,
304 "output_tokens_details": {
305 "image_tokens": 0,
306 "text_tokens": 0
307 }
308 }
309}
310```
311
312### Generate image
313
314```http
315curl https://api.openai.com/v1/images/generations \
316 -H "Content-Type: application/json" \
317 -H "Authorization: Bearer $OPENAI_API_KEY" \
318 -d '{
319 "model": "gpt-image-1.5",
320 "prompt": "A cute baby sea otter",
321 "n": 1,
322 "size": "1024x1024"
323 }'
324```
325
326#### Response
327
328```json
329{
330 "created": 1713833628,
331 "data": [
332 {
333 "b64_json": "..."
334 }
335 ],
336 "usage": {
337 "total_tokens": 100,
338 "input_tokens": 50,
339 "output_tokens": 50,
340 "input_tokens_details": {
341 "text_tokens": 10,
342 "image_tokens": 40
343 }
344 }
345}
346```
347
348### Streaming
349
350```http
351curl https://api.openai.com/v1/images/generations \
352 -H "Content-Type: application/json" \
353 -H "Authorization: Bearer $OPENAI_API_KEY" \
354 -d '{
355 "model": "gpt-image-1.5",
356 "prompt": "A cute baby sea otter",
357 "n": 1,
358 "size": "1024x1024",
359 "stream": true
360 }' \
361 --no-buffer
362```
363
364#### Response
365
366```json
367event: image_generation.partial_image
368data: {"type":"image_generation.partial_image","b64_json":"...","partial_image_index":0}
369
370event: image_generation.completed
371data: {"type":"image_generation.completed","b64_json":"...","usage":{"total_tokens":100,"input_tokens":50,"output_tokens":50,"input_tokens_details":{"text_tokens":10,"image_tokens":40}}}
372```