0

Create Invoice

by
Published today

Create an invoice. Required: invoice-number, invoice-date, supplier, currency, invoice-lines; field names use dashes.

Script coupa Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Create Invoice
5
 * Create an invoice. Required: invoice-number, invoice-date, supplier, currency, invoice-lines; field names use dashes.
6
 */
7
export async function main(auth: RT.Coupa, body: { [key: string]: any }) {
8
  const base = auth.instance_url.replace(/\/+$/, "")
9
  const url = new URL(`${base}/api/invoices`)
10

11
  const response = await fetch(url, {
12
    method: "POST",
13
    headers: {
14
      Authorization: `Bearer ${auth.token}`,
15
      "Content-Type": "application/json",
16
      Accept: "application/json",
17
    },
18
    body: JSON.stringify(body),
19
  })
20

21
  if (!response.ok) {
22
    throw new Error(`${response.status} ${await response.text()}`)
23
  }
24

25
  if (response.status === 204) return { success: true }
26
  return await response.json()
27
}
28