0

Create a single transaction or multiple transactions

by
Published Nov 5, 2024

Creates a single transaction or multiple transactions. If you provide a body containing a `transaction` object, a single transaction will be created and if you provide a body containing a `transactions` array, multiple transactions will be created. Scheduled transactions (transactions with a future date) cannot be created on this endpoint.

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
		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
			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
		} & { import_id?: string }
29
		transactions?: {
30
			account_id?: string
31
			date?: string
32
			amount?: number
33
			payee_id?: string
34
			payee_name?: string
35
			category_id?: string
36
			memo?: string
37
			cleared?: 'cleared' | 'uncleared' | 'reconciled'
38
			approved?: false | true
39
			flag_color?: 'red' | 'orange' | 'yellow' | 'green' | 'blue' | 'purple'
40
			subtransactions?: {
41
				amount: number
42
				payee_id?: string
43
				payee_name?: string
44
				category_id?: string
45
				memo?: string
46
			}[]
47
		} & { import_id?: string }[]
48
	}
49
) {
50
	const url = new URL(`https://api.ynab.com/v1/budgets/${budget_id}/transactions`)
51

52
	const response = await fetch(url, {
53
		method: 'POST',
54
		headers: {
55
			'Content-Type': 'application/json',
56
			Authorization: 'Bearer ' + auth.token
57
		},
58
		body: JSON.stringify(body)
59
	})
60

61
	if (!response.ok) {
62
		const text = await response.text()
63
		throw new Error(`${response.status} ${text}`)
64
	}
65

66
	return await response.json()
67
}
68