0

UpsertMerchantCustomAttribute

by
Published Oct 17, 2025

Creates or updates a [custom attribute]($m/CustomAttribute) for a merchant.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * UpsertMerchantCustomAttribute
7
 * Creates or updates a [custom attribute]($m/CustomAttribute) for a merchant.
8
 */
9
export async function main(
10
  auth: Square,
11
  merchant_id: string,
12
  key: string,
13
  body: {
14
    custom_attribute: {
15
      key?: string;
16
      value?: {};
17
      version?: number;
18
      visibility?:
19
        | "VISIBILITY_HIDDEN"
20
        | "VISIBILITY_READ_ONLY"
21
        | "VISIBILITY_READ_WRITE_VALUES";
22
      definition?: {
23
        key?: string;
24
        schema?: {};
25
        name?: string;
26
        description?: string;
27
        visibility?:
28
          | "VISIBILITY_HIDDEN"
29
          | "VISIBILITY_READ_ONLY"
30
          | "VISIBILITY_READ_WRITE_VALUES";
31
        version?: number;
32
        updated_at?: string;
33
        created_at?: string;
34
      };
35
      updated_at?: string;
36
      created_at?: string;
37
    };
38
    idempotency_key?: string;
39
  },
40
) {
41
  const url = new URL(
42
    `https://connect.squareup.com/v2/merchants/${merchant_id}/custom-attributes/${key}`,
43
  );
44

45
  const response = await fetch(url, {
46
    method: "POST",
47
    headers: {
48
      "Content-Type": "application/json",
49
      Authorization: "Bearer " + auth.token,
50
    },
51
    body: JSON.stringify(body),
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59