0

Updates an existing transaction

by
Published Nov 5, 2024

Updates a single transaction

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

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

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

48
	return await response.json()
49
}
50