0

Create Price

by
Published Oct 30, 2023

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.

Script stripe Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Create Price
5
 * 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.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  product: string,
10
  currency: string,
11
  unit_amount: number,
12
  recurring_interval: "day" | "week" | "month" | "year" | undefined,
13
  recurring_interval_count: number | undefined,
14
  nickname: string | undefined,
15
  lookup_key: string | undefined,
16
  metadata: { [key: string]: string } | undefined,
17
  extra_params: { [key: string]: any } | undefined
18
) {
19
  const response = await fetch(`https://api.stripe.com/v1/prices`, {
20
    method: "POST",
21
    headers: {
22
      Authorization: `Bearer ${auth.token}`,
23
      "Content-Type": "application/x-www-form-urlencoded",
24
      Accept: "application/json",
25
      "Stripe-Version": "2025-03-31.basil",
26
    },
27
    body: toForm({
28
      product,
29
      currency,
30
      unit_amount,
31
      recurring: recurring_interval
32
        ? {
33
            interval: recurring_interval,
34
            interval_count: recurring_interval_count,
35
          }
36
        : undefined,
37
      nickname,
38
      lookup_key,
39
      metadata,
40
      ...extra_params,
41
    }),
42
  })
43

44
  if (!response.ok) {
45
    throw new Error(`${response.status} ${await response.text()}`)
46
  }
47

48
  return await response.json()
49
}
50

51
// Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
52
function toForm(
53
  params: { [key: string]: any },
54
  prefix = "",
55
  out = new URLSearchParams()
56
) {
57
  for (const [key, value] of Object.entries(params)) {
58
    if (value == null || value === "") continue
59
    const name = prefix ? `${prefix}[${key}]` : key
60
    if (typeof value === "object") toForm(value, name, out)
61
    else out.append(name, String(value))
62
  }
63
  return out
64
}
65

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Post prices
    6
     * Creates a new price for an existing product. The price can be recurring or one-time.
    7
     */
    8
    export async function main(
    9
      auth: Stripe,
    10
      body: {
    11
        active?: boolean;
    12
        billing_scheme?: "per_unit" | "tiered";
    13
        currency: string;
    14
        currency_options?: {
    15
          [k: string]: {
    16
            custom_unit_amount?: {
    17
              enabled: boolean;
    18
              maximum?: number;
    19
              minimum?: number;
    20
              preset?: number;
    21
              [k: string]: unknown;
    22
            };
    23
            tax_behavior?: "exclusive" | "inclusive" | "unspecified";
    24
            tiers?: {
    25
              flat_amount?: number;
    26
              flat_amount_decimal?: string;
    27
              unit_amount?: number;
    28
              unit_amount_decimal?: string;
    29
              up_to: "inf" | number;
    30
              [k: string]: unknown;
    31
            }[];
    32
            unit_amount?: number;
    33
            unit_amount_decimal?: string;
    34
            [k: string]: unknown;
    35
          };
    36
        };
    37
        custom_unit_amount?: {
    38
          enabled: boolean;
    39
          maximum?: number;
    40
          minimum?: number;
    41
          preset?: number;
    42
          [k: string]: unknown;
    43
        };
    44
        expand?: string[];
    45
        lookup_key?: string;
    46
        metadata?: { [k: string]: string };
    47
        nickname?: string;
    48
        product?: string;
    49
        product_data?: {
    50
          active?: boolean;
    51
          id?: string;
    52
          metadata?: { [k: string]: string };
    53
          name: string;
    54
          statement_descriptor?: string;
    55
          tax_code?: string;
    56
          unit_label?: string;
    57
          [k: string]: unknown;
    58
        };
    59
        recurring?: {
    60
          aggregate_usage?: "last_during_period" | "last_ever" | "max" | "sum";
    61
          interval: "day" | "month" | "week" | "year";
    62
          interval_count?: number;
    63
          usage_type?: "licensed" | "metered";
    64
          [k: string]: unknown;
    65
        };
    66
        tax_behavior?: "exclusive" | "inclusive" | "unspecified";
    67
        tiers?: {
    68
          flat_amount?: number;
    69
          flat_amount_decimal?: string;
    70
          unit_amount?: number;
    71
          unit_amount_decimal?: string;
    72
          up_to: "inf" | number;
    73
          [k: string]: unknown;
    74
        }[];
    75
        tiers_mode?: "graduated" | "volume";
    76
        transfer_lookup_key?: boolean;
    77
        transform_quantity?: {
    78
          divide_by: number;
    79
          round: "down" | "up";
    80
          [k: string]: unknown;
    81
        };
    82
        unit_amount?: number;
    83
        unit_amount_decimal?: string;
    84
      }
    85
    ) {
    86
      const url = new URL(`https://api.stripe.com/v1/prices`);
    87
    
    
    88
      const response = await fetch(url, {
    89
        method: "POST",
    90
        headers: {
    91
          "Content-Type": "application/x-www-form-urlencoded",
    92
          Authorization: "Bearer " + auth.token,
    93
        },
    94
        body: encodeParams(body),
    95
      });
    96
      if (!response.ok) {
    97
        const text = await response.text();
    98
        throw new Error(`${response.status} ${text}`);
    99
      }
    100
      return await response.json();
    101
    }
    102
    
    
    103
    function encodeParams(o: any) {
    104
      function iter(o: any, path: string) {
    105
        if (Array.isArray(o)) {
    106
          o.forEach(function (a) {
    107
            iter(a, path + "[]");
    108
          });
    109
          return;
    110
        }
    111
        if (o !== null && typeof o === "object") {
    112
          Object.keys(o).forEach(function (k) {
    113
            iter(o[k], path + "[" + k + "]");
    114
          });
    115
          return;
    116
        }
    117
        data.push(path + "=" + o);
    118
      }
    119
      const data: string[] = [];
    120
      Object.keys(o).forEach(function (k) {
    121
        if (o[k] !== undefined) {
    122
          iter(o[k], k);
    123
        }
    124
      });
    125
      return new URLSearchParams(data.join("&"));
    126
    }
    127