Creates a new address for a customer

Creates a new address for 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
 * Creates a new address for a customer
7
 * Creates a new address for a customer.
8
 */
9
export async function main(
10
  auth: Shopify,
11
  api_version: string = "2023-10",
12
  customer_id: string,
13
  body: {
14
    address?: {
15
      address1?: string;
16
      address2?: string;
17
      city?: string;
18
      company?: string;
19
      country?: string;
20
      country_code?: string;
21
      country_name?: string;
22
      first_name?: string;
23
      last_name?: string;
24
      name?: string;
25
      phone?: string;
26
      province?: string;
27
      province_code?: string;
28
      zip?: string;
29
      [k: string]: unknown;
30
    };
31
    [k: string]: unknown;
32
  }
33
) {
34
  const url = new URL(
35
    `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/customers/${customer_id}/addresses.json`
36
  );
37

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