0

Duplicate a Form

by
Published Mar 25, 2024

Duplicates an existing form in your Typeform account and adds \"(copy)\" to the end of the title. [See the docs here](https://developer.typeform.com/create/reference/create-form/)

Script typeform Verified

The script

Submitted by hugo697 Bun
Verified 398 days ago
1
import { createClient } from '@typeform/api-client'
2

3
type Typeform = {
4
	token: string
5
	baseUrl: string
6
}
7

8
export async function main(resource: Typeform, formId: string) {
9
	const typeformAPI = createClient({
10
		token: resource.token,
11
		apiBaseUrl: resource.baseUrl
12
	})
13

14
	// // Get the form
15
	const form = await typeformAPI.forms.get({ uid: formId })
16

17
	if (!form.workspace) {
18
		throw new Error('Form not found')
19
	}
20

21
	// Create a copy of the form
22
	const newForm = await typeformAPI.forms.copy({
23
		uid: formId,
24
		workspaceHref: form.workspace!.href!
25
	})
26

27
	// Update the title of the new form
28
	await typeformAPI.forms.update({
29
		uid: newForm.id!,
30
		override: false,
31
		data: [
32
			{
33
				op: 'replace',
34
				path: '/title',
35
				value: `${form.title} (copy)`
36
			}
37
		]
38
	})
39

40
	newForm.title = `${form.title} (copy)`
41

42
	return newForm
43
}
44