0

Upload bill credit note attachment

by
Published Oct 17, 2025

--- stoplight-id: c26f5b1b19168 --- The *Upload bill credit note attachment* endpoint uploads an attachment and assigns it against a specific `billCreditNoteId`. [Bill Credit Notes](https://docs.codat.io/accounting-api#/schemas/BillCreditNote) are issued by a supplier for the purpose of recording credit. **Integration-specific behaviour** For more details on supported file types by integration see [Attachments](https://docs.codat.io/accounting-api#/schemas/Attachment).

Script codat Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Codat = {
3
	encodedKey: string
4
}
5
type Base64 = string
6
/**
7
 * Upload bill credit note attachment
8
 * ---
9
stoplight-id: c26f5b1b19168
10
---
11

12
The *Upload bill credit note attachment* endpoint uploads an attachment and assigns it against a specific `billCreditNoteId`.
13

14
[Bill Credit Notes](https://docs.codat.io/accounting-api#/schemas/BillCreditNote) are issued by a supplier for the purpose of recording credit.
15

16
**Integration-specific behaviour**
17

18
For more details on supported file types by integration see [Attachments](https://docs.codat.io/accounting-api#/schemas/Attachment).
19

20
 */
21
export async function main(
22
	auth: Codat,
23
	companyId: string,
24
	connectionId: string,
25
	billCreditNoteId: string,
26
	body: {
27
		file: {
28
			base64: Base64
29
			type:
30
				| 'image/png'
31
				| 'image/jpeg'
32
				| 'image/gif'
33
				| 'application/pdf'
34
				| 'appication/json'
35
				| 'text/csv'
36
				| 'text/plain'
37
				| 'audio/mpeg'
38
				| 'audio/wav'
39
				| 'video/mp4'
40
			name: string
41
		}
42
	}
43
) {
44
	const url = new URL(
45
		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/push/billCreditNotes/${billCreditNoteId}/attachment`
46
	)
47

48
	const formData = new FormData()
49
	for (const [k, v] of Object.entries(body)) {
50
		if (v !== undefined) {
51
			if (['file'].includes(k)) {
52
				const { base64, type, name } = v as {
53
					base64: Base64
54
					type: string
55
					name: string
56
				}
57
				formData.append(
58
					k,
59
					new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
60
						type
61
					}),
62
					name
63
				)
64
			} else {
65
				formData.append(k, String(v))
66
			}
67
		}
68
	}
69

70
	const response = await fetch(url, {
71
		method: 'POST',
72
		headers: {
73
			Authorization: `Basic ${auth.encodedKey}`
74
		},
75
		body: formData
76
	})
77
	if (!response.ok) {
78
		const text = await response.text()
79
		throw new Error(`${response.status} ${text}`)
80
	}
81
	return await response.text()
82
}
83