0

Upload files for a company

by
Published Oct 17, 2025

The *Upload files* endpoint uploads multiple files provided by the SMB to Codat. This may include personal identity documents, pitch decks, contracts, or files with accounting and banking data. Uploaded files must meet the following requirements: - Up to 20 files can be uploaded at a time. - PDF, XLS, XLSX, XLSB, CSV, DOC, DOCX, PPT, PPTX, JPEG, JPG, and PNG files can be uploaded. - Each file can be up to 10MB in size.

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 files for a company
8
 * The *Upload files* endpoint uploads multiple files provided by the SMB to Codat. This may include personal identity documents, pitch decks, contracts, or files with accounting and banking data.
9

10
Uploaded files must meet the following requirements:
11

12
- Up to 20 files can be uploaded at a time.
13
- PDF, XLS, XLSX, XLSB, CSV, DOC, DOCX, PPT, PPTX, JPEG, JPG, and PNG files can be uploaded.
14
- Each file can be up to 10MB in size.
15
 */
16
export async function main(
17
	auth: Codat,
18
	companyId: string,
19
	connectionId: string,
20
	body: {
21
		file: {
22
			base64: Base64
23
			type:
24
				| 'image/png'
25
				| 'image/jpeg'
26
				| 'image/gif'
27
				| 'application/pdf'
28
				| 'appication/json'
29
				| 'text/csv'
30
				| 'text/plain'
31
				| 'audio/mpeg'
32
				| 'audio/wav'
33
				| 'video/mp4'
34
			name: string
35
		}
36
	}
37
) {
38
	const url = new URL(
39
		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/files`
40
	)
41

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

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