Ensure your HTTP client already wraps requests with fetchWithPayment (see /quickstart) so
Coinbase x402 invoices are paid automatically when a job is queued.
1. Submit the text job
const baseUrl = process.env.HORIZON_BASE_URL ?? 'https://api.horizon.new/v1';
const textResponse = await fetchWithPayment(`${baseUrl}/generate/text`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
prompt: 'Write a 50-word welcome note for new Horizon users joining the beta.',
webhookUrl: 'https://example.com/webhooks/horizon/generation',
}),
});
const textJob = await textResponse.json();
console.log('job', textJob.jobId, textJob.statusUrl);
2. Handle synchronous responses
if (textJob.content) {
console.log('Ready-made copy', textJob.content);
// You still receive jobId for auditing, even when the response is synchronous.
}
3. Poll for completion
let status;
do {
status = await fetchWithPayment(textJob.statusUrl).then((res) => res.json());
if (status.state === 'processing') {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
} while (status.state === 'processing');
4. Persist the result
if (status.state !== 'succeeded') {
throw new Error(`Text generation failed: ${status.error?.code ?? 'unknown'}`);
}
console.log(status.result.content);
- Store the final copy alongside campaign identifiers in your system for reporting.
- Distribute the text to downstream channels (email, product copy, onboarding flows).
- If you queued multiple variants, tag them in your datastore to support later A/B tests.