Sends an account invite to a customer

Sends an account invite to a customer.

Script shopify Verified

by hugo697 ยท 11/8/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Sends an account invite to a customer
7
 * Sends an account invite to a customer.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  customer_id: string,
13
  body: {
14
    customer_invite?: {
15
      bcc?: string[];
16
      custom_message?: string;
17
      from?: string;
18
      subject?: string;
19
      to?: string;
20
      [k: string]: unknown;
21
    };
22
    [k: string]: unknown;
23
  }
24
) {
25
  const url = new URL(
26
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/customers/${customer_id}/send_invite.json`
27
  );
28

29
  const response = await fetch(url, {
30
    method: "POST",
31
    headers: {
32
      "Content-Type": "application/json",
33
      "X-Shopify-Access-Token": auth.token,
34
    },
35
    body: JSON.stringify(body),
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43