Skip to main content

Batch generation

TypeScript
import { BabySea, BabySeaError } from 'babysea-sdk';

const client = new BabySea({ apiKey: 'bye_...', region: 'us', maxRetries: 3 });

const prompts = [
  'A mountain landscape at dawn',
  'An underwater coral reef',
  'A futuristic city skyline',
];

const results = await Promise.allSettled(
  prompts.map((prompt) =>
    client.generate('google/nano-banana', {
      generation_prompt: prompt,
      generation_ratio: '16:9',
      generation_output_format: 'webp',
    }),
  ),
);

for (const [i, result] of results.entries()) {
  if (result.status === 'fulfilled') {
    console.log(`✓ "${prompts[i]}" → ${result.value.data.generation_id}`);
  } else {
    const err = result.reason;
    if (err instanceof BabySeaError) {
      console.log(`✗ "${prompts[i]}" → ${err.code}: ${err.message}`);
    }
  }
}

Cost estimate before generation

TypeScript
const estimate = await client.estimate('google/nano-banana', 3);

if (!estimate.data.credit_balance_can_afford) {
  console.log(
    `Not enough credits. Can afford at most ${estimate.data.credit_balance_max_affordable} generations.`,
  );
  process.exit(1);
}

const result = await client.generate('google/nano-banana', {
  generation_prompt: 'A cute baby seal',
});

Credit balance check

TypeScript
const billing = await client.billing();

if (billing.data.billing_credit_balance < 1) {
  console.log('No credits remaining. Top up at https://us.babysea.ai or https://eu.babysea.ai');
  process.exit(1);
}

const result = await client.generate('google/nano-banana', {
  generation_prompt: 'A cute baby seal',
});

Polling for results

Use webhooks when possible. If you need polling instead:
TypeScript
const gen = await client.generate('google/nano-banana', {
  generation_prompt: 'A beautiful sunset',
});

let generation;
do {
  await new Promise((r) => setTimeout(r, 2000));
  const res = await client.getGeneration(gen.data.generation_id);
  generation = res.data;
} while (generation.generation_status === 'processing');

if (generation.generation_status === 'succeeded') {
  console.log('Output:', generation.generation_output_file);
} else {
  console.log('Failed:', generation.generation_error);
}

Paginating all generations

TypeScript
const allGenerations = [];
let offset = 0;
const limit = 100;

while (true) {
  const res = await client.listGenerations({ limit, offset });
  allGenerations.push(...res.data.generations);

  if (allGenerations.length >= res.total) break;
  offset += limit;
}

console.log(`Fetched ${allGenerations.length} generations`);

Input file (img2img)

Pass public file URLs to models that support generation_input_file:
TypeScript
const res = await client.generate('google/nano-banana', {
  generation_prompt: 'In the style of a watercolor painting',
  generation_input_file: [
    'https://example.com/input.jpg',
  ],
});
Check client.library.models() to see which models support generation_input_file.