Skip to content

Bun

This guide walks you through integrating the CreativeEditor SDK (CE.SDK) Engine in a Node.js environment using Bun as the package manager and runtime. By the end of this guide, you’ll have a working Node.js script that loads a scene, modifies it, and exports it as an image using Bun’s fast runtime and bundling capabilities.

Who is This Guide For?

This guide is for developers who:

  • Need to perform image editing and scene manipulation programmatically in a Node.js environment using Bun.
  • Want to use CE.SDK’s Node.js package for automation or backend processing with a fast, modern runtime.

What You’ll Achieve

  • Install and configure CE.SDK Engine for Node.js using Bun.
  • Load and manipulate design scenes programmatically.
  • Export designs as PNG images without requiring a UI.

Prerequisites

Before getting started, ensure you have:

Step 1: Set Up Your Project

Initialize your project with bun:

Terminal window
bun init

Your project structure should look as follows:

/my-cesdk-bun-project
├── index.ts
├── bun.lockb
├── tsconfig.json
├── package.json
├── README.md

Step 2: Install CE.SDK for Node.js Using Bun

Run the following command to install the required packages:

Terminal window
# Install the CE.SDK Node.js package with Bun
bun add @cesdk/node

index.ts (TypeScript with Bun)

Modify your index.ts file to initialize CE.SDK Engine and process a scene using Bun:

import { writeFile } from 'fs/promises';
import CreativeEngine from '@cesdk/node';
const { MimeType } = CreativeEngine as any;
// Configuration for the engine
const config = {
license: 'YOUR_LICENSE_KEY', // Replace with a valid license key
baseURL: 'https://cdn.img.ly/packages/imgly/cesdk-node/1.51.0/assets',
};
// Initialize CE.SDK Engine
CreativeEngine.init(config).then(async engine => {
console.log('CE.SDK Engine initialized');
try {
// Load default assets
await engine.addDefaultAssetSources();
// Load a scene from a URL
await engine.scene.loadFromURL(
'https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_instagram_photo_1.scene',
);
// Find the first page in the scene
const [page] = engine.block.findByType('page');
// Export the scene as a PNG image
const blob = await engine.block.export(page, MimeType.Png);
const arrayBuffer = await blob.arrayBuffer();
// Save the exported image to the file system
await writeFile('./example-output.png', Buffer.from(arrayBuffer));
console.log('Export completed: example-output.png');
} catch (error) {
console.error('Error processing scene:', error);
} finally {
// Dispose of the engine to free resources
engine.dispose();
}
});

Step 3: Run the Script Using Bun

Once everything is set up, run your script using:

Terminal window
bun run index.ts

This will process the scene and generate an image file named example-output.png in your project directory.

Step 4: Optimize Your Bun Project

1. Using Bun’s Faster File System API

Bun has a built-in fs API that performs better than Node.js. Modify index.ts like this:

import { write } from 'bun';
import CreativeEngine from '@cesdk/node';
const { MimeType } = CreativeEngine;
const config = {
license: 'YOUR_LICENSE_KEY',
baseURL: 'https://cdn.img.ly/packages/imgly/cesdk-node/1.51.0/assets',
};
CreativeEngine.init(config).then(async engine => {
console.log('CE.SDK Engine initialized');
try {
await engine.addDefaultAssetSources();
await engine.scene.loadFromURL(
'https://cdn.img.ly/assets/demo/v1/ly.img.template/templates/cesdk_instagram_photo_1.scene',
);
const [page] = engine.block.findByType('page');
const blob = await engine.block.export(page, MimeType.Png);
const arrayBuffer = await blob.arrayBuffer();
await write('./example-output.png', Buffer.from(arrayBuffer));
console.log('Export completed: example-output.png');
} catch (error) {
console.error('Error processing scene:', error);
} finally {
engine.dispose();
}
});

2. Running the Bun Optimized Script

Terminal window
bun run index.ts

Troubleshooting & Common Errors

❌ Error: fetch is not defined

  • Bun natively supports fetch, so this error should not occur unless using an outdated version.

❌ Error: Invalid license key

  • Verify that your license key is correct and not expired.

❌ Error: Cannot find module '@cesdk/node'

  • Ensure that @cesdk/node is installed correctly using bun add @cesdk/node.

❌ Error: SyntaxError: Unexpected token 'import'

  • Ensure your script is named index.ts for TypeScript support in Bun.

Next Steps

Congratulations! You’ve successfully integrated CE.SDK Engine in Node.js using Bun. Next, explore: