Image API

Generate images using AI with OpenAI and Replicate formats

The Image API supports two formats for AI image generation:

  1. OpenAI Format - Synchronous generation (compatible with OpenAI SDK)
  2. Replicate Format - Asynchronous predictions (for longer-running models)

Authentication: API Key or OAuth token
Required Scopes (OAuth): ai:images

Quick Comparison

FeatureOpenAI FormatReplicate Format
Endpoint/api/v1/images/generations/api/v1/predictions
ExecutionSynchronousAsynchronous (polling)
Best ForFast models (< 10s)Slow models (10s - minutes)
FormatOpenAI-compatibleReplicate-compatible
SDK SupportOpenAI SDKfetch/axios

OpenAI Format (Generations)

Endpoint

POST /api/v1/images/generations

Quick Start

import OpenAI from 'openai'
 
const client = new OpenAI({
  apiKey: process.env.GATEWAY_API_KEY,
  baseURL: 'https://hypery.ai/api/v1'
})
 
const response = await client.images.generate({
  model: 'dall-e-3',
  prompt: 'A serene landscape with mountains',
  n: 1,
  size: '1024x1024'
})
 
console.log(response.data[0].url)

Request Format

{
  "model": "dall-e-3",
  "prompt": "A beautiful sunset over the ocean",
  "n": 1,
  "size": "1024x1024",
  "quality": "standard",
  "style": "vivid"
}

Request Parameters

ParameterTypeRequiredDescription
modelstringYesModel ID (e.g., dall-e-3)
promptstringYesText description of desired image
nnumberNoNumber of images (1-10), default 1
sizestringNoImage dimensions
qualitystringNostandard or hd
stylestringNovivid or natural

Response Format

{
  "created": 1699564800,
  "data": [
    {
      "url": "https://hypery.ai/api/v1/images/serve/abc123",
      "revised_prompt": "A beautiful sunset over the ocean with vibrant colors"
    }
  ]
}

Replicate Format (Predictions)

For slower models or when you need polling support, use the Predictions.

See complete Predictions documentation →

Quick Example

// 1. Create prediction
const prediction = await fetch('https://hypery.ai/api/v1/predictions', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    model: 'black-forest-labs/flux-schnell',
    provider: 'replicate',
    input: {
      prompt: 'A futuristic city at night',
      width: 1024,
      height: 1024
    }
  })
}).then(r => r.json())
 
// 2. Poll for completion
while (prediction.status === 'starting' || prediction.status === 'processing') {
  await new Promise(resolve => setTimeout(resolve, 1000))
  
  prediction = await fetch(
    `https://hypery.ai/api/v1/predictions/${prediction.id}`,
    { headers: { 'Authorization': `Bearer ${apiKey}` } }
  ).then(r => r.json())
}
 
// 3. Get result
if (prediction.status === 'succeeded') {
  console.log('Image URL:', prediction.output[0])
}

Available Models

Fast Models (OpenAI Format)

ModelProviderSpeedQualityBest For
dall-e-3OpenAIFast (~10s)HighGeneral purpose
dall-e-2OpenAIVery Fast (~5s)GoodQuick iterations

Advanced Models (Replicate Format)

ModelSpeedQualityBest For
black-forest-labs/flux-schnellFast (10-20s)ExcellentHigh quality images
black-forest-labs/flux-devMedium (20-40s)ExceptionalProfessional work
stability-ai/stable-diffusion-3Medium (15-30s)ExcellentVersatile generation
ideogram-ai/ideogram-v2Medium (20-40s)ExcellentText in images

Code Examples

import OpenAI from 'openai'
 
const client = new OpenAI({
  apiKey: process.env.GATEWAY_API_KEY,
  baseURL: 'https://hypery.ai/api/v1'
})
 
// Generate image
const image = await client.images.generate({
  model: 'dall-e-3',
  prompt: 'A programmer drinking coffee, digital art',
  size: '1024x1024',
  quality: 'hd'
})
 
console.log(image.data[0].url)

Error Responses

Insufficient Credits

{
  "error": {
    "message": "Insufficient credits for image generation",
    "type": "insufficient_quota",
    "code": "insufficient_credits"
  }
}

Status Code: 429

Invalid Model

{
  "error": {
    "message": "Model not found or not supported for image generation",
    "type": "invalid_request_error",
    "code": "model_not_found"
  }
}

Status Code: 400

Best Practices

Choose the Right Format

  • Use OpenAI format for fast models (DALL-E)
  • Use Replicate format for slower, higher-quality models (Flux, SD3)

Handle Errors

try {
  const image = await client.images.generate({
    model: 'dall-e-3',
    prompt: 'A sunset'
  })
} catch (error) {
  if (error.status === 429) {
    console.error('Out of credits')
  } else if (error.status === 400) {
    console.error('Invalid request:', error.message)
  }
}

Estimate Costs

Check model pricing before generation:

// List image models with pricing
const models = await fetch(
  'https://hypery.ai/api/v1/models?type=image'
).then(r => r.json())
 
models.data.forEach(model => {
  console.log(`${model.id}: $${model.pricing?.imageCost || 'varies'}`)
})

Next Steps