Create Service Binding

Creates a new Service Binding, routing traffic to IPs within the given CIDR to a service running on Cloudflare's network. **Note:** This API may only be used on prefixes currently configured with a Magic Transit service binding, and only allows creating service bindings for the Cloudflare CDN or Cloudflare Spectrum.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create Service Binding
8
 * Creates a new Service Binding, routing traffic to IPs within the given CIDR to a service running on Cloudflare's network. 
9
**Note:** This API may only be used on prefixes currently configured with a Magic Transit service binding, and only allows creating service bindings for the Cloudflare CDN or Cloudflare Spectrum.
10

11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  account_identifier: string,
15
  prefix_identifier: string,
16
  body: { cidr?: string; service_id?: string; [k: string]: unknown }
17
) {
18
  const url = new URL(
19
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/addressing/prefixes/${prefix_identifier}/bindings`
20
  );
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      "X-AUTH-EMAIL": auth.email,
26
      "X-AUTH-KEY": auth.key,
27
      "Content-Type": "application/json",
28
      Authorization: "Bearer " + auth.token,
29
    },
30
    body: JSON.stringify(body),
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38