Recipe: carousels
Create an AI carousel with the API, follow its job, download the PDF, then add, redraw, remove and move slides.
A carousel is a set of slides that LinkedIn shows as a swipeable document. Through the API you create an AI carousel from a prompt (or from your own slide texts), wait for its job, download the PDF, then change slides.
Creating a carousel costs 50 credits, held when you start and charged when the PDF is built. Adding or redrawing a slide costs 10 per slide credits, up to 50 per change, and only for slides that succeed. Removing and moving slides is free.
The flow:
- Optionally
GET /carousel-templatesto browse the built-in templates. POST .../carouselswith anIdempotency-Key. It answers202with ajobId.- Poll
GET /jobs/{jobId}or streamGET /jobs/{jobId}/eventsuntildone. GET .../carousels/{id}for the slides and a PDF link.POST .../carousels/{id}/changeswithopsto change slides. That is another job.
GET /carousel-templates lists the built-in templates (key, name, group, tone, recommendedSlides, sizes, isPremium and locked). Carousels created through the API are AI artwork carousels: you shape their look with stylePreset (photo, editorial_illustration, flat_vector, 3d_clay, line_art, watercolor, isometric, minimal, bold_poster, risograph, paper_cut, blueprint), size and useBrandKit. The create call does not take a template key.
Create options
| Field | Meaning |
|---|---|
prompt | What the carousel is about. Up to 50,000 characters. |
slides | Your own texts, one per slide, instead of a prompt. Needs at least 2. When you send them, the slides use your text as written. |
slideCount | 3 to 12, default 8. Used when you send a prompt. |
size | portrait (default, 1080 by 1350) or square. |
stylePreset | One of the presets above, or null. |
references | Up to 20 uploaded pictures: objects with an objectId (UUID) and a role of style, subject or logo. |
useBrandKit | true applies the workspace brand kit. |
Send prompt or slides. Only one carousel can be drawn at a time in an organization: a second create while one runs answers 409 carousel_in_progress.
The script
import { randomUUID } from "node:crypto" ;
const BASE = process .env .POSTCHAMP_BASE_URL ?? "https://api.postchampai.com" ;
const KEY = process .env .POSTCHAMP_API_KEY ;
const sleep = (ms ) => new Promise ((r ) => setTimeout (r , ms ));
async function api (method , path , { body , idempotencyKey , tries = 4 } = {}) {
for (let attempt = 1 ; ; attempt ++ ) {
let res ;
try {
res = await fetch (` ${ BASE } /api/v1 ${ path } ` , {
method ,
headers: {
"x-api-key" : KEY ,
... (body ? { "Content-Type" : "application/json" } : {}),
... (idempotencyKey ? { "Idempotency-Key" : idempotencyKey } : {}),
},
body: body ? JSON .stringify (body ) : undefined ,
});
} catch (err ) {
if (attempt >= tries ) throw err ;
await sleep (1000 * attempt );
continue ;
}
const json = await res .json ();
if (res .ok ) return json ;
const retryable = res .status === 429 || res .status >= 500 ||
(res .status === 409 && json .error ?.details ?.reason === "request_in_progress" );
if (retryable && attempt < tries ) {
await sleep ((Number (res .headers .get ("Retry-After" )) || 2 * attempt ) * 1000 );
continue ;
}
const e = new Error (` ${ res . status } ${ json . error ?. code } : ${ json . error ?. message } ` );
e .code = json .error ?.code ;
e .requestId = res .headers .get ("X-Request-Id" );
throw e ;
}
}
async function waitForJob (jobId , { intervalMs = 3000 , timeoutMs = 10 * 60_000 } = {}) {
const started = Date .now ();
while (Date .now () - started < timeoutMs ) {
const job = await api ("GET" , `/jobs/ ${ jobId } ` );
if (job .status === "done" ) return job ;
if (job .status === "failed" ) throw new Error (job .message ?? "The job failed" );
await sleep (intervalMs );
}
throw new Error ("Timed out waiting for the job" );
}
async function main () {
const { workspaces } = await api ("GET" , "/key" );
const ws = (workspaces .find ((w ) => w .isMain ) ?? workspaces [0 ]).id ;
// 1. Create. The idempotency key makes a retry return the same carousel.
const started = await api ("POST" , `/workspaces/ ${ ws } /carousels` , {
idempotencyKey: randomUUID (),
body: {
prompt: "5 habits of founders who ship weekly" ,
slideCount: 6 ,
size: "portrait" ,
stylePreset: "flat_vector" ,
},
});
console .log ("Queued" , started .id , started .jobId );
// 2. Wait for the PDF. (Or stream: see "Jobs and streaming".)
await waitForJob (started .jobId );
// 3. Read it.
const carousel = await api ("GET" , `/workspaces/ ${ ws } /carousels/ ${ started . id } ` );
console .log (` ${ carousel . slides . length } slides, PDF: ${ carousel . pdfUrl } ` );
console .log (`The link expires at ${ carousel . pdfUrlExpiresAt } ` ); // about 5 minutes: download now
// 4. Change it: redraw slide 2, add one at position 3, move the first slide to the end.
const [first , second ] = carousel .slides ;
const change = await api ("POST" , `/workspaces/ ${ ws } /carousels/ ${ carousel . id } /changes` , {
idempotencyKey: randomUUID (),
body: {
expectedRevision: carousel .revision , // refuse if someone else changed it meanwhile
ops: [
{ op: "redraw" , slideId: second .id , instruction: "Use a bolder headline" },
{ op: "add" , prompt: "Habit 6: write it down" , position: 3 },
{ op: "move" , slideId: first .id , toPosition: carousel .slides .length },
],
},
});
console .log (`Change queued: ${ change . paidSlides } slides to pay for` );
await waitForJob (change .jobId );
const updated = await api ("GET" , `/workspaces/ ${ ws } /carousels/ ${ carousel . id } ` );
console .log (`Now ${ updated . slides . length } slides at revision ${ updated . revision } ` );
}
main ().catch ((err ) => {
if (err .code === "carousel_in_progress" ) console .error ("Another carousel is being drawn. Try again soon." );
else if (err .code === "insufficient_credits" ) console .error ("Not enough credits." );
else console .error (err .message , err .requestId ? `(request ${ err . requestId } )` : "" );
process .exit (1 );
});
import { randomUUID } from "node:crypto";
const BASE = process.env.POSTCHAMP_BASE_URL ?? "https://api.postchampai.com";
const KEY = process.env.POSTCHAMP_API_KEY;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function api(method, path, { body, idempotencyKey, tries = 4 } = {}) {
for (let attempt = 1; ; attempt++) {
let res;
try {
res = await fetch(`${BASE}/api/v1${path}`, {
method,
headers: {
"x-api-key": KEY,
...(body ? { "Content-Type": "application/json" } : {}),
...(idempotencyKey ? { "Idempotency-Key": idempotencyKey } : {}),
},
body: body ? JSON.stringify(body) : undefined,
});
} catch (err) {
if (attempt >= tries) throw err;
await sleep(1000 * attempt);
continue;
}
const json = await res.json();
if (res.ok) return json;
const retryable = res.status === 429 || res.status >= 500 ||
(res.status === 409 && json.error?.details?.reason === "request_in_progress");
if (retryable && attempt < tries) {
await sleep((Number(res.headers.get("Retry-After")) || 2 * attempt) * 1000);
continue;
}
const e = new Error(`${res.status} ${json.error?.code}: ${json.error?.message}`);
e.code = json.error?.code;
e.requestId = res.headers.get("X-Request-Id");
throw e;
}
}
async function waitForJob(jobId, { intervalMs = 3000, timeoutMs = 10 * 60_000 } = {}) {
const started = Date.now();
while (Date.now() - started < timeoutMs) {
const job = await api("GET", `/jobs/${jobId}`);
if (job.status === "done") return job;
if (job.status === "failed") throw new Error(job.message ?? "The job failed");
await sleep(intervalMs);
}
throw new Error("Timed out waiting for the job");
}
async function main() {
const { workspaces } = await api("GET", "/key");
const ws = (workspaces.find((w) => w.isMain) ?? workspaces[0]).id;
// 1. Create. The idempotency key makes a retry return the same carousel.
const started = await api("POST", `/workspaces/${ws}/carousels`, {
idempotencyKey: randomUUID(),
body: {
prompt: "5 habits of founders who ship weekly",
slideCount: 6,
size: "portrait",
stylePreset: "flat_vector",
},
});
console.log("Queued", started.id, started.jobId);
// 2. Wait for the PDF. (Or stream: see "Jobs and streaming".)
await waitForJob(started.jobId);
// 3. Read it.
const carousel = await api("GET", `/workspaces/${ws}/carousels/${started.id}`);
console.log(`${carousel.slides.length} slides, PDF: ${carousel.pdfUrl}`);
console.log(`The link expires at ${carousel.pdfUrlExpiresAt}`); // about 5 minutes: download now
// 4. Change it: redraw slide 2, add one at position 3, move the first slide to the end.
const [first, second] = carousel.slides;
const change = await api("POST", `/workspaces/${ws}/carousels/${carousel.id}/changes`, {
idempotencyKey: randomUUID(),
body: {
expectedRevision: carousel.revision, // refuse if someone else changed it meanwhile
ops: [
{ op: "redraw", slideId: second.id, instruction: "Use a bolder headline" },
{ op: "add", prompt: "Habit 6: write it down", position: 3 },
{ op: "move", slideId: first.id, toPosition: carousel.slides.length },
],
},
});
console.log(`Change queued: ${change.paidSlides} slides to pay for`);
await waitForJob(change.jobId);
const updated = await api("GET", `/workspaces/${ws}/carousels/${carousel.id}`);
console.log(`Now ${updated.slides.length} slides at revision ${updated.revision}`);
}
main().catch((err) => {
if (err.code === "carousel_in_progress") console.error("Another carousel is being drawn. Try again soon.");
else if (err.code === "insufficient_credits") console.error("Not enough credits.");
else console.error(err.message, err.requestId ? `(request ${err.requestId})` : "");
process.exit(1);
});The carousel
GET .../carousels/{id} returns:
{
"id" : "4cbd208e-358a-54d6-ae41-7e1d253b8a2c" ,
"kind" : "ai_artwork" ,
"title" : "Placeholder text" ,
"size" : "portrait" ,
"width" : 1080 ,
"height" : 1350 ,
"status" : "ready" ,
"progress" : 100 ,
"revision" : 0 ,
"slideCount" : 3 ,
"error" : null ,
"hasPdf" : true ,
"jobId" : "carousel-build:4cbd208e-358a-54d6-ae41-7e1d253b8a2c:0" ,
"slides" : [
{
"id" : "0adac3e5-1750-4471-af5f-40628a7f19db" ,
"position" : 0 ,
"prompt" : "Hook: ship weekly" ,
"status" : "ready" ,
"url" : "https://...presigned image url..." ,
"drawing" : false ,
"lastError" : null
}
],
"pdfUrl" : "https://...presigned pdf url..." ,
"pdfUrlExpiresAt" : "2026-12-05T11:26:22.567Z"
}
{
"id": "4cbd208e-358a-54d6-ae41-7e1d253b8a2c",
"kind": "ai_artwork",
"title": "Placeholder text",
"size": "portrait",
"width": 1080,
"height": 1350,
"status": "ready",
"progress": 100,
"revision": 0,
"slideCount": 3,
"error": null,
"hasPdf": true,
"jobId": "carousel-build:4cbd208e-358a-54d6-ae41-7e1d253b8a2c:0",
"slides": [
{
"id": "0adac3e5-1750-4471-af5f-40628a7f19db",
"position": 0,
"prompt": "Hook: ship weekly",
"status": "ready",
"url": "https://...presigned image url...",
"drawing": false,
"lastError": null
}
],
"pdfUrl": "https://...presigned pdf url...",
"pdfUrlExpiresAt": "2026-12-05T11:26:22.567Z"
}The answer is shortened. status is ready when the PDF is built and failed when the build failed (error says why). pdfUrl and each slide url are presigned links that expire, the PDF link after about 5 minutes. Read the carousel again for a fresh link. pdfUrl is null until a PDF exists.
Change operations
ops takes 1 to 40 operations per change:
op | Fields | Cost |
|---|---|---|
add | prompt, optional position (0-based) | 10 per slide credits |
redraw | slideId, optional instruction, optional referenceObjectIds (up to 3) | 10 per slide credits |
remove | slideId | Free |
move | slideId, toPosition | Free |
A change answers 202 with id, jobId (it ends with the new revision), revision, the accepted ops, paidSlides and upTo. Credits are charged only for slides that succeed. expectedRevision is optional: with it, a carousel that changed since you read it answers 409 conflict with reason: "carousel_changed".
Errors and retries
- A failed job. The job’s
messagesays why, and the hold is released (“You weren’t charged”). Create the carousel again with a newIdempotency-Key: the old key replays the failed carousel. 409 carousel_in_progress. Wait for the running carousel’s job, then try again.409 carousel_locked. The carousel is on a post waiting for approval. Changes are paused.409 conflictwithalready_done. The change was already made. Read the carousel.- A lost response. Retry with the same key: you get the same carousel id and
Idempotent-Replayed: true.
See Errors for the full list.
- Stream a job instead of polling.
- Another long job.