async function createCheckout(amountMicros: number, maxAttempts = 4): Promise<string> {
for (let attempt = 1; ; attempt++) {
const res = await fetch("https://api.opentype.dev/v1/billing/checkout", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENTYPE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ amount_micros: amountMicros }),
});
const data = await res.json();
if (res.ok) return data.checkout_url;
const { code, request_id } = data.error;
if (res.status < 500 || attempt >= maxAttempts) throw new Error(`${code} (${request_id})`);
await new Promise((r) => setTimeout(r, 2 ** attempt * 1000));
}
}
const url = await createCheckout(25_000_000); // $25; needs billing_write