0

List transactions in month, excluding any pending transactions

by
Published Nov 5, 2024

Returns all transactions for a specified month

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

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

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

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

39
	return await response.json()
40
}
41