Skip to main content
Ensure fetchWithPayment wraps your HTTP client so Coinbase x402 payments replay automatically.

1. Submit the audio job

const baseUrl = process.env.HORIZON_BASE_URL ?? 'https://api.horizon.new/v1';

const audioResponse = await fetchWithPayment(`${baseUrl}/generate/audio`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    prompt: 'Ambient synth bed with subtle percussion for onboarding screens.',
    webhookUrl: 'https://example.com/webhooks/horizon/generation',
  }),
});

const audioJob = await audioResponse.json();
console.log('audio job', audioJob.jobId, audioJob.statusUrl);

2. Handle synchronous completion

if (audioJob.status === 'completed' && audioJob.result) {
  console.log('Inline audio artifact', audioJob.result.artifacts?.[0]);
  // Deliver directly to your CMS or playback surface.
}

3. Poll for completion

let status;

do {
  status = await fetchWithPayment(audioJob.statusUrl).then((res) => res.json());
  if (status.state === 'processing') {
    await new Promise((resolve) => setTimeout(resolve, 2000));
  }
} while (status.state === 'processing');

if (status.state !== 'succeeded') {
  throw new Error(`Audio generation failed: ${status.error?.code ?? 'unknown'}`);
}

const artifacts = status.result.artifacts;

4. Package the deliverables

  • Persist generated audio URLs alongside campaign identifiers in your own system for easy lookup.
  • Trigger mastering or distribution workflows from your webhook listener when jobs finish.
  • Track usage via the returned jobId to reconcile payments and content lineage.