0

CreateTerminalAction

by
Published Oct 17, 2025

Creates a Terminal action request and sends it to the specified device.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * CreateTerminalAction
7
 * Creates a Terminal action request and sends it to the specified device.
8
 */
9
export async function main(
10
  auth: Square,
11
  body: {
12
    idempotency_key: string;
13
    action: {
14
      id?: string;
15
      device_id?: string;
16
      deadline_duration?: string;
17
      status?: string;
18
      cancel_reason?: "BUYER_CANCELED" | "SELLER_CANCELED" | "TIMED_OUT";
19
      created_at?: string;
20
      updated_at?: string;
21
      app_id?: string;
22
      location_id?: string;
23
      type?:
24
        | "QR_CODE"
25
        | "PING"
26
        | "SAVE_CARD"
27
        | "SIGNATURE"
28
        | "CONFIRMATION"
29
        | "RECEIPT"
30
        | "DATA_COLLECTION"
31
        | "SELECT";
32
      qr_code_options?: {
33
        title: string;
34
        body: string;
35
        barcode_contents: string;
36
      };
37
      save_card_options?: {
38
        customer_id: string;
39
        card_id?: string;
40
        reference_id?: string;
41
      };
42
      signature_options?: {
43
        title: string;
44
        body: string;
45
        signature?: { image_type?: string; data?: string }[];
46
      };
47
      confirmation_options?: {
48
        title: string;
49
        body: string;
50
        agree_button_text: string;
51
        disagree_button_text?: string;
52
        decision?: { has_agreed?: false | true };
53
      };
54
      receipt_options?: {
55
        payment_id: string;
56
        print_only?: false | true;
57
        is_duplicate?: false | true;
58
      };
59
      data_collection_options?: {
60
        title: string;
61
        body: string;
62
        input_type: "EMAIL" | "PHONE_NUMBER";
63
        collected_data?: { input_text?: string };
64
      };
65
      select_options?: {
66
        title: string;
67
        body: string;
68
        options: { reference_id: string; title: string }[];
69
        selected_option?: { reference_id: string; title: string };
70
      };
71
      device_metadata?: {
72
        battery_percentage?: string;
73
        charging_state?: string;
74
        location_id?: string;
75
        merchant_id?: string;
76
        network_connection_type?: string;
77
        payment_region?: string;
78
        serial_number?: string;
79
        os_version?: string;
80
        app_version?: string;
81
        wifi_network_name?: string;
82
        wifi_network_strength?: string;
83
        ip_address?: string;
84
      };
85
      await_next_action?: false | true;
86
      await_next_action_duration?: string;
87
    };
88
  },
89
) {
90
  const url = new URL(`https://connect.squareup.com/v2/terminals/actions`);
91

92
  const response = await fetch(url, {
93
    method: "POST",
94
    headers: {
95
      "Content-Type": "application/json",
96
      Authorization: "Bearer " + auth.token,
97
    },
98
    body: JSON.stringify(body),
99
  });
100
  if (!response.ok) {
101
    const text = await response.text();
102
    throw new Error(`${response.status} ${text}`);
103
  }
104
  return await response.json();
105
}
106