0

List transactions

by
Published Nov 5, 2024

Returns budget transactions, excluding any pending transactions

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

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

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

33
	if (!response.ok) {
34
		const text = await response.text()
35
		throw new Error(`${response.status} ${text}`)
36
	}
37

38
	return await response.json()
39
}
40