0

Send Draft Order Invoice

by
Published Nov 8, 2023

Email a draft order's invoice (checkout link) to the customer.

Script shopify Verified

The script

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

3
/**
4
 * Send Draft Order Invoice
5
 * Email the draft order's invoice (a checkout link) to the customer. Leave to empty to use the draft order's email; subject and custom_message override the store's invoice template.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  draft_order_id: string,
10
  to: string | undefined,
11
  subject: string | undefined,
12
  custom_message: string | undefined,
13
  bcc: string[] | undefined
14
) {
15
  const id = draft_order_id.startsWith("gid://")
16
    ? draft_order_id
17
    : `gid://shopify/DraftOrder/${draft_order_id}`
18
  const response = await fetch(
19
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
20
    {
21
      method: "POST",
22
      headers: {
23
        "X-Shopify-Access-Token": auth.token,
24
        "Content-Type": "application/json",
25
        Accept: "application/json",
26
      },
27
      body: JSON.stringify({
28
        query: `mutation ($id: ID!, $email: EmailInput) { draftOrderInvoiceSend(id: $id, email: $email) { draftOrder { id name status email invoiceUrl invoiceSentAt } userErrors { field message } } }`,
29
        variables: {
30
          id,
31
          email: {
32
            to: to || undefined,
33
            subject: subject || undefined,
34
            customMessage: custom_message || undefined,
35
            bcc: bcc?.length ? bcc : undefined,
36
          },
37
        },
38
      }),
39
    }
40
  )
41

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

46
  const { data, errors } = await response.json()
47
  if (errors) {
48
    throw new Error(JSON.stringify(errors))
49
  }
50

51
  if (data.draftOrderInvoiceSend.userErrors.length) {
52
    throw new Error(JSON.stringify(data.draftOrderInvoiceSend.userErrors))
53
  }
54

55
  return data.draftOrderInvoiceSend
56
}
57

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Send an invoice
    7
     * Sends an invoice for the draft order.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      draft_order_id: string,
    13
      body: { draft_order_invoice?: { [k: string]: unknown }; [k: string]: unknown }
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/draft_orders/${draft_order_id}/send_invoice.json`
    17
      );
    18
    
    
    19
      const response = await fetch(url, {
    20
        method: "POST",
    21
        headers: {
    22
          "Content-Type": "application/json",
    23
          "X-Shopify-Access-Token": auth.token,
    24
        },
    25
        body: JSON.stringify(body),
    26
      });
    27
      if (!response.ok) {
    28
        const text = await response.text();
    29
        throw new Error(`${response.status} ${text}`);
    30
      }
    31
      return await response.json();
    32
    }
    33