0

Upload transfer attachment

by
Published Oct 17, 2025

The *Upload transfer attachment* endpoint uploads an attachment and assigns it against a specific `transferId`. [Transfers](https://docs.codat.io/accounting-api#/schemas/Transfer) are issued by a supplier for the purpose of recording transfer. **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 transfer attachment
8
 * The *Upload transfer attachment* endpoint uploads an attachment and assigns it against a specific `transferId`.
9

10
[Transfers](https://docs.codat.io/accounting-api#/schemas/Transfer) are issued by a supplier for the purpose of recording transfer.
11

12
**Integration-specific behaviour**
13

14
For more details on supported file types by integration see [Attachments](https://docs.codat.io/accounting-api#/schemas/Attachment).
15
 */
16
export async function main(
17
	auth: Codat,
18
	companyId: string,
19
	connectionId: string,
20
	transferId: string,
21
	body: {
22
		file: {
23
			base64: Base64
24
			type:
25
				| 'image/png'
26
				| 'image/jpeg'
27
				| 'image/gif'
28
				| 'application/pdf'
29
				| 'appication/json'
30
				| 'text/csv'
31
				| 'text/plain'
32
				| 'audio/mpeg'
33
				| 'audio/wav'
34
				| 'video/mp4'
35
			name: string
36
		}
37
	}
38
) {
39
	const url = new URL(
40
		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/push/transfers/${transferId}/attachment`
41
	)
42

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

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