0

Aged debtors report

by
Published Oct 17, 2025

Returns aged debtors report for company that shows the total outstanding balance due from customers to the business over time.

Script codat Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Codat = {
3
	encodedKey: string
4
}
5
/**
6
 * Aged debtors report
7
 * Returns aged debtors report for company that shows the total outstanding balance due from customers to the business over time.
8
 */
9
export async function main(
10
	auth: Codat,
11
	companyId: string,
12
	reportDate: string | undefined,
13
	numberOfPeriods: string | undefined,
14
	periodLengthDays: string | undefined
15
) {
16
	const url = new URL(`https://api.codat.io/companies/${companyId}/reports/agedDebtor`)
17
	for (const [k, v] of [
18
		['reportDate', reportDate],
19
		['numberOfPeriods', numberOfPeriods],
20
		['periodLengthDays', periodLengthDays]
21
	]) {
22
		if (v !== undefined && v !== '' && k !== undefined) {
23
			url.searchParams.append(k, v)
24
		}
25
	}
26

27
	const response = await fetch(url, {
28
		method: 'GET',
29
		headers: {
30
			Authorization: `Basic ${auth.encodedKey}`
31
		},
32
		body: undefined
33
	})
34
	if (!response.ok) {
35
		const text = await response.text()
36
		throw new Error(`${response.status} ${text}`)
37
	}
38
	return await response.json()
39
}
40