Back to Hub

OpenAI Image Generator

Sandbox Official API

Generate images from text prompts using OpenAI's GPT Image model. Uploads to artifact storage and returns a URL.

Tony CostanzoTony Costanzo
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

  1. Go to https://platform.openai.com/api-keys
  2. Create a new API key
  3. Ensure you have credits on your OpenAI account

Add workspace secret

Go to Settings > Integrations and create a new secret:

Secret NameDescription
OPENAI_API_KEYYour 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

ParameterRequiredDescription
promptYesText description of the image to generate
sizeNo1024x1024 (default), 1536x1024 (landscape), 1024x1536 (portrait)
qualityNoauto (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 generate
qualitystringImage 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}')