//native
/**
* Create Product
* Create a product. Set `default_price_unit_amount` (smallest currency unit) and `default_price_currency` to create its default price in the same call; add a recurring interval for a subscription price. `extra_params` passes any other documented field through as-is.
*/
export async function main(
auth: RT.Stripe,
name: string,
description: string | undefined,
unit_label: string | undefined,
default_price_unit_amount: number | undefined,
default_price_currency: string | undefined,
default_price_recurring_interval:
"day" | "week" | "month" | "year" | undefined,
metadata: { [key: string]: string } | undefined,
extra_params: { [key: string]: any } | undefined
) {
const response = await fetch(`https://api.stripe.com/v1/products`, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
"Stripe-Version": "2025-03-31.basil",
},
body: toForm({
name,
description,
unit_label,
default_price_data: default_price_currency
? {
currency: default_price_currency,
unit_amount: default_price_unit_amount,
recurring: { interval: default_price_recurring_interval },
}
: undefined,
metadata,
...extra_params,
}),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
// Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
function toForm(
params: { [key: string]: any },
prefix = "",
out = new URLSearchParams()
) {
for (const [key, value] of Object.entries(params)) {
if (value == null || value === "") continue
const name = prefix ? `${prefix}[${key}]` : key
if (typeof value === "object") toForm(value, name, out)
else out.append(name, String(value))
}
return out
}
Submitted by hugo989 3 hours ago