0

List Campaigns

by
Published Apr 8, 2025

Retrieve a list of campaigns associated with a supplied `brandId`.

Script telnyx Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Telnyx = {
3
	apiKey: string
4
}
5
/**
6
 * List Campaigns
7
 * Retrieve a list of campaigns associated with a supplied `brandId`.
8
 */
9
export async function main(
10
	auth: Telnyx,
11
	brandId: string | undefined,
12
	page: string | undefined,
13
	recordsPerPage: string | undefined,
14
	sort:
15
		| 'assignedPhoneNumbersCount'
16
		| '-assignedPhoneNumbersCount'
17
		| 'campaignId'
18
		| '-campaignId'
19
		| 'createdAt'
20
		| '-createdAt'
21
		| 'status'
22
		| '-status'
23
		| 'tcrCampaignId'
24
		| '-tcrCampaignId'
25
		| undefined
26
) {
27
	const url = new URL(`https://api.telnyx.com/v2/campaign`)
28
	for (const [k, v] of [
29
		['brandId', brandId],
30
		['page', page],
31
		['recordsPerPage', recordsPerPage],
32
		['sort', sort]
33
	]) {
34
		if (v !== undefined && v !== '' && k !== undefined) {
35
			url.searchParams.append(k, v)
36
		}
37
	}
38
	const response = await fetch(url, {
39
		method: 'GET',
40
		headers: {
41
			Authorization: 'Bearer ' + auth.apiKey
42
		},
43
		body: undefined
44
	})
45
	if (!response.ok) {
46
		const text = await response.text()
47
		throw new Error(`${response.status} ${text}`)
48
	}
49
	return await response.json()
50
}
51