Retrieves invoices or purchase bills as PDF files

Script xero Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 515 days ago
1
//native
2
type Xero = {
3
  token: string;
4
};
5
/**
6
 * Retrieves invoices or purchase bills as PDF files
7
 *
8
 */
9
export async function main(
10
  auth: Xero,
11
  InvoiceID: string,
12
  xero_tenant_id: string,
13
) {
14
  const url = new URL(
15
    `https://api.xero.com/api.xro/2.0/Invoices/${InvoiceID}/pdf`,
16
  );
17

18
  const response = await fetch(url, {
19
    method: "GET",
20
    headers: {
21
      Accept: 'application/json',
22
      "xero-tenant-id": xero_tenant_id,
23
      Authorization: "Bearer " + auth.token,
24
    },
25
    body: undefined,
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.text();
32
}
33