0

Create Report

by
Published Sep 27, 2024

Create new report of a form.

Script jotform Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 621 days ago
1
type Jotform = {
2
	apiKey: string
3
	baseUrl: string
4
}
5

6
export async function main(
7
	resource: Jotform,
8
	formId: string,
9
	reportData: {
10
		title: string
11
		list_type: 'excel' | 'csv' | 'grid' | 'table' | 'calendar' | 'rss' | 'visual'
12
	}
13
) {
14
	const queryParams = new URLSearchParams({ apiKey: resource.apiKey })
15

16
	const endpoint = `${resource.baseUrl}/form/${formId}/reports?${queryParams.toString()}`
17

18
	// Construct the URLSearchParams body
19
	const bodyParams = new URLSearchParams({
20
		title: reportData.title,
21
		list_type: reportData.list_type
22
	})
23

24
	const response = await fetch(endpoint, {
25
		method: 'POST',
26
		headers: {
27
			'Content-Type': 'application/x-www-form-urlencoded'
28
		},
29
		body: bodyParams.toString()
30
	})
31

32
	if (!response.ok) {
33
		throw new Error(`HTTP error! status: ${response.status}`)
34
	}
35

36
	const data = await response.json()
37

38
	return data
39
}
40