0

Create SKU

by
Published Nov 5, 2024

Create additional SKUs to cover every variant of your Product.

Script webflow Verified

The script

Submitted by hugo697 Bun
Verified 581 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
	product_id: string,
10
	body: {
11
		publishStatus?: 'staging' | 'live'
12
		skus: {
13
			id?: string
14
			cmsLocaleId?: string
15
			lastPublished?: string
16
			lastUpdated?: string
17
			createdOn?: string
18
			fieldData?: {
19
				'sku-values'?: {}
20
				name: string
21
				slug: string
22
				price: { value?: number; unit?: string }
23
				'compare-at-price'?: { value?: number; unit?: string }
24
				'ec-sku-billing-method'?: 'one-time' | 'subscription'
25
				'ec-sku-subscription-plan'?: {
26
					interval?: 'day' | 'week' | 'month' | 'year'
27
					frequency?: number
28
					trial?: number
29
					plans?: {
30
						platform?: 'stripe'
31
						id?: string
32
						status?: 'active' | 'inactive' | 'canceled'
33
					}[]
34
				}
35
				'track-inventory'?: false | true
36
				quantity?: number
37
			}
38
		}[]
39
	}
40
) {
41
	const url = new URL(`https://api.webflow.com/v2/sites/${site_id}/products/${product_id}/skus`)
42

43
	const response = await fetch(url, {
44
		method: 'POST',
45
		headers: {
46
			'Content-Type': 'application/json',
47
			Authorization: 'Bearer ' + auth.token
48
		},
49
		body: JSON.stringify(body)
50
	})
51

52
	if (!response.ok) {
53
		const text = await response.text()
54
		throw new Error(`${response.status} ${text}`)
55
	}
56

57
	return await response.json()
58
}
59