Best Practices
Tips for building robust integrations.
Best Practices
1. Use Webhooks Over Polling
typescript
// ❌ Don't poll in a tight loop
while (status !== 'COMPLETED') {
await sleep(100);
status = await checkStatus(runId);
}
// ✅ Use webhooks
const run = await createRun({
input: { prompt: '...' },
webhook_url: 'https://yoursite.com/webhook'
});2. Implement Retry Logic
typescript
async function withRetryT>(fn: () => PromiseT>, maxRetries = 3): PromiseT> {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
await sleep(1000 * Math.pow(2, i)); // Exponential backoff
}
}
throw new Error('Max retries exceeded');
}3. Store API Keys Securely
typescript
// ❌ Never hardcode
const API_KEY = 'bnn_live_abc123';
// ✅ Use environment variables
const API_KEY = process.env.BANAN0_API_KEY;4. Handle Rate Limits Gracefully
typescript
const remaining = parseInt(response.headers.get('X-RateLimit-Remaining') || '0');
if (remaining < 5) {
console.warn('Approaching rate limit, slowing down...');
await sleep(5000);
}5. Use Test Keys in Development
bash
# Development
BANAN0_API_KEY=bnn_test_xxx
# Production
BANAN0_API_KEY=bnn_live_xxx6. Validate Webhook Signatures
Always verify the X-Banan0-Signature header before processing webhooks.
7. Handle Idempotency
Store run IDs and check for duplicates before creating new runs.