0

Creates a consumer

by
Published Oct 17, 2025

Creates a new consumer for this account.

Script zuplo Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Zuplo = {
3
	apiKey: string
4
}
5
/**
6
 * Creates a consumer
7
 * Creates a new consumer for this account.
8
 */
9
export async function main(
10
	auth: Zuplo,
11
	accountName: string,
12
	bucketName: string,
13
	with_api_key: string | undefined,
14
	body: {
15
		name: string
16
		managers?: string[] | { email: string; sub: string }[]
17
		description?: string
18
		tags?: {}
19
		metadata?: {}
20
	}
21
) {
22
	const url = new URL(
23
		`https://dev.zuplo.com/v1/accounts/${accountName}/key-buckets/${bucketName}/consumers`
24
	)
25
	for (const [k, v] of [['with-api-key', with_api_key]]) {
26
		if (v !== undefined && v !== '' && k !== undefined) {
27
			url.searchParams.append(k, v)
28
		}
29
	}
30
	const response = await fetch(url, {
31
		method: 'POST',
32
		headers: {
33
			'Content-Type': 'application/json',
34
			Authorization: 'Bearer ' + auth.apiKey
35
		},
36
		body: JSON.stringify(body)
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44