0

List Contracts

by
Published today

Query contracts with optional filters. Returns up to 50 records per page; paginate with offset.

Script coupa Verified

The script

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

3
/**
4
 * List Contracts
5
 * Query contracts with optional filters. Returns up to 50 records per page; paginate with offset.
6
 */
7
export async function main(
8
  auth: RT.Coupa,
9
  supplier_name: string | undefined,
10
  updated_after: string | undefined,
11
  limit: number | undefined,
12
  offset: number | undefined,
13
  return_object: "limited" | "shallow" | undefined
14
) {
15
  const base = auth.instance_url.replace(/\/+$/, "")
16
  const url = new URL(`${base}/api/contracts`)
17
  const filters: { [key: string]: string | number | boolean | undefined } = {
18
    "supplier[name]": supplier_name,
19
    "updated_at[gt]": updated_after,
20
    limit,
21
    offset,
22
    return_object,
23
  }
24
  for (const [k, v] of Object.entries(filters)) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, String(v))
27
    }
28
  }
29

30
  const response = await fetch(url, {
31
    headers: {
32
      Authorization: `Bearer ${auth.token}`,
33
      Accept: "application/json",
34
    },
35
  })
36

37
  if (!response.ok) {
38
    throw new Error(`${response.status} ${await response.text()}`)
39
  }
40

41
  return await response.json()
42
}
43