Back to Hub
Tony Costanzo
OpenAI Image Generator
Sandbox Official APIGenerate images from text prompts using OpenAI's GPT Image model. Uploads to artifact storage and returns a URL.
0 installs
v2.0.0
Mar 27, 2026
OpenAI Image Generator
Official Generate images from text prompts using OpenAI's GPT Image model.
What this tool does: Creates images from text descriptions using OpenAI's GPT Image model (gpt-image-1). Supports multiple sizes and quality levels. Images are uploaded to artifact storage and displayed inline in chat. Runs in a secure sandbox.
Setup
Get an OpenAI API key
- Go to https://platform.openai.com/api-keys
- Create a new API key
- Ensure you have credits on your OpenAI account
Add workspace secret
Go to Settings > Integrations and create a new secret:
| Secret Name | Description |
|---|---|
OPENAI_API_KEY | Your OpenAI API key |
This is a sandbox tool and OpenAI image generation has its own costs. Each execution uses workspace credits plus OpenAI's API charges.
Parameters
| Parameter | Required | Description |
|---|---|---|
prompt | Yes | Text description of the image to generate |
size | No | 1024x1024 (default), 1536x1024 (landscape), 1024x1536 (portrait) |
quality | No | auto (default), low, medium, high |
Example Prompts
- "Generate an image of a sunset over mountains in watercolor style"
- "Create a logo concept for a coffee shop called 'Bean There'"
- "Make a landscape image of a futuristic city at night"
- "Generate a portrait-style illustration of a friendly robot"
Tool Definition
Input Parameters
sizestringImage size: '1024x1024' (default), '1536x1024' (landscape), '1024x1536' (portrait)promptstringText description of the image to generatequalitystringImage quality: 'auto' (default), 'low', 'medium', 'high'Sandbox Configuration
Language: python
Timeout: 60s
Code
import json
import urllib.request
import urllib.error
import os
import base64
data = TOOL_INPUT
prompt = data.get('prompt', '')
size = data.get('size', '1024x1024')
quality = data.get('quality', 'auto')
if not prompt:
print('Error: prompt is required')
else:
api_key = os.environ.get('OPENAI_API_KEY', '')
if not api_key:
print('Error: OPENAI_API_KEY secret is required. Add it in Settings > Secrets.')
else:
try:
url = 'https://api.openai.com/v1/images/generations'
payload = json.dumps({
'model': 'gpt-image-1',
'prompt': prompt,
'n': 1,
'size': size,
'quality': quality
}).encode('utf-8')
req = urllib.request.Request(url, data=payload, headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}, method='POST')
with urllib.request.urlopen(req, timeout=50) as resp:
result = json.loads(resp.read().decode('utf-8'))
image = result['data'][0]
image_b64 = image.get('b64_json', '')
image_bytes = base64.b64decode(image_b64)
artifact_key = upload_artifact(image_bytes, 'image/png')
if artifact_key:
print(json.dumps({
'artifact_key': artifact_key,
'contentType': 'image/png',
'revised_prompt': image.get('revised_prompt', ''),
'model': 'gpt-image-1',
'size': size
}))
else:
print(json.dumps({
'message': 'Image generated but artifact storage unavailable.',
'revised_prompt': image.get('revised_prompt', ''),
'model': 'gpt-image-1',
'size': size
}))
except urllib.error.HTTPError as e:
err_body = e.read().decode('utf-8') if e.fp else ''
print(f'Error from OpenAI: {e.code} {err_body}')
except Exception as e:
print(f'Error: {e}')