0

CreateDeviceCode

by
Published Oct 17, 2025

Creates a DeviceCode that can be used to login to a Square Terminal device to enter the connected terminal mode.

Script square Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Square = {
3
  token: string;
4
};
5
/**
6
 * CreateDeviceCode
7
 * Creates a DeviceCode that can be used to login to a Square Terminal device to enter the connected
8
terminal mode.
9
 */
10
export async function main(
11
  auth: Square,
12
  body: {
13
    idempotency_key: string;
14
    device_code: {
15
      id?: string;
16
      name?: string;
17
      code?: string;
18
      device_id?: string;
19
      product_type: "TERMINAL_API";
20
      location_id?: string;
21
      status?: "UNKNOWN" | "UNPAIRED" | "PAIRED" | "EXPIRED";
22
      pair_by?: string;
23
      created_at?: string;
24
      status_changed_at?: string;
25
      paired_at?: string;
26
    };
27
  },
28
) {
29
  const url = new URL(`https://connect.squareup.com/v2/devices/codes`);
30

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