0

Retrieve a QR Code image

by
Published Apr 8, 2025

Get the image of a given QR Code.

Script bitly Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Bitly = {
3
  token: string;
4
};
5
/**
6
 * Retrieve a QR Code image
7
 * Get the image of a given QR Code.
8
 */
9
export async function main(
10
  auth: Bitly,
11
  qrcode_id: string,
12
  format: "svg" | "png" | undefined,
13
  Accept: string,
14
) {
15
  const url = new URL(
16
    `https://api-ssl.bitly.com/v4/qr-codes/${qrcode_id}/image`,
17
  );
18
  for (const [k, v] of [["format", format]]) {
19
    if (v !== undefined && v !== "" && k !== undefined) {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      Accept: Accept,
27
      Authorization: "Bearer " + auth.token,
28
    },
29
    body: undefined,
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37