easyCDN Logo

xAI Integration

Learn how to upload images from xAI Grok image generation directly to easyCDN with the Node.js SDK.

AI SDK

This guide uses the Vercel AI SDK with the xAI provider. See the official xAI documentation here.

Installation

Install the easyCDN Node.js SDK, the AI SDK, and the xAI provider:

bash
npm install @easycdn/server ai @ai-sdk/xai

Usage

typescript
import { createClient } from '@easycdn/server'
import { xai } from '@ai-sdk/xai'
import { generateImage } from 'ai'

const easyCdnClient = createClient({
  secretKey: process.env.EASYCDN_SECRET_KEY!,
})

// Generate image with xAI
const { image } = await generateImage({
  model: xai.image('grok-imagine-image'),
  prompt: 'A lake with a mountain in the background',
  aspectRatio: '16:9',
})

// Upload to easyCDN
const uploadedAsset = await easyCdnClient.upload(
  Buffer.from(image.base64, 'base64'),
  { fileName: 'xai-lake.png' },
)

console.log(`Uploaded: ${uploadedAsset.asset.url}`)

Extended Usage

You can also pass additional options to the upload() method, such as setting the expiration date and transforming the image. See the API reference for more details.

typescript
const uploadedAsset = await easyCdnClient.upload(
  Buffer.from(image.base64, 'base64'),
  {
    fileName: 'xai-lake.png',
    expiresAt: dayjs().add(1, 'day').toISOString(),
    transform: {
      image: {
        width: 642,
        format: 'webp',
      },
    },
  },
)

API Reference

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