0

Create direct cost

by
Published Oct 17, 2025

The *Create direct cost* endpoint creates a new [direct cost](https://docs.codat.io/lending-api#/schemas/DirectCost) for a given company's connection. [Direct costs](https://docs.codat.io/lending-api#/schemas/DirectCost) are business expenses that don't impact Accounts Payable. **Integration-specific behaviour** Required data may vary by integration. To see what data to post, first call [Get create direct cost model](https://docs.codat.io/lending-api#/operations/get-create-directCosts-model).

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 direct cost
7
 * The *Create direct cost* endpoint creates a new [direct cost](https://docs.codat.io/lending-api#/schemas/DirectCost) for a given company's connection.
8

9
[Direct costs](https://docs.codat.io/lending-api#/schemas/DirectCost) are business expenses that don't impact Accounts Payable.
10

11
**Integration-specific behaviour**
12

13
Required data may vary by integration. To see what data to post, first call [Get create direct cost model](https://docs.codat.io/lending-api#/operations/get-create-directCosts-model).
14
 */
15
export async function main(
16
	auth: Codat,
17
	companyId: string,
18
	connectionId: string,
19
	timeoutInMinutes: string | undefined,
20
	allowSyncOnPushComplete: string | undefined,
21
	body: {
22
		reference?: string
23
		note?: string
24
		contactRef?: { id: string; dataType?: 'customers' | 'suppliers' }
25
		issueDate: string
26
		currency: string
27
		currencyRate?: number
28
		lineItems: {
29
			description?: string
30
			unitAmount: number
31
			quantity: number
32
			discountAmount?: number
33
			discountPercentage?: number
34
			subTotal?: number
35
			taxAmount?: number
36
			totalAmount?: number
37
			accountRef?: { id?: string; name?: string }
38
			taxRateRef?: { id?: string; name?: string; effectiveTaxRate?: number }
39
			itemRef?: { id: string; name?: string }
40
			trackingCategoryRefs?: { id: string; name?: string }[]
41
			tracking?: {
42
				recordRefs: {
43
					id?: string
44
					dataType?: 'customers' | 'suppliers' | 'trackingCategories'
45
				}[]
46
				invoiceTo?: { id?: string; dataType?: string }
47
			}
48
		}[]
49
		paymentAllocations: {
50
			payment: {
51
				id?: string
52
				note?: string
53
				reference?: string
54
				accountRef?: { id?: string; name?: string }
55
				currency?: string
56
				currencyRate?: number
57
				paidOnDate?: string
58
				totalAmount?: number
59
			}
60
			allocation: {
61
				currency?: string
62
				currencyRate?: number
63
				allocatedOnDate?: string
64
				totalAmount?: number
65
			}
66
		}[]
67
		subTotal: number
68
		taxAmount: number
69
		totalAmount: number
70
		supplementalData?: { content?: {} }
71
	}
72
) {
73
	const url = new URL(
74
		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/push/directCosts`
75
	)
76
	for (const [k, v] of [
77
		['timeoutInMinutes', timeoutInMinutes],
78
		['allowSyncOnPushComplete', allowSyncOnPushComplete]
79
	]) {
80
		if (v !== undefined && v !== '' && k !== undefined) {
81
			url.searchParams.append(k, v)
82
		}
83
	}
84

85
	const response = await fetch(url, {
86
		method: 'POST',
87
		headers: {
88
			'Content-Type': 'application/json',
89
			Authorization: `Basic ${auth.encodedKey}`
90
		},
91
		body: JSON.stringify(body)
92
	})
93
	if (!response.ok) {
94
		const text = await response.text()
95
		throw new Error(`${response.status} ${text}`)
96
	}
97
	return await response.json()
98
}
99