0
Create Product & SKU
One script reply has been approved by the moderators Verified

Creating a new Product involves creating both a Product and a SKU, since a Product Item has to have, at minimum, a single SKU.

Created by hugo697 253 days ago Viewed 13510 times
0
Submitted by hugo697 Bun
Verified 253 days ago
1
//native
2
type Webflow = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Webflow,
8
	site_id: string,
9
	body: {
10
		publishStatus?: 'staging' | 'live'
11
		product?: {
12
			fieldData?: {
13
				name?: string
14
				slug?: string
15
				description?: string
16
				shippable?: false | true
17
				'sku-properties'?: {
18
					id: string
19
					name: string
20
					enum: { id: string; name: string; slug: string }[]
21
				}[]
22
				categories?: string[]
23
				'tax-category'?:
24
					| 'standard-taxable'
25
					| 'standard-exempt'
26
					| 'books-religious'
27
					| 'books-textbook'
28
					| 'clothing'
29
					| 'clothing-swimwear'
30
					| 'digital-goods'
31
					| 'digital-service'
32
					| 'drugs-non-prescription'
33
					| 'drugs-prescription'
34
					| 'food-bottled-water'
35
					| 'food-candy'
36
					| 'food-groceries'
37
					| 'food-prepared'
38
					| 'food-soda'
39
					| 'food-supplements'
40
					| 'magazine-individual'
41
					| 'magazine-subscription'
42
					| 'service-admission'
43
					| 'service-advertising'
44
					| 'service-dry-cleaning'
45
					| 'service-hairdressing'
46
					| 'service-installation'
47
					| 'service-miscellaneous'
48
					| 'service-parking'
49
					| 'service-printing'
50
					| 'service-professional'
51
					| 'service-repair'
52
					| 'service-training'
53
				'default-sku'?: string
54
				'ec-product-type'?:
55
					| 'ff42fee0113744f693a764e3431a9cc2'
56
					| 'f22027db68002190aef89a4a2b7ac8a1'
57
					| 'c599e43b1a1c34d5a323aedf75d3adf6'
58
					| 'b6ccc1830db4b1babeb06a9ac5f6dd76'
59
			} & {}
60
		}
61
		sku?: {
62
			id?: string
63
			cmsLocaleId?: string
64
			lastPublished?: string
65
			lastUpdated?: string
66
			createdOn?: string
67
			fieldData?: {
68
				'sku-values'?: {}
69
				name: string
70
				slug: string
71
				price: { value?: number; unit?: string }
72
				'compare-at-price'?: { value?: number; unit?: string }
73
				'ec-sku-billing-method'?: 'one-time' | 'subscription'
74
				'ec-sku-subscription-plan'?: {
75
					interval?: 'day' | 'week' | 'month' | 'year'
76
					frequency?: number
77
					trial?: number
78
					plans?: {
79
						platform?: 'stripe'
80
						id?: string
81
						status?: 'active' | 'inactive' | 'canceled'
82
					}[]
83
				}
84
				'track-inventory'?: false | true
85
				quantity?: number
86
			}
87
		}
88
	}
89
) {
90
	const url = new URL(`https://api.webflow.com/v2/sites/${site_id}/products`)
91

92
	const response = await fetch(url, {
93
		method: 'POST',
94
		headers: {
95
			'Content-Type': 'application/json',
96
			Authorization: 'Bearer ' + auth.token
97
		},
98
		body: JSON.stringify(body)
99
	})
100

101
	if (!response.ok) {
102
		const text = await response.text()
103
		throw new Error(`${response.status} ${text}`)
104
	}
105

106
	return await response.json()
107
}
108