0

List categories

by
Published Nov 5, 2024

Returns all categories grouped by category group. Amounts (budgeted, activity, balance, etc.) are specific to the current budget month (UTC).

Script ynab Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Ynab = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Ynab,
8
	budget_id: string,
9
	last_knowledge_of_server: string | undefined
10
) {
11
	const url = new URL(`https://api.ynab.com/v1/budgets/${budget_id}/categories`)
12

13
	for (const [k, v] of [['last_knowledge_of_server', last_knowledge_of_server]]) {
14
		if (v !== undefined && v !== '' && k !== undefined) {
15
			url.searchParams.append(k, v)
16
		}
17
	}
18

19
	const response = await fetch(url, {
20
		method: 'GET',
21
		headers: {
22
			Authorization: 'Bearer ' + auth.token
23
		},
24
		body: undefined
25
	})
26

27
	if (!response.ok) {
28
		const text = await response.text()
29
		throw new Error(`${response.status} ${text}`)
30
	}
31

32
	return await response.json()
33
}
34