Update a device registration

Specific attributes of an existing registration can be updated. Only clientId, metadata and push.recipient are mutable.

Script ably Verified

by hugo697 ยท 10/17/2025

The script

Submitted by hugo697 Bun
Verified 220 days ago
1
//native
2
type Ably = {
3
  apiKey: string;
4
};
5
/**
6
 * Update a device registration
7
 * Specific attributes of an existing registration can be updated. Only clientId, metadata and push.recipient are mutable.
8
 */
9
export async function main(
10
  auth: Ably,
11
  device_id: string,
12
  format: "json" | "jsonp" | "msgpack" | "html" | undefined,
13
  X_Ably_Version: string,
14
  body: {
15
    id?: string;
16
    clientId?: string;
17
    formFactor?:
18
      | "phone"
19
      | "tablet"
20
      | "desktop"
21
      | "tv"
22
      | "watch"
23
      | "car"
24
      | "embedded";
25
    metadata?: {};
26
    platform?: "ios" | "android" | "browser";
27
    deviceSecret?: string;
28
    "push.recipient"?: {
29
      transportType?: "apns" | "fcm" | "gcm" | "web";
30
      deviceToken?: string;
31
      registrationToken?: string;
32
      encryptionKey?: {};
33
      clientId?: string;
34
      deviceId?: string;
35
    };
36
    "push.state"?: "Active" | "Failing" | "Failed";
37
  },
38
) {
39
  const url = new URL(
40
    `https://rest.ably.io/push/deviceRegistrations/${device_id}`,
41
  );
42
  for (const [k, v] of [["format", format]]) {
43
    if (v !== undefined && v !== "" && k !== undefined) {
44
      url.searchParams.append(k, v);
45
    }
46
  }
47
  const response = await fetch(url, {
48
    method: "PATCH",
49
    headers: {
50
      "X-Ably-Version": X_Ably_Version,
51
      "Content-Type": "application/json",
52
      Authorization: "Bearer " + auth.apiKey,
53
    },
54
    body: JSON.stringify(body),
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62