Auth & Basic Usage
Learn how to authenticate and integrate the easyCDN Node.js SDK into your server-side applications with practical examples.
Authentication
The easyCDN Node.js SDK uses secret API keys for server-side authentication:
Security Warning
Secret keys provide full access to your easyCDN account. Never commit secret keys to version control, expose them in client-side code, or share them publicly.
Getting Your Secret Key
To get your secret API key:
- 1Log in to your easyCDN dashboard
- 2Navigate to your project settings
- 3Go to the "Developers" section
- 4Copy your Secret Key for server-side use
Key Permissions
Secret keys have full access to all API endpoints including file uploads, asset management, and account settings. Use them responsibly and rotate them regularly.
Note: Fine-grained API keys with specific permissions (read-only, upload-only, etc.) may be available in the future. If you need more granular access control, please submit a feature request to help us prioritize this feature.
Basic Usage
The easyCDN Node.js SDK provides a simple API for uploading and managing files from your server-side applications:
Client Initialization
First, create a client instance with your secret key:
import { createClient } from '@easycdn/server'
const client = createClient({
// use a .env file or similar to expose the key from the easyCDN dashboard
secretKey: process.env.EASYCDN_SECRET_KEY
})
Persist Files from Dropzone / React SDK
When files are uploaded from the React Dropzone component, they need to be persisted on the server:
// In your API route or server handler
export async function POST(request: Request) {
const { tempAssetId } = await request.json()
try {
const persistedAsset = await client.persist({
tempAssetId
})
return Response.json({
success: true,
asset: persistedAsset
})
} catch (error) {
return Response.json({
success: false,
error: error.message
}, { status: 500 })
}
}
Simple File Upload
Upload a file using a file path, Buffer, or stream:
// Upload from file path
const result = await client.upload('./image.jpg')
// Upload from Buffer
const buffer = Buffer.from('file content')
const result = await client.upload(buffer, { fileName: 'data.txt' })
// Upload from stream
import { createReadStream } from 'fs'
const stream = createReadStream('./video.mp4')
const result = await client.upload(stream, { fileName: 'video.mp4' })
console.log('File uploaded:', result)