Skip to main content
This example walks through the complete flow of creating a vendor invoice programmatically — from creating the invoice, adding line items, uploading a document, and submitting it for approval.

Complete Flow

const API_KEY = process.env.LIGHT_API_KEY;
const BASE_URL = 'https://api.light.inc';

const headers = {
  'Authorization': `Basic ${API_KEY}`,
  'Content-Type': 'application/json',
};

async function createInvoicePayable() {
  // 1. Create the invoice
  const invoiceRes = await fetch(`${BASE_URL}/v1/invoice-payables`, {
    method: 'POST',
    headers,
    body: JSON.stringify({
      vendorId: 'vnd_abc123',
      invoiceNumber: 'INV-2025-042',
      issueDate: '2025-01-15',
      dueDate: '2025-02-15',
      currency: 'GBP',
    }),
  });
  const invoice = await invoiceRes.json();
  console.log('Created invoice:', invoice.id);

  // 2. Add line items
  const lines = [
    { description: 'Software license', quantity: 1, unitPrice: 1200.00 },
    { description: 'Support contract', quantity: 1, unitPrice: 300.00 },
  ];

  for (const line of lines) {
    await fetch(`${BASE_URL}/v1/invoice-payables/${invoice.id}/lines`, {
      method: 'POST',
      headers,
      body: JSON.stringify(line),
    });
  }
  console.log('Added', lines.length, 'line items');

  // 3. Upload the invoice document
  const uploadUrlRes = await fetch(
    `${BASE_URL}/v1/invoice-payables/${invoice.id}/document/upload-url`,
    {
      method: 'POST',
      headers,
      body: JSON.stringify({
        fileName: 'invoice-042.pdf',
        contentType: 'application/pdf',
      }),
    }
  );
  const { uploadUrl } = await uploadUrlRes.json();

  // Upload the file to the presigned URL
  const fs = require('fs');
  const fileBuffer = fs.readFileSync('./invoice-042.pdf');
  await fetch(uploadUrl, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/pdf' },
    body: fileBuffer,
  });
  console.log('Uploaded document');

  // 4. Submit for approval
  await fetch(`${BASE_URL}/v1/invoice-payables/${invoice.id}/submit`, {
    method: 'POST',
    headers,
  });
  console.log('Submitted for approval');

  return invoice;
}

createInvoicePayable().catch(console.error);