15 -H "Authorization: Bearer $XAI_API_KEY" \15 -H "Authorization: Bearer $XAI_API_KEY" \
16 -H "Content-Type: application/json" \16 -H "Content-Type: application/json" \
17 -d '{17 -d '{
18 "model": "grok-imagine-image-quality",18 "model": "grok-imagine-image-2.0",
19 "prompt": "A serene Japanese garden in winter",19 "prompt": "A serene Japanese garden in winter",
20 "response_format": "url",20 "response_format": "url",
21 "storage_options": {21 "storage_options": {
33 33
34response = client.image.sample(34response = client.image.sample(
35 prompt="A serene Japanese garden in winter",35 prompt="A serene Japanese garden in winter",
36 model="grok-imagine-image-quality",36 model="grok-imagine-image-2.0",
37 storage_options={"filename": "garden.jpg", "public_url": True},37 storage_options={"filename": "garden.jpg", "public_url": True},
38)38)
39 39
86 -H "Authorization: Bearer $XAI_API_KEY" \86 -H "Authorization: Bearer $XAI_API_KEY" \
87 -H "Content-Type: application/json" \87 -H "Content-Type: application/json" \
88 -d '{88 -d '{
89 "model": "grok-imagine-image-quality",89 "model": "grok-imagine-image-2.0",
90 "prompt": "A red circle on a white background",90 "prompt": "A red circle on a white background",
91 "response_format": "url",91 "response_format": "url",
92 "storage_options": {"filename": "circle.jpg"}92 "storage_options": {"filename": "circle.jpg"}
96```python customLanguage="pythonXAI"96```python customLanguage="pythonXAI"
97response = client.image.sample(97response = client.image.sample(
98 prompt="A red circle on a white background",98 prompt="A red circle on a white background",
99 model="grok-imagine-image-quality",99 model="grok-imagine-image-2.0",
100 storage_options={"filename": "circle.jpg"}, # store privately, no public URL100 storage_options={"filename": "circle.jpg"}, # store privately, no public URL
101)101)
102 102
117 -H "Authorization: Bearer $XAI_API_KEY" \117 -H "Authorization: Bearer $XAI_API_KEY" \
118 -H "Content-Type: application/json" \118 -H "Content-Type: application/json" \
119 -d '{119 -d '{
120 "model": "grok-imagine-image-quality",120 "model": "grok-imagine-image-2.0",
121 "prompt": "A futuristic city skyline at night",121 "prompt": "A futuristic city skyline at night",
122 "response_format": "url",122 "response_format": "url",
123 "storage_options": {123 "storage_options": {
131 -H "Authorization: Bearer $XAI_API_KEY" \131 -H "Authorization: Bearer $XAI_API_KEY" \
132 -H "Content-Type: application/json" \132 -H "Content-Type: application/json" \
133 -d '{133 -d '{
134 "model": "grok-imagine-image-quality",134 "model": "grok-imagine-image-2.0",
135 "prompt": "A futuristic city skyline at night",135 "prompt": "A futuristic city skyline at night",
136 "response_format": "url",136 "response_format": "url",
137 "storage_options": {137 "storage_options": {
151# Permanent file, public URL expires in 24h.151# Permanent file, public URL expires in 24h.
152response = client.image.sample(152response = client.image.sample(
153 prompt="A futuristic city skyline at night",153 prompt="A futuristic city skyline at night",
154 model="grok-imagine-image-quality",154 model="grok-imagine-image-2.0",
155 storage_options={"filename": "skyline.jpg", "public_url": {"expires_after": 86400}}, # 24h155 storage_options={"filename": "skyline.jpg", "public_url": {"expires_after": 86400}}, # 24h
156)156)
157print(response.file_output.file_id) # file_...157print(response.file_output.file_id) # file_...
161# File and public URL both expire in 2h (URL inherits the file's expiry).161# File and public URL both expire in 2h (URL inherits the file's expiry).
162response = client.image.sample(162response = client.image.sample(
163 prompt="A futuristic city skyline at night",163 prompt="A futuristic city skyline at night",
164 model="grok-imagine-image-quality",164 model="grok-imagine-image-2.0",
165 storage_options={"filename": "skyline.jpg", "expires_after": 7200, "public_url": True},165 storage_options={"filename": "skyline.jpg", "expires_after": 7200, "public_url": True},
166)166)
167print(response.file_output.file_id) # file_...167print(response.file_output.file_id) # file_...
172# File expires in 24h, public URL expires in 1h (independent, shorter).172# File expires in 24h, public URL expires in 1h (independent, shorter).
173response = client.image.sample(173response = client.image.sample(
174 prompt="A futuristic city skyline at night",174 prompt="A futuristic city skyline at night",
175 model="grok-imagine-image-quality",175 model="grok-imagine-image-2.0",
176 storage_options={176 storage_options={
177 "filename": "skyline.jpg",177 "filename": "skyline.jpg",
178 "expires_after": 86400,178 "expires_after": 86400,
209 -H "Authorization: Bearer $XAI_API_KEY" \209 -H "Authorization: Bearer $XAI_API_KEY" \
210 -H "Content-Type: application/json" \210 -H "Content-Type: application/json" \
211 -d '{211 -d '{
212 "model": "grok-imagine-image-quality",212 "model": "grok-imagine-image-2.0",
213 "prompt": "A cat wearing a hat, four different art styles",213 "prompt": "A cat wearing a hat, four different art styles",
214 "n": 4,214 "n": 4,
215 "response_format": "url",215 "response_format": "url",
225 225
226responses = client.image.sample_batch(226responses = client.image.sample_batch(
227 prompt="A cat wearing a hat, four different art styles",227 prompt="A cat wearing a hat, four different art styles",
228 model="grok-imagine-image-quality",228 model="grok-imagine-image-2.0",
229 n=4,229 n=4,
230 storage_options={"filename": "cat-styles.jpg", "public_url": True},230 storage_options={"filename": "cat-styles.jpg", "public_url": True},
231)231)
243 -H "Authorization: Bearer $XAI_API_KEY" \243 -H "Authorization: Bearer $XAI_API_KEY" \
244 -H "Content-Type: application/json" \244 -H "Content-Type: application/json" \
245 -d '{245 -d '{
246 "model": "grok-imagine-image-quality",246 "model": "grok-imagine-image-2.0",
247 "prompt": "Add a party hat to the dog",247 "prompt": "Add a party hat to the dog",
248 "image": {248 "image": {
249 "url": "https://docs.x.ai/assets/api-examples/images/style-realistic.png"249 "url": "https://docs.x.ai/assets/api-examples/images/style-realistic.png"
261 261
262response = client.image.sample(262response = client.image.sample(
263 prompt="Add a party hat to the dog",263 prompt="Add a party hat to the dog",
264 model="grok-imagine-image-quality",264 model="grok-imagine-image-2.0",
265 image_url="https://docs.x.ai/assets/api-examples/images/style-realistic.png",265 image_url="https://docs.x.ai/assets/api-examples/images/style-realistic.png",
266 storage_options={"filename": "party-dog.png", "public_url": True},266 storage_options={"filename": "party-dog.png", "public_url": True},
267)267)
352 352
353response = client.image.sample(353response = client.image.sample(
354 prompt="A vintage poster",354 prompt="A vintage poster",
355 model="grok-imagine-image-quality",355 model="grok-imagine-image-2.0",
356 storage_options={"filename": "poster.jpg", "public_url": True},356 storage_options={"filename": "poster.jpg", "public_url": True},
357)357)
358 358
396 396
397response = client.image.sample(397response = client.image.sample(
398 prompt="A futuristic city",398 prompt="A futuristic city",
399 model="grok-imagine-image-quality",399 model="grok-imagine-image-2.0",
400 storage_options={"filename": "city.jpg", "public_url": True},400 storage_options={"filename": "city.jpg", "public_url": True},
401)401)
402file_id = response.file_output.file_id402file_id = response.file_output.file_id