Skip to content
Dashboard

Workflows

End-to-end automation recipes for common content creation pipelines.

Article Pipeline

The most common workflow — generate an article, wait for completion, then create social media posts from the article. This pipeline covers the full lifecycle from topic to published social content.

const API_KEY = 'bp_your_key';
const headers = {
  'Authorization': `Bearer ${API_KEY}`,
  'Content-Type': 'application/json',
};

async function articlePipeline(topic, platforms = ['twitter', 'linkedin']) {
  // Step 1: Generate article
  console.log('Generating article...');
  const genRes = await fetch('https://brainpercent.app/api/v1/articles/generate', {
    method: 'POST',
    headers,
    body: JSON.stringify({ topic, tone: 'professional', length: 'long' }),
  });
  const { data: genData } = await genRes.json();
  const articleId = genData.article_id;

  // Step 2: Wait for article completion
  let article;
  while (true) {
    const statusRes = await fetch(
      `https://brainpercent.app/api/v1/articles/${articleId}/status`,
      { headers }
    );
    const { data: status } = await statusRes.json();
    if (status.status === 'draft' || status.status === 'published') {
      const articleRes = await fetch(
        `https://brainpercent.app/api/v1/articles/${articleId}`,
        { headers }
      );
      article = (await articleRes.json()).data;
      break;
    }
    if (status.status === 'failed') throw new Error('Article generation failed');
    await new Promise(r => setTimeout(r, 5000));
  }
  console.log(`Article ready: ${article.title}`);

  // Step 3: Generate social content from article
  const socialRes = await fetch('https://brainpercent.app/api/v1/social/generate', {
    method: 'POST',
    headers,
    body: JSON.stringify({
      source_url: `https://brainpercent.app/articles/${article.slug}`,
      platforms,
      tone: 'professional',
    }),
  });
  const { data: socialData } = await socialRes.json();

  // Step 4: Wait for social content
  const socialContent = [];
  for (const item of socialData.items) {
    while (true) {
      const res = await fetch(
        `https://brainpercent.app/api/v1/social/content/${item.id}`,
        { headers }
      );
      const content = (await res.json()).data;
      if (content.status !== 'generating') {
        socialContent.push(content);
        break;
      }
      await new Promise(r => setTimeout(r, 3000));
    }
  }

  // Step 5: Publish social content
  for (const content of socialContent) {
    await fetch('https://brainpercent.app/api/v1/social/publish', {
      method: 'POST',
      headers,
      body: JSON.stringify({
        content_id: content.id,
        platform: content.platform,
      }),
    });
    console.log(`Published to ${content.platform}`);
  }

  return { article, socialContent };
}

// Usage
articlePipeline('AI-Powered Content Marketing in 2026');

Content Calendar

Generate articles for each project, then schedule social posts. This workflow iterates through all published articles in a project and creates social content for each one.

async function contentCalendar(apiKey, projectId) {
  const headers = {
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
  };

  // Get project details
  const projectRes = await fetch(
    `https://brainpercent.app/api/v1/projects/${projectId}`,
    { headers }
  );
  const project = (await projectRes.json()).data;
  console.log(`Project: ${project.name}`);

  // Get all articles in the project
  const articlesRes = await fetch(
    `https://brainpercent.app/api/v1/articles?project_id=${projectId}&status=published&limit=100`,
    { headers }
  );
  const { data: articles } = await articlesRes.json();

  // Schedule social content for each article
  for (const article of articles) {
    const socialRes = await fetch('https://brainpercent.app/api/v1/social/generate', {
      method: 'POST',
      headers,
      body: JSON.stringify({
        source_url: `https://brainpercent.app/articles/${article.slug}`,
        platforms: ['twitter', 'linkedin'],
      }),
    });
    console.log(`Generated social content for: ${article.title}`);
  }
}

Credit-Aware Generation

Check your available credits before generating content to avoid failed requests. This pattern ensures you only start jobs you can afford.

async function creditAwareGenerate(apiKey, topics) {
  const headers = { 'Authorization': `Bearer ${apiKey}` };

  // Check available credits
  const creditsRes = await fetch('https://brainpercent.app/api/v1/user/credits', { headers });
  const { data: credits } = await creditsRes.json();
  const available = credits.available_credits;
  const costPerArticle = 40;
  const maxArticles = Math.floor(available / costPerArticle);

  console.log(`Available: ${available} credits (can generate ${maxArticles} articles)`);

  // Generate only what we can afford
  const toGenerate = topics.slice(0, maxArticles);
  const results = [];

  for (const topic of toGenerate) {
    const res = await fetch('https://brainpercent.app/api/v1/articles/generate', {
      method: 'POST',
      headers: { ...headers, 'Content-Type': 'application/json' },
      body: JSON.stringify({ topic }),
    });
    results.push(await res.json());
    console.log(`Started: ${topic}`);
  }

  if (topics.length > maxArticles) {
    console.warn(`Skipped ${topics.length - maxArticles} topics due to insufficient credits`);
  }

  return results;
}

Bulk Operations

Process multiple projects and their articles in bulk. This example demonstrates paginating through all active projects and fetching their article counts.

async function bulkGenerateForProjects(apiKey) {
  const headers = { 'Authorization': `Bearer ${apiKey}` };

  // Fetch all active projects
  let page = 1;
  const projects = [];
  while (true) {
    const res = await fetch(
      `https://brainpercent.app/api/v1/projects?status=active&page=${page}&limit=100`,
      { headers }
    );
    const { data, pagination } = await res.json();
    projects.push(...data);
    if (!pagination.has_more) break;
    page++;
  }

  console.log(`Found ${projects.length} active projects`);

  // Fetch articles per project
  for (const project of projects) {
    const articlesRes = await fetch(
      `https://brainpercent.app/api/v1/articles?project_id=${project.id}&limit=100`,
      { headers }
    );
    const { data: articles, pagination } = await articlesRes.json();
    console.log(`${project.name}: ${pagination.total} articles`);
  }
}

Next Steps