//native
/**
* Create Price
* Create a price for a product. `unit_amount` is in the smallest currency unit; a `recurring_interval` makes it a subscription price (every `recurring_interval_count` intervals, default 1). `extra_params` passes any other documented field through as-is.
*/
export async function main(
auth: RT.Stripe,
product: string,
currency: string,
unit_amount: number,
recurring_interval: "day" | "week" | "month" | "year" | undefined,
recurring_interval_count: number | undefined,
nickname: string | undefined,
lookup_key: string | undefined,
metadata: { [key: string]: string } | undefined,
extra_params: { [key: string]: any } | undefined
) {
const response = await fetch(`https://api.stripe.com/v1/prices`, {
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({
product,
currency,
unit_amount,
recurring: recurring_interval
? {
interval: recurring_interval,
interval_count: recurring_interval_count,
}
: undefined,
nickname,
lookup_key,
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