1

Create Invoice Item

by
Published Aug 23, 2022

Add a line item to a draft invoice (`invoice`) or to the customer's pending items for their next invoice. Give either a `price` id (with `quantity`) or an `amount` in the smallest currency unit plus `currency`. `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 Invoice Item
5
 * Add a line item to a draft invoice (`invoice`) or to the customer's pending items for their next invoice. Give either a `price` id (with `quantity`) or an `amount` in the smallest currency unit plus `currency`. `extra_params` passes any other documented field through as-is.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  customer: string,
10
  invoice: string | undefined,
11
  price: string | undefined,
12
  quantity: number | undefined,
13
  amount: number | undefined,
14
  currency: string | undefined,
15
  description: string | undefined,
16
  subscription: string | undefined,
17
  metadata: { [key: string]: string } | undefined,
18
  extra_params: { [key: string]: any } | undefined
19
) {
20
  const response = await fetch(`https://api.stripe.com/v1/invoiceitems`, {
21
    method: "POST",
22
    headers: {
23
      Authorization: `Bearer ${auth.token}`,
24
      "Content-Type": "application/x-www-form-urlencoded",
25
      Accept: "application/json",
26
      "Stripe-Version": "2025-03-31.basil",
27
    },
28
    body: toForm({
29
      customer,
30
      invoice,
31
      pricing: { price },
32
      quantity,
33
      amount,
34
      currency,
35
      description,
36
      subscription,
37
      metadata,
38
      ...extra_params,
39
    }),
40
  })
41

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

46
  return await response.json()
47
}
48

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

Other submissions
  • Submitted by rossmccrann Deno
    Created 528 days ago
    1
    import Stripe from "https://esm.sh/stripe?target=deno";
    2
    
    
    3
    type Stripe = {
    4
      token: string;
    5
    };
    6
    export async function main(
    7
      stripe_con: Stripe,
    8
      customer_id: string,
    9
      price_id: string
    10
    ) {
    11
      const token = stripe_con["token"];
    12
      const stripe = Stripe(`${token}`, {
    13
        httpClient: Stripe.createFetchHttpClient(),
    14
      });
    15
    
    
    16
      const invoiceItem = await stripe.invoiceItems.create({
    17
        customer: `${customer_id}`,
    18
        price: `${price_id}`,
    19
      });
    20
    
    
    21
      return await invoiceItem.json();
    22
    }
    23
    
    
  • Submitted by hugo989 Bun
    Created 136 days ago
    1
    import StripeClient from "stripe@11";
    2
    
    
    3
    type Stripe = {
    4
      token: string;
    5
    };
    6
    export async function main(
    7
      stripe_con: Stripe,
    8
      customer_id: string,
    9
      price_id: string
    10
    ) {
    11
      const token = stripe_con["token"];
    12
      const stripe = StripeClient(`${token}`, {
    13
        httpClient: StripeClient.createFetchHttpClient(),
    14
      });
    15
    
    
    16
      const invoiceItem = await stripe.invoiceItems.create({
    17
        customer: `${customer_id}`,
    18
        price: `${price_id}`,
    19
      });
    20
    
    
    21
      return await invoiceItem.json();
    22
    }
    23