0

Create Envelope

by
Published 21 days ago

Create an envelope from a JSON envelope definition. Set status to 'sent' to send immediately, or 'created' to save as draft.

Script docusign Verified

The script

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

3
/**
4
 * Create Envelope
5
 * Create an envelope from a JSON envelope definition. Set status to "sent" to send immediately, or "created" to save as draft.
6
 */
7
export async function main(
8
  auth: RT.Docusign,
9
  envelope_definition: {
10
    emailSubject?: string
11
    emailBlurb?: string
12
    status?: "sent" | "created"
13
    documents?: unknown[]
14
    recipients?: unknown
15
    templateId?: string
16
    templateRoles?: unknown[]
17
    [key: string]: unknown
18
  },
19
) {
20
  const url = new URL(
21
    `${auth.base_uri}/restapi/v2.1/accounts/${auth.account_id}/envelopes`,
22
  )
23

24
  const response = await fetch(url, {
25
    method: "POST",
26
    headers: {
27
      Authorization: `Bearer ${auth.token}`,
28
      "Content-Type": "application/json",
29
      Accept: "application/json",
30
    },
31
    body: JSON.stringify(envelope_definition),
32
  })
33

34
  if (!response.ok) {
35
    throw new Error(`${response.status} ${await response.text()}`)
36
  }
37

38
  return await response.json()
39
}
40