0

Create a single scheduled transaction

by
Published Nov 5, 2024

Creates a single scheduled transaction (a transaction with a future date).

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
		scheduled_transaction: {
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
			flag_color?: 'red' | 'orange' | 'yellow' | 'green' | 'blue' | 'purple'
19
			frequency?:
20
				| 'never'
21
				| 'daily'
22
				| 'weekly'
23
				| 'everyOtherWeek'
24
				| 'twiceAMonth'
25
				| 'every4Weeks'
26
				| 'monthly'
27
				| 'everyOtherMonth'
28
				| 'every3Months'
29
				| 'every4Months'
30
				| 'twiceAYear'
31
				| 'yearly'
32
				| 'everyOtherYear'
33
		}
34
	}
35
) {
36
	const url = new URL(`https://api.ynab.com/v1/budgets/${budget_id}/scheduled_transactions`)
37

38
	const response = await fetch(url, {
39
		method: 'POST',
40
		headers: {
41
			'Content-Type': 'application/json',
42
			Authorization: 'Bearer ' + auth.token
43
		},
44
		body: JSON.stringify(body)
45
	})
46

47
	if (!response.ok) {
48
		const text = await response.text()
49
		throw new Error(`${response.status} ${text}`)
50
	}
51

52
	return await response.json()
53
}
54