Category: Deep Art API
Section: Code examples
Contents
- Prerequisites
- Install dependencies
- Example script (Node.js)
- Run the script
- Notes & troubleshooting
- See also
Prerequisites
-
Node.js 18+ (or a version with global
fetchavailable; otherwise installnode-fetch). - A test image file (e.g.,
sample.jpg). - If an endpoint requires credentials in your setup, have your API key ready (see Get your API credentials).
Install dependencies
If you’re on Node < 18, install node-fetch and a base64 helper; for Node 18+ you can use built‑in fetch.
# optional for Node < 18 npm i node-fetch@3
Example script (Node.js)
Save as deeparteffects.js in the same folder as sample.jpg.
// deeparteffects.js
// Usage: node deeparteffects.js
// Set API_KEY in env if your endpoints require it (see NOTE below).
const fs = require('fs');
// For Node < 18 uncomment the next line and use fetch from node-fetch
// import fetch from 'node-fetch';
const BASE = 'https://api.deeparteffects.com/v1';
async function listStyles() {
const res = await fetch(`${BASE}/noauth/styles`);
if (!res.ok) throw new Error(`List styles failed: ${res.status}`);
return res.json();
}
async function uploadImage(styleId, imagePath, imageSize = 1024) {
const data = fs.readFileSync(imagePath);
const b64 = data.toString('base64');
const res = await fetch(`${BASE}/noauth/upload`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// NOTE: If your environment requires auth for this endpoint, add:
// 'x-api-key': process.env.API_KEY,
},
body: JSON.stringify({ styleId, imageBase64Encoded: b64, imageSize })
});
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
return res.json(); // { submissionId }
}
async function checkResult(submissionId) {
const url = `${BASE}/noauth/result?submissionId=${encodeURIComponent(submissionId)}`;
const res = await fetch(url);
if (!res.ok) throw new Error(`Check result failed: ${res.status}`);
return res.json(); // { status, url? }
}
async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
async function main() {
console.log('Listing styles...');
const styles = await listStyles();
const style = styles[0];
if (!style) throw new Error('No styles returned');
console.log('Using style:', style);
console.log('Uploading image...');
const { submissionId } = await uploadImage(style.id, 'sample.jpg', 1024);
console.log('Submission ID:', submissionId);
console.log('Polling result...');
let attempt = 0;
let result;
while (true) {
attempt++;
result = await checkResult(submissionId);
if (result.status === 'finished') break;
if (result.status === 'error') throw new Error('Processing error');
// simple backoff: 1s, 2s, 3s, max 5s
const wait = Math.min(1000 * attempt, 5000);
await sleep(wait);
}
console.log('Finished. Result URL:', result.url);
// Optionally download and save the image:
const imgRes = await fetch(result.url);
if (!imgRes.ok) throw new Error(`Download failed: ${imgRes.status}`);
const buf = Buffer.from(await imgRes.arrayBuffer());
fs.writeFileSync('result.png', buf);
console.log('Saved to result.png');
}
main().catch(err => {
console.error(err);
process.exit(1);
});
NOTE: The example uses the documented
noauthendpoints. If your environment requires authentication for specific calls, add the headerx-api-key: YOUR_KEY(for example viaprocess.env.API_KEY).
Run the script
node deeparteffects.js
You should see logs for Listing styles, Uploading image, Polling result, and a file result.png created on success.
Notes & troubleshooting
-
401/403 — Add your API key header if the endpoint you’re calling requires it; confirm the exact path (
/noauth/vs authenticated). - 429 — Back off your polling (increase wait times) or reduce concurrent jobs.
- Invalid base64 — Ensure you read the file as binary and convert to base64 without line breaks.
-
Large inputs — Try a smaller
imageSizefirst to validate your flow, then increase.
Comments
0 comments
Please sign in to leave a comment.