0

List category transactions, excluding any pending transactions

by
Published Nov 5, 2024

Returns all transactions for a specified category

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
	category_id: string,
10
	since_date: string | undefined,
11
	type: 'uncategorized' | 'unapproved' | undefined,
12
	last_knowledge_of_server: string | undefined
13
) {
14
	const url = new URL(
15
		`https://api.ynab.com/v1/budgets/${budget_id}/categories/${category_id}/transactions`
16
	)
17

18
	for (const [k, v] of [
19
		['since_date', since_date],
20
		['type', type],
21
		['last_knowledge_of_server', last_knowledge_of_server]
22
	]) {
23
		if (v !== undefined && v !== '' && k !== undefined) {
24
			url.searchParams.append(k, v)
25
		}
26
	}
27

28
	const response = await fetch(url, {
29
		method: 'GET',
30
		headers: {
31
			Authorization: 'Bearer ' + auth.token
32
		},
33
		body: undefined
34
	})
35

36
	if (!response.ok) {
37
		const text = await response.text()
38
		throw new Error(`${response.status} ${text}`)
39
	}
40

41
	return await response.json()
42
}
43