0

Update a category

by
Published Nov 5, 2024

Update a 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
	body: {
11
		category: { name?: string; note?: string; category_group_id?: string }
12
	}
13
) {
14
	const url = new URL(`https://api.ynab.com/v1/budgets/${budget_id}/categories/${category_id}`)
15

16
	const response = await fetch(url, {
17
		method: 'PATCH',
18
		headers: {
19
			'Content-Type': 'application/json',
20
			Authorization: 'Bearer ' + auth.token
21
		},
22
		body: JSON.stringify(body)
23
	})
24

25
	if (!response.ok) {
26
		const text = await response.text()
27
		throw new Error(`${response.status} ${text}`)
28
	}
29

30
	return await response.json()
31
}
32