0

Generate spend analysis report

by
Published Oct 17, 2025

Generate new spend analysis report (no body required in POST).

Script codat Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Codat = {
3
	encodedKey: string
4
}
5
/**
6
 * Generate spend analysis report
7
 * Generate new spend analysis report (no body required in POST).
8
 */
9
export async function main(
10
	auth: Codat,
11
	companyId: string,
12
	from: string | undefined,
13
	to: string | undefined,
14
	excludeSuppliersWithAnnualisedSpendBelow: string | undefined
15
) {
16
	const url = new URL(`https://banking-api.codat.io/companies/${companyId}/reports/spendAnalysis`)
17
	for (const [k, v] of [
18
		['from', from],
19
		['to', to],
20
		['excludeSuppliersWithAnnualisedSpendBelow', excludeSuppliersWithAnnualisedSpendBelow]
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: 'POST',
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