Generate Images with OpenAI and Store Them on a CDN
You know the deal, your favorite AI provider drops a new model and you want to vibe to the max. You write a script to call the OpenAI API to create 5000 images, because you just got accepted into YC and have investor money to burn. OpenAI gladly accepts your requests (credits). But what now? Download the images manually and upload them to Google Photos? No, there's a better way!
Let's start with the script to generate these images.
import OpenAI from 'openai'
const openAiClient = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
// generate image(s) with Image API
const response = await openAiClient.images.generate({
model: 'gpt-image-1',
prompt: 'A beautiful sunset over a calm ocean. A godlike cat sitting on a throne vibing over the lake.',
n: 1, // simply change to 5000 for max credit burning
size: '1024x1024',
})
console.log(response)You can now check the logs of your script and manually save your files or write the buffer to your temp folder. But wait, there's a better way. You can now upload the generated images directly to easyCDN. That way you will receive a permalink to your newly generated asset, which you can store directly in your database.
Let's see how this looks like:
import { createClient } from '@easycdn/server'
import OpenAI from 'openai'
const easyCdnClient = createClient({
secretKey: process.env.EASYCDN_SECRET_KEY!,
})
const openAiClient = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
console.log('Generating images...')
// generate image(s) with Image API
const response = await openAiClient.images.generate({
model: 'gpt-image-1',
prompt: 'A beautiful sunset over a calm ocean. A godlike cat sitting on a throne vibing over the lake.',
n: 2,
size: '1024x1024',
})
console.log('Uploading images to easyCDN...')
// Upload all images to easyCDN
const uploadResponses = await easyCdnClient.uploadFromOpenAi(response)
console.log('Done! Uploaded assets:')
for (const uploadResponse of uploadResponses) {
console.log(`- ${uploadResponse.asset.name}: ${uploadResponse.asset.url}`)
}Note that it works with multiple image generations. It also works with the chat image generation tool use. Just replace the OpenAI call with:
const response = await openAiClient.responses.create({
model: 'gpt-5',
input: 'Generate an image of gray tabby cat hugging an otter with an orange scarf',
tools: [{ type: 'image_generation' }],
})While uploading to easyCDN, you can make use of image transformations or set expiry dates on the uploaded images. You can find all options in the docs.
As you can see, vibemaxing and uploading those vibes to easyCDN couldn't be simpler. You can now generate cat pics as avatars for all your users, create content for your website or whatever else you want. Enjoy vibemaxing and happy uploading!