0

Download purchase order attachment

by
Published Oct 17, 2025

The *Download purchase order attachment* endpoint downloads a specific attachment for a given `purchaseOrderId` and `attachmentId`. [Purchase Orders](https://docs.codat.io/accounting-api#/schemas/PurchaseOrder) represent a business's intent to purchase goods or services from a supplier.

Script codat Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Codat = {
3
	encodedKey: string
4
}
5
/**
6
 * Download purchase order attachment
7
 * The *Download purchase order attachment* endpoint downloads a specific attachment for a given `purchaseOrderId` and `attachmentId`.
8

9
[Purchase Orders](https://docs.codat.io/accounting-api#/schemas/PurchaseOrder) represent a business's intent to purchase goods or services from a supplier.
10
 */
11
export async function main(
12
	auth: Codat,
13
	companyId: string,
14
	connectionId: string,
15
	purchaseOrderId: string,
16
	attachmentId: string
17
) {
18
	const url = new URL(
19
		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/data/purchaseOrders/${purchaseOrderId}/attachments/${attachmentId}/download`
20
	)
21

22
	const response = await fetch(url, {
23
		method: 'GET',
24
		headers: {
25
			Authorization: `Basic ${auth.encodedKey}`
26
		},
27
		body: undefined
28
	})
29
	if (!response.ok) {
30
		const text = await response.text()
31
		throw new Error(`${response.status} ${text}`)
32
	}
33
	return await response.text()
34
}
35