Creates a customer
One script reply has been approved by the moderators Verified

Creates a customer.

Created by hugo697 883 days ago Picked 13 times
Submitted by hugo697 Typescript (fetch-only)
Verified 337 days ago
1
type Shopify = {
2
  token: string;
3
  store_name: string;
4
};
5
/**
6
 * Creates a customer
7
 * Creates a customer.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  body: {
13
    customer?: {
14
      addresses?: {
15
        address1?: string;
16
        city?: string;
17
        country?: string;
18
        first_name?: string;
19
        last_name?: string;
20
        phone?: string;
21
        province?: string;
22
        zip?: string;
23
        [k: string]: unknown;
24
      }[];
25
      email?: string;
26
      first_name?: string;
27
      last_name?: string;
28
      phone?: string;
29
      verified_email?: boolean;
30
      [k: string]: unknown;
31
    };
32
    [k: string]: unknown;
33
  }
34
) {
35
  const url = new URL(
36
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/customers.json`
37
  );
38

39
  const response = await fetch(url, {
40
    method: "POST",
41
    headers: {
42
      "Content-Type": "application/json",
43
      "X-Shopify-Access-Token": auth.token,
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53