0

Attach a file to contract

by
Published Oct 17, 2025

Attach a file to contract document. **Token scopes**: `contracts:write`

Script deel Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Base64 = string
3
/**
4
 * Attach a file to contract
5
 * Attach a file to contract document.
6
 **Token scopes**: `contracts:write`
7
 */
8
export async function main(auth: RT.Deel, contract_id: string, body: Body) {
9
	const url = new URL(`https://api.letsdeel.com/rest/v2/contracts/${contract_id}/documents`)
10

11
	const formData = new FormData()
12
	for (const [k, v] of Object.entries(body)) {
13
		if (v !== undefined && v !== '') {
14
			if (['file'].includes(k)) {
15
				const { base64, type, name } = v as {
16
					base64: Base64
17
					type: string
18
					name: string
19
				}
20
				formData.append(
21
					k,
22
					new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], { type }),
23
					name
24
				)
25
			} else {
26
				formData.append(k, String(v))
27
			}
28
		}
29
	}
30
	const response = await fetch(url, {
31
		method: 'POST',
32
		headers: {
33
			Authorization: 'Bearer ' + auth.apiKey
34
		},
35
		body: formData
36
	})
37
	if (!response.ok) {
38
		const text = await response.text()
39
		throw new Error(`${response.status} ${text}`)
40
	}
41
	return await response.json()
42
}
43

44
/* eslint-disable */
45
/**
46
 * This file was automatically generated by json-schema-to-typescript.
47
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
48
 * and run json-schema-to-typescript to regenerate this file.
49
 */
50

51
/**
52
 * This is the file you will upload in a multi-part form.
53
 */
54
export interface Body {
55
	/**
56
	 * Upload the file you want to attach to this entry.
57
	 */
58
	file?: {
59
		base64: Base64
60
		type:
61
			| 'image/png'
62
			| 'image/jpeg'
63
			| 'image/gif'
64
			| 'application/pdf'
65
			| 'appication/json'
66
			| 'text/csv'
67
			| 'text/plain'
68
			| 'audio/mpeg'
69
			| 'audio/wav'
70
			| 'video/mp4'
71
		name: string
72
	}
73
	[k: string]: unknown
74
}
75