AI Gateway Image Generation Quickstart
Generate an image through AI Gateway and save it to a file.
Paste this prompt into a coding agent with terminal access:
Add image generation through AI Gateway in the current environment. Use the AI Gateway skill for this task. If it is unavailable, run npx skills add vercel/vercel-plugin --skill ai-gateway, then find and read its SKILL.md before continuing. Reuse the environment's language, framework, package manager, and AI client when possible, choose an AI Gateway SDK or API that supports image generation, and add only required dependencies. Read AI_GATEWAY_API_KEY from the environment. If it is missing, run npx vercel@latest whoami and pause for login if needed. Determine the team, then run npx vercel@latest --scope <team-slug> ai-gateway api-keys create --name <descriptive-name>. Capture stdout directly into AI_GATEWAY_API_KEY for the request or existing ignored secret storage, and never expose the value. Use openai/gpt-image-2, save the first image to a file, run the result, and report the output.
Use Node.js 22.18 or later and a team with available AI Gateway Credits. Export AI_GATEWAY_API_KEY in your current shell. If you need a key, open the Create API Key dialog.
export AI_GATEWAY_API_KEY="your_ai_gateway_api_key"Install the AI SDK:
pnpm add ai@latestCreate index.mts:
import { generateImage } from 'ai';
import { writeFile } from 'node:fs/promises';
const { images } = await generateImage({
model: 'openai/gpt-image-2',
prompt: 'A turquoise hummingbird resting on a dew-covered branch at sunrise.',
});
await writeFile('output.png', images[0].uint8Array);
console.log('Saved output.png');Run the script:
node index.mts- Browse image generation models and learn when to use image-only or multimodal models in the image generation guide
- Learn how the AI SDK returns generated images
- Edit images with OpenAI image models
Was this helpful?