Image Generation
Sprelay exposes POST /v1/images/generations. The combination gpt-image-2, 1024x1024, low, and Base64 output has been tested with a temporary low-quota key. Image editing is not documented because it has not been verified.
Before you begin
- Confirm
gpt-image-2in the Model Catalog. - Choose the current dedicated image group when creating the key.
- Give that key a separate, small quota.
- Generate one low-quality image for the first test.
curl
bash
# Save the large JSON response to a file instead of filling the terminal.
curl https://sprelaytoken.com/v1/images/generations \
-H "Authorization: Bearer $SPRELAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "A simple flat blue circle centered on a white background.",
"n": 1,
"size": "1024x1024",
"quality": "low"
}' \
--output response.jsonprompt describes the image, n: 1 requests one result, and size plus quality use the tested low-cost combination. JSON cannot contain comments.
Decode the first image on a system with jq and base64:
bash
# Extract Base64 from JSON, decode it, and write a PNG file.
jq -r '.data[0].b64_json' response.json | base64 --decode > sprelay-image.pngPython
python
# base64 decodes the image; requests sends HTTP; os reads the key.
import base64
import os
import requests
# Image generation can take longer than text, so use a 180-second timeout.
response = requests.post(
"https://sprelaytoken.com/v1/images/generations",
headers={
"Authorization": f"Bearer {os.environ['SPRELAY_API_KEY']}",
"Content-Type": "application/json",
},
json={
"model": "gpt-image-2",
"prompt": "A simple flat blue circle centered on a white background.",
"n": 1,
"size": "1024x1024",
"quality": "low",
},
timeout=180,
)
# Stop on 4xx/5xx before trying to decode an error response.
response.raise_for_status()
payload = response.json()
image_bytes = base64.b64decode(payload["data"][0]["b64_json"])
# wb writes the decoded bytes as a binary PNG file.
with open("sprelay-image.png", "wb") as image_file:
image_file.write(image_bytes)PowerShell
powershell
# Read the key from the current PowerShell environment.
$headers = @{
Authorization = "Bearer $env:SPRELAY_API_KEY"
"Content-Type" = "application/json"
}
# Request one tested low-quality image to control the first-call cost.
$body = @{
model = "gpt-image-2"
prompt = "A simple flat blue circle centered on a white background."
n = 1
size = "1024x1024"
quality = "low"
} | ConvertTo-Json
# PowerShell parses the JSON response automatically.
$response = Invoke-RestMethod `
-Uri "https://sprelaytoken.com/v1/images/generations" `
-Method Post `
-Headers $headers `
-Body $body `
-TimeoutSec 180
# Decode the first Base64 image and save it in the current directory.
$bytes = [Convert]::FromBase64String($response.data[0].b64_json)
[IO.File]::WriteAllBytes("sprelay-image.png", $bytes)Base64 image data is large. Decode or upload it promptly; do not write the complete value to application logs. Before retrying a timeout, check usage logs because the upstream image may already have been generated and charged.
