easyCDN Logo

OpenAI Integration

Learn how to upload images from OpenAI Image API and Chat Completions with Image Generation directly to easyCDN with the Node.js SDK.

Installation

Install both the easyCDN Node.js SDK and the official OpenAI SDK:

bash
npm install @easycdn/server openai

Usage with Image API

typescript
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,
})

// generate image(s) with Image API
const response = await openAiClient.images.generate({
  model: 'gpt-image-1',
  prompt: 'A beautiful sunset over a calm ocean',
  n: 1,
  size: '1024x1024',
})

// Upload all images to easyCDN
const uploadedAssets = await easyCdnClient.uploadFromOpenAi(response)

console.log('Uploaded assets:')
for (const asset of uploadedAssets) {
  console.log(`- ${asset.asset.name}: ${asset.asset.url}`)
}

Usage with Chat Completions API

typescript
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,
})

// generate image(s) with Chat Completions API
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' }],
})

// Upload all images to easyCDN
const uploadedAssets = await easyCdnClient.uploadFromOpenAi(response)

console.log('Uploaded assets:')
for (const asset of uploadedAssets) {
  console.log(`- ${asset.asset.name}: ${asset.asset.url}`)
}

Extended Usage

You can also use the uploadFromOpenAi() method with additional options. You can give a custom name to each image, set a prefix for the filename, set the expiration date, and transform the image. The expiration date and transform options are the same as with normal uploads. See the API reference for more details.

typescript
const uploadedAssets = await easyCdnClient.uploadFromOpenAi(response, {
  fileNamePrefix: 'openai-sunset',
  expiresAt: dayjs().add(1, 'day').toISOString(),
  transform: {
    image: {
      width: 642,
      format: 'webp',
    },
  },
})

API Reference

For complete API documentation including all options (custom naming, transformations, expiration, progress tracking), see the uploadFromOpenAi() reference.