0

List Envelopes

by
Published 21 days ago

Search envelopes in the account by date range, status, folder, recipient email, or full-text.

Script docusign Verified

The script

Submitted by hugo989 Bun
Verified 22 days ago
1
//native
2

3
/**
4
 * List Envelopes
5
 * Search envelopes in the account by date range, status, folder, recipient email, or full-text.
6
 */
7
export async function main(
8
  auth: RT.Docusign,
9
  from_date: string,
10
  to_date: string | undefined,
11
  status: string | undefined,
12
  folder_ids: string | undefined,
13
  email: string | undefined,
14
  search_text: string | undefined,
15
) {
16
  const url = new URL(
17
    `${auth.base_uri}/restapi/v2.1/accounts/${auth.account_id}/envelopes`,
18
  )
19
  url.searchParams.append("from_date", from_date)
20
  for (const [k, v] of [
21
    ["to_date", to_date],
22
    ["status", status],
23
    ["folder_ids", folder_ids],
24
    ["email", email],
25
    ["search_text", search_text],
26
  ] as const) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v)
29
    }
30
  }
31

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

40
  if (!response.ok) {
41
    throw new Error(`${response.status} ${await response.text()}`)
42
  }
43

44
  return await response.json()
45
}
46