1

Update Customer

by
Published Aug 25, 2022

Update a customer. Empty fields are left unchanged; metadata keys are merged into the existing metadata. `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
 * Update Customer
5
 * Update a customer. Empty fields are left unchanged; metadata keys are merged into the existing metadata. `extra_params` passes any other documented field through as-is.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  customer_id: string,
10
  name: string | undefined,
11
  email: string | undefined,
12
  phone: string | undefined,
13
  description: string | undefined,
14
  address:
15
    | {
16
        line1?: string
17
        line2?: string
18
        city?: string
19
        state?: string
20
        postal_code?: string
21
        country?: string
22
      }
23
    | undefined,
24
  metadata: { [key: string]: string } | undefined,
25
  extra_params: { [key: string]: any } | undefined
26
) {
27
  const response = await fetch(
28
    `https://api.stripe.com/v1/customers/${customer_id}`,
29
    {
30
      method: "POST",
31
      headers: {
32
        Authorization: `Bearer ${auth.token}`,
33
        "Content-Type": "application/x-www-form-urlencoded",
34
        Accept: "application/json",
35
        "Stripe-Version": "2025-03-31.basil",
36
      },
37
      body: toForm({
38
        name,
39
        email,
40
        phone,
41
        description,
42
        address,
43
        metadata,
44
        ...extra_params,
45
      }),
46
    }
47
  )
48

49
  if (!response.ok) {
50
    throw new Error(`${response.status} ${await response.text()}`)
51
  }
52

53
  return await response.json()
54
}
55

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

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
      order_id: string
    10
    ) {
    11
      const token = stripe_con["token"];
    12
      const stripe = Stripe(`${token}`, {
    13
        httpClient: Stripe.createFetchHttpClient(),
    14
      });
    15
    
    
    16
      const customer = await stripe.customers.update(`${customer_id}`, {
    17
        metadata: { order_id: order_id },
    18
      });
    19
    
    
    20
      return await customer.json();
    21
    }
    22
    
    
  • 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
      order_id: string
    10
    ) {
    11
      const token = stripe_con["token"];
    12
      const stripe = StripeClient(`${token}`, {
    13
        httpClient: StripeClient.createFetchHttpClient(),
    14
      });
    15
    
    
    16
      const customer = await stripe.customers.update(`${customer_id}`, {
    17
        metadata: { order_id: order_id },
    18
      });
    19
    
    
    20
      return await customer.json();
    21
    }
    22