0

Update multiple transactions

by
Published Nov 5, 2024

Updates multiple transactions, by `id` or `import_id`.

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
	body: {
10
		transactions: { id?: string; import_id?: string } & {
11
			account_id?: string
12
			date?: string
13
			amount?: number
14
			payee_id?: string
15
			payee_name?: string
16
			category_id?: string
17
			memo?: string
18
			cleared?: 'cleared' | 'uncleared' | 'reconciled'
19
			approved?: false | true
20
			flag_color?: 'red' | 'orange' | 'yellow' | 'green' | 'blue' | 'purple'
21
			subtransactions?: {
22
				amount: number
23
				payee_id?: string
24
				payee_name?: string
25
				category_id?: string
26
				memo?: string
27
			}[]
28
		}[]
29
	}
30
) {
31
	const url = new URL(`https://api.ynab.com/v1/budgets/${budget_id}/transactions`)
32

33
	const response = await fetch(url, {
34
		method: 'PATCH',
35
		headers: {
36
			'Content-Type': 'application/json',
37
			Authorization: 'Bearer ' + auth.token
38
		},
39
		body: JSON.stringify(body)
40
	})
41

42
	if (!response.ok) {
43
		const text = await response.text()
44
		throw new Error(`${response.status} ${text}`)
45
	}
46

47
	return await response.json()
48
}
49