invoice API for Node
Invoice API for Node
Create an invoice from a Node server with fetch. Keep the API key in an environment variable and return only the customer link to your app.
Base URL https://invoicetxt.com Auth Authorization: Bearer itxt_live_…
POST/v1/invoices
Create from a server route
Call the API from Node on the server. Do not ship the API key in a React or mobile bundle. Read it from the environment and pass the JSON body you already collected from your own form.
const response = await fetch('https://invoicetxt.com/v1/invoices', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.INVOICETXT_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
client: 'Oak Street Homes',
due: 'on receipt',
currency: 'USD',
items: [
{ description: 'Deep clean', quantity: 4, rate: 35 },
{ description: 'Oven clean', quantity: 1, rate: 45 }
]
})
});
if (!response.ok) throw new Error(await response.text());
const invoice = await response.json();
console.log(invoice.url);GET/v1/invoices/{id}/pdf
Save the PDF
The PDF endpoint returns bytes. Write them to disk with arrayBuffer. Use the invoice id from the create response, not a number you invented.
const pdf = await fetch(`https://invoicetxt.com/v1/invoices/${invoice.id}/pdf`, {
headers: { Authorization: `Bearer ${process.env.INVOICETXT_API_KEY}` }
});
const bytes = Buffer.from(await pdf.arrayBuffer());
await fs.writeFile(`invoice-${invoice.id}.pdf`, bytes);