0

Create journal

by
Published Oct 17, 2025

The *Create journal* endpoint creates a new [journal](https://docs.

Script codat Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Codat = {
3
	encodedKey: string
4
}
5
/**
6
 * Create journal
7
 * The *Create journal* endpoint creates a new [journal](https://docs.
8
 */
9
export async function main(
10
	auth: Codat,
11
	companyId: string,
12
	connectionId: string,
13
	timeoutInMinutes: string | undefined,
14
	body: {
15
		journalCode?: string
16
		name?: string
17
		type?: string
18
		parentId?: string
19
		hasChildren?: false | true
20
		createdOn?: string
21
		status?: 'Unknown' | 'Active' | 'Archived'
22
	}
23
) {
24
	const url = new URL(
25
		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/push/journals`
26
	)
27
	for (const [k, v] of [['timeoutInMinutes', timeoutInMinutes]]) {
28
		if (v !== undefined && v !== '' && k !== undefined) {
29
			url.searchParams.append(k, v)
30
		}
31
	}
32

33
	const response = await fetch(url, {
34
		method: 'POST',
35
		headers: {
36
			'Content-Type': 'application/json',
37
			Authorization: `Basic ${auth.encodedKey}`
38
		},
39
		body: JSON.stringify(body)
40
	})
41
	if (!response.ok) {
42
		const text = await response.text()
43
		throw new Error(`${response.status} ${text}`)
44
	}
45
	return await response.json()
46
}
47