Skip to main content

When to Use Batch Endpoints

Batch endpoints are perfect for:
  • Processing multiple videos at once
  • Fetching all videos from a playlist or channel
  • Building content analysis pipelines
  • Reducing overhead compared to individual requests

Sync vs Async Processing

Item CountProcessingResponse
≤50 itemsSynchronousImmediate results
>50 itemsAsynchronousJob ID for polling

Synchronous Batch Example

For ≤50 items, you get immediate results:
const response = await fetch(
  'https://api.scriptbase.app/api/v1/transcripts/batch',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'sk_your_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      videoIds: ['dQw4w9WgXcQ', 'jNQXAC9IVRw'],
      format: 'text'
    })
  }
);

const { data } = await response.json();
console.log(data.results);

Asynchronous Batch Example

For >50 items, you receive a job ID:
// Step 1: Create the batch job
const response = await fetch(
  'https://api.scriptbase.app/api/v1/transcripts/batch',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'sk_your_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      playlistId: 'PLrAXtmErZgOeiKm4sgNOknGvNjby9efdf',
      format: 'text'
    })
  }
);

const { data } = await response.json();
const jobId = data.jobId;

// Step 2: Poll for completion
async function waitForJob(jobId) {
  while (true) {
    const jobResponse = await fetch(
      `https://api.scriptbase.app/api/v1/jobs/${jobId}`,
      { headers: { 'X-API-Key': 'sk_your_key' } }
    );
    
    const job = await jobResponse.json();
    
    if (job.data.status === 'completed') {
      return job.data.output;
    }
    
    if (job.data.status === 'failed') {
      throw new Error(job.data.error);
    }
    
    console.log(`Progress: ${job.data.progress}/${job.data.total}`);
    await new Promise(resolve => setTimeout(resolve, 2000));
  }
}

const results = await waitForJob(jobId);

Best Practices

Don’t poll too frequently. 2-5 second intervals are optimal.
Set a maximum wait time to avoid infinite loops:
const MAX_WAIT = 10 * 60 * 1000; // 10 minutes
const startTime = Date.now();

while (Date.now() - startTime < MAX_WAIT) {
  // Poll job
}
Some items may fail while others succeed. Check individual results.
Batch operations consume credits per item. Plan accordingly.

Processing Playlists

Get all transcripts from a playlist:
const response = await fetch(
  'https://api.scriptbase.app/api/v1/transcripts/batch',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'sk_your_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      playlistId: 'PLrAXtmErZgOeiKm4sgNOknGvNjby9efdf',
      format: 'json',
      limit: 100
    })
  }
);

Processing Channels

Get video metadata for all channel videos:
const response = await fetch(
  'https://api.scriptbase.app/api/v1/videos/batch',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'sk_your_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      channelId: 'UCuAXFkgsw1L7xaCfnd5JJOw',
      limit: 50
    })
  }
);