Skip to content

Async jobs

Both generate routes return 202 and a poll_url. There is no callback.

01The pattern

  • POST generate returns 202, an id, and poll_url. Article costs 1.5 credits.
  • GET poll_url until is_complete is true, or status is failed.
  • GET the resource for the finished body, captions, or image_urls.

02Article

Poll GET /api/v1/articles/:id/status. Finished is cms or needs_editing. There is no published.

async function waitForArticle(apiKey, articleId) {
  const url = `https://www.brainpercent.app/api/v1/articles/${articleId}/status`;
  for (;;) {
    const res = await fetch(url, {
      headers: { Authorization: `Bearer ${apiKey}` },
    });
    const { data } = await res.json();
    if (data.status === 'failed') throw new Error(data.error || 'failed');
    if (data.is_complete) return data;
    await new Promise((r) => setTimeout(r, 15000));
  }
}

03Social

Poll GET /api/v1/social/content/:id. While it runs, platforms is an array. When it finishes, it is an object of captions.

04How often

Articles: every 15 seconds, for up to twenty minutes. Social: every 5 seconds, for a few minutes. Respect Retry-After if you hit 429.