Node.js SDK
+ Pictify
Official Node.js library for server-side image generation.
About Integration
The official Pictify Node.js SDK provides a simple, typed interface for generating images from your Node.js applications. Supports both CommonJS and ES Modules, with full TypeScript definitions included.
Key Capabilities
Full TypeScript support
Promise-based API
Automatic retries
Stream support
Common Use Cases
Next.js API routes
Express.js backends
Serverless functions
Build-time generation
Integration Guide
Prerequisites
- Node.js 16 or higher
- A Pictify account with an API key
- A template created in Pictify
Install the SDK
Add the Pictify SDK to your project using npm, yarn, or pnpm.
# Using npm
npm install @pictify/sdk
# Using yarn
yarn add @pictify/sdk
# Using pnpm
pnpm add @pictify/sdkInitialize the Client
Create a Pictify client instance with your API key. Store your API key in environment variables for security.
// ES Modules
import { Pictify } from '@pictify/sdk';
// CommonJS
const { Pictify } = require('@pictify/sdk');
// Initialize the client
const pictify = new Pictify({
apiKey: process.env.PICTIFY_API_KEY
});Never hardcode your API key. Use environment variables or a secrets manager.
Generate Your First Image
Call the render method with your template ID and variables to generate an image.
const result = await pictify.render({
templateId: 'your-template-id',
variables: {
title: 'Hello World',
subtitle: 'Generated with Pictify',
backgroundColor: '#667eea'
},
format: 'png', // or 'jpg', 'webp'
width: 1200,
height: 630
});
console.log('Image URL:', result.imageUrl);
// https://cdn.pictify.io/renders/abc123.pngThe imageUrl is permanently hosted on Pictify's CDN. No need to download and re-upload.
Download Image as Buffer (Optional)
If you need the raw image data instead of a URL, use the download option.
const result = await pictify.render({
templateId: 'your-template-id',
variables: { title: 'My Image' },
format: 'png',
download: true // Returns buffer instead of URL
});
// Save to file
import fs from 'fs';
fs.writeFileSync('output.png', result.buffer);
// Or use with streams
const stream = await pictify.renderStream({
templateId: 'your-template-id',
variables: { title: 'Streamed Image' }
});
stream.pipe(fs.createWriteStream('streamed.png'));Batch Rendering
Generate multiple images efficiently with a single API call.
const items = [
{ title: 'Image 1', color: '#ff6b6b' },
{ title: 'Image 2', color: '#4ecdc4' },
{ title: 'Image 3', color: '#667eea' }
];
const results = await pictify.renderBatch({
templateId: 'your-template-id',
items: items.map(item => ({
variables: item
}))
});
results.forEach((result, i) => {
console.log(`Image ${i + 1}: ${result.imageUrl}`);
});Batch rendering is faster and more efficient than multiple individual requests.
Error Handling
Handle errors gracefully with try/catch and the SDK's error types.
import { Pictify, PictifyError } from '@pictify/sdk';
try {
const result = await pictify.render({
templateId: 'your-template-id',
variables: { title: 'Test' }
});
} catch (error) {
if (error instanceof PictifyError) {
console.error('Pictify error:', error.message);
console.error('Error code:', error.code);
// Handle specific errors
if (error.code === 'TEMPLATE_NOT_FOUND') {
// Template doesn't exist
} else if (error.code === 'RATE_LIMIT_EXCEEDED') {
// Back off and retry
}
} else {
throw error; // Re-throw unexpected errors
}
}Fast Install
Ready to build with Node.js SDK?
Get your API key in seconds and start generating images programmatically.