Documentation Index
Fetch the complete documentation index at: https://docs.pixcraft.es/llms.txt
Use this file to discover all available pages before exploring further.
Node.js Integration Guide
This guide shows how to use the PixCraft API from a Node.js application using the built-infetch API.
Installation
No additional packages needed — Node.js 18+ includesfetch natively. For older versions, install node-fetch:
npm install node-fetch
Basic usage
const PIXCRAFT_API_KEY = process.env.PIXCRAFT_API_KEY;
const BASE_URL = 'https://www.pixcraft.es/api/v1';
async function generatePattern(imageUrl, niche, widthCm, heightCm) {
const response = await fetch(`${BASE_URL}/generate`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${PIXCRAFT_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
image_url: imageUrl,
niche,
width_cm: widthCm,
height_cm: heightCm,
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`PixCraft API error: ${error.code} — ${error.message}`);
}
return response.json();
}
// Usage
const pattern = await generatePattern(
'https://example.com/sunset.jpg',
'mosaics',
100,
100
);
console.log(`Generated ${pattern.dimensions.total_units} tiles`);
Generate and save a PDF
import { writeFile } from 'fs/promises';
async function generateAndSavePdf(imageUrl, niche, widthCm, heightCm, outputPath) {
const response = await fetch(`${BASE_URL}/generate/pdf`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${PIXCRAFT_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
image_url: imageUrl,
niche,
width_cm: widthCm,
height_cm: heightCm,
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(`PixCraft API error: ${error.code}`);
}
const buffer = Buffer.from(await response.arrayBuffer());
await writeFile(outputPath, buffer);
console.log(`PDF saved to ${outputPath}`);
}
await generateAndSavePdf(
'https://example.com/photo.jpg',
'curtains',
200,
250,
'./pattern.pdf'
);
Process a local image (base64)
import { readFile } from 'fs/promises';
async function processLocalImage(filePath, niche, widthCm, heightCm) {
const imageBuffer = await readFile(filePath);
const base64 = imageBuffer.toString('base64');
const response = await fetch(`${BASE_URL}/generate`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${PIXCRAFT_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
image: base64,
niche,
width_cm: widthCm,
height_cm: heightCm,
}),
});
return response.json();
}
Error handling with retry for rate limits
async function callWithRetry(fn, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
if (error.message.includes('RATE_LIMIT_EXCEEDED') && attempt < maxRetries - 1) {
const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
console.log(`Rate limited. Retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
}
// Usage
const pattern = await callWithRetry(() =>
generatePattern('https://example.com/photo.jpg', 'lego', 50, 50)
);
Check usage before processing
async function checkUsage() {
const response = await fetch(`${BASE_URL}/usage`, {
headers: { 'Authorization': `Bearer ${PIXCRAFT_API_KEY}` },
});
return response.json();
}
const usage = await checkUsage();
if (usage.calls_remaining < 10) {
console.warn(`Low API credits: ${usage.calls_remaining} calls remaining`);
}
Express.js middleware example
import express from 'express';
const app = express();
app.use(express.json());
app.post('/api/pattern', async (req, res) => {
const { imageUrl, niche, width, height } = req.body;
try {
const pattern = await generatePattern(imageUrl, niche, width, height);
res.json(pattern);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000);