0

Create a Bitlink

by
Published Apr 8, 2025

Converts a long url to a Bitlink and sets additional parameters. You may see errors returned from this endpoint - "BRANDED_LINK_MONTHLY_LIMIT_EXCEEDED" occurs if you have shortened more links than your account is configured for for the month, and "DNS_CONFIGURATION_ERROR" occurs if you are attempting to shorten links against a custom domain which doesn't have DNS properly configured.

Script bitly Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Bitly = {
3
  token: string;
4
};
5
/**
6
 * Create a Bitlink
7
 * Converts a long url to a Bitlink and sets additional parameters. You may see errors returned from this endpoint - "BRANDED_LINK_MONTHLY_LIMIT_EXCEEDED" occurs if you have shortened more links than your account is configured for for the month, and "DNS_CONFIGURATION_ERROR" occurs if you are attempting to shorten links against a custom domain which doesn't have DNS properly configured.
8
 */
9
export async function main(
10
  auth: Bitly,
11
  body: {
12
    long_url: string;
13
    domain?: string;
14
    group_guid?: string;
15
    title?: string;
16
    tags?: string[];
17
    deeplinks?: {
18
      app_id?: string;
19
      app_uri_path?: string;
20
      install_url?: string;
21
      install_type?: "no_install" | "auto_install" | "promote_install";
22
    }[];
23
  },
24
) {
25
  const url = new URL(`https://api-ssl.bitly.com/v4/bitlinks`);
26

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