import { readFile } from 'node:fs/promises';
import { basename } from 'node:path';
import { fileTypeFromBuffer } from 'file-type';
const baseUrl = process.env.CUTAI_BASE_URL;
const apiKey = process.env.CUTAI_API_KEY;
const filePath = '/path/to/reference.png';
const bytes = await readFile(filePath);
const detected = await fileTypeFromBuffer(bytes);
const contentType = detected?.mime ?? 'image/png';
const form = new FormData();
form.set('file', new File([bytes], basename(filePath), { type: contentType }));
form.set('width', '1024');
form.set('height', '1024');
const uploadResponse = await fetch(`${baseUrl}/api/public/uploads`, {
method: 'POST',
headers: { authorization: `Bearer ${apiKey}` },
body: form,
});
const { asset } = await uploadResponse.json();
if (!uploadResponse.ok) throw new Error('Upload failed');
const generationResponse = await fetch(`${baseUrl}/api/generations`, {
method: 'POST',
headers: {
authorization: `Bearer ${apiKey}`,
'content-type': 'application/json',
},
body: JSON.stringify({
generationType: 'image-to-image',
modelId: 'gpt-img2',
prompt: 'Keep the subject and turn it into a polished commercial poster',
inputAssetIds: [asset.id],
}),
});
const generation = await generationResponse.json();
if (!generationResponse.ok) throw new Error(generation.error?.message ?? 'Generation failed');
console.log(generation.batch.task.id);