0

Create a new account

by
Published Nov 5, 2024

Creates a new account

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
		account: {
11
			name: string
12
			type:
13
				| 'checking'
14
				| 'savings'
15
				| 'cash'
16
				| 'creditCard'
17
				| 'lineOfCredit'
18
				| 'otherAsset'
19
				| 'otherLiability'
20
				| 'mortgage'
21
				| 'autoLoan'
22
				| 'studentLoan'
23
				| 'personalLoan'
24
				| 'medicalDebt'
25
				| 'otherDebt'
26
			balance: number
27
		}
28
	}
29
) {
30
	const url = new URL(`https://api.ynab.com/v1/budgets/${budget_id}/accounts`)
31

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

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

46
	return await response.json()
47
}
48