0

Create expense-transactions

by
Published Oct 17, 2025

Create an expense transaction

Script codat Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Codat = {
3
	encodedKey: string
4
}
5
/**
6
 * Create expense-transactions
7
 * Create an expense transaction
8
 */
9
export async function main(
10
	auth: Codat,
11
	companyId: string,
12
	body: {
13
		items?: {
14
			id: string
15
			type:
16
				| 'Payment'
17
				| 'Refund'
18
				| 'Reward'
19
				| 'Chargeback'
20
				| 'TransferIn'
21
				| 'TransferOut'
22
				| 'AdjustmentIn'
23
				| 'AdjustmentOut'
24
			issueDate: string
25
			currency: string
26
			currencyRate?: number
27
			contactRef?: { id?: string; contactType?: 'Supplier' }
28
			merchantName?: string
29
			lines?: {
30
				netAmount: number
31
				taxAmount: number
32
				taxRateRef?: { id?: string }
33
				accountRef: { id?: string }
34
				trackingRefs?: { id?: string }[]
35
			}[]
36
			notes?: string
37
		}[]
38
	}
39
) {
40
	const url = new URL(
41
		`https://api.codat.io/companies/${companyId}/sync/expenses/data/expense-transactions`
42
	)
43

44
	const response = await fetch(url, {
45
		method: 'POST',
46
		headers: {
47
			'Content-Type': 'application/json',
48
			Authorization: `Basic ${auth.encodedKey}`
49
		},
50
		body: JSON.stringify(body)
51
	})
52
	if (!response.ok) {
53
		const text = await response.text()
54
		throw new Error(`${response.status} ${text}`)
55
	}
56
	return await response.json()
57
}
58