0

Create Topic for a Kafka Cluster

by
Published Dec 20, 2024

To create a topic attached to a Kafka cluster, send a POST request to `/v2/databases/$DATABASE_ID/topics`. The result will be a JSON object with a `topic` key.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create Topic for a Kafka Cluster
7
 * To create a topic attached to a Kafka cluster, send a POST request to
8
`/v2/databases/$DATABASE_ID/topics`.
9

10
The result will be a JSON object with a `topic` key.
11

12
 */
13
export async function main(
14
  auth: Digitalocean,
15
  database_cluster_uuid: string,
16
  body: {
17
    name?: string;
18
    replication_factor?: number;
19
    partition_count?: number;
20
  } & {
21
    config?: {
22
      cleanup_policy?: "delete" | "compact" | "compact_delete";
23
      compression_type?:
24
        | "producer"
25
        | "gzip"
26
        | "snappy"
27
        | "Iz4"
28
        | "zstd"
29
        | "uncompressed";
30
      delete_retention_ms?: number;
31
      file_delete_delay_ms?: number;
32
      flush_messages?: number;
33
      flush_ms?: number;
34
      index_interval_bytes?: number;
35
      max_compaction_lag_ms?: number;
36
      max_message_bytes?: number;
37
      message_down_conversion_enable?: false | true;
38
      message_format_version?:
39
        | "0.8.0"
40
        | "0.8.1"
41
        | "0.8.2"
42
        | "0.9.0"
43
        | "0.10.0-IV0"
44
        | "0.10.0-IV1"
45
        | "0.10.1-IV0"
46
        | "0.10.1-IV1"
47
        | "0.10.1-IV2"
48
        | "0.10.2-IV0"
49
        | "0.11.0-IV0"
50
        | "0.11.0-IV1"
51
        | "0.11.0-IV2"
52
        | "1.0-IV0"
53
        | "1.1-IV0"
54
        | "2.0-IV0"
55
        | "2.0-IV1"
56
        | "2.1-IV0"
57
        | "2.1-IV1"
58
        | "2.1-IV2"
59
        | "2.2-IV0"
60
        | "2.2-IV1"
61
        | "2.3-IV0"
62
        | "2.3-IV1"
63
        | "2.4-IV0"
64
        | "2.4-IV1"
65
        | "2.5-IV0"
66
        | "2.6-IV0"
67
        | "2.7-IV0"
68
        | "2.7-IV1"
69
        | "2.7-IV2"
70
        | "2.8-IV0"
71
        | "2.8-IV1"
72
        | "3.0-IV0"
73
        | "3.0-IV1"
74
        | "3.1-IV0"
75
        | "3.2-IV0"
76
        | "3.3-IV0"
77
        | "3.3-IV1"
78
        | "3.3-IV2"
79
        | "3.3-IV3";
80
      message_timestamp_type?: "create_time" | "log_append_time";
81
      min_cleanable_dirty_ratio?: number;
82
      min_compaction_lag_ms?: number;
83
      min_insync_replicas?: number;
84
      preallocate?: false | true;
85
      retention_bytes?: number;
86
      retention_ms?: number;
87
      segment_bytes?: number;
88
      segment_jitter_ms?: number;
89
      segment_ms?: number;
90
    };
91
  },
92
) {
93
  const url = new URL(
94
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/topics`,
95
  );
96

97
  const response = await fetch(url, {
98
    method: "POST",
99
    headers: {
100
      "Content-Type": "application/json",
101
      Authorization: "Bearer " + auth.token,
102
    },
103
    body: JSON.stringify(body),
104
  });
105
  if (!response.ok) {
106
    const text = await response.text();
107
    throw new Error(`${response.status} ${text}`);
108
  }
109
  return await response.json();
110
}
111