0

Search Customers

by
Published Oct 30, 2023

Search customers with Stripe's search query language, e.g. `email~"@example.com" AND metadata["plan"]:"pro"`. Pass the response's `next_page` as `page` for more. Search is not available in India and new objects can take a minute to become searchable.

Script stripe Verified

The script

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

3
/**
4
 * Search Customers
5
 * Search customers with Stripe's search query language, e.g. `email~"@example.com" AND metadata["plan"]:"pro"`. Pass the response's `next_page` as `page` for more. Search is not available in India and new objects can take a minute to become searchable.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  query: string,
10
  limit: number | undefined,
11
  page: string | undefined
12
) {
13
  const url = new URL(`https://api.stripe.com/v1/customers/search`)
14
  url.search = toForm({ query, limit, page }).toString()
15

16
  const response = await fetch(url, {
17
    headers: {
18
      Authorization: `Bearer ${auth.token}`,
19
      Accept: "application/json",
20
      "Stripe-Version": "2025-03-31.basil",
21
    },
22
  })
23

24
  if (!response.ok) {
25
    throw new Error(`${response.status} ${await response.text()}`)
26
  }
27

28
  return await response.json()
29
}
30

31
// Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
32
function toForm(
33
  params: { [key: string]: any },
34
  prefix = "",
35
  out = new URLSearchParams()
36
) {
37
  for (const [key, value] of Object.entries(params)) {
38
    if (value == null || value === "") continue
39
    const name = prefix ? `${prefix}[${key}]` : key
40
    if (typeof value === "object") toForm(value, name, out)
41
    else out.append(name, String(value))
42
  }
43
  return out
44
}
45

Other submissions
  • Submitted by jbmolle697 Deno
    Created 1076 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
      auth: Stripe,
    8
      query: string,
    9
      limit?: number,
    10
      page?: string,
    11
    ) {
    12
      const token = auth["token"];
    13
      const stripe = Stripe(`${token}`, {
    14
        httpClient: Stripe.createFetchHttpClient(),
    15
      });
    16
      const searchParams: Stripe.CustomerSearchParams = {
    17
        query,
    18
        limit: limit || undefined,
    19
        page: page || undefined
    20
      };
    21
      const searchCustomers = await stripe.customers.search(searchParams);
    22
      return searchCustomers;
    23
    }
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get customers search
    6
     * Search for customers you’ve previously created using Stripe’s Search Query Language.
    7
    Don’t use search in read-after-write flows where strict consistency is necessary. Under normal operating
    8
    conditions, data is searchable in less than a minute. Occasionally, propagation of new or updated data can be up
    9
    to an hour behind during outages. Search functionality is not available to merchants in India.
    10
     */
    11
    export async function main(
    12
      auth: Stripe,
    13
      expand: any,
    14
      limit: string | undefined,
    15
      page: string | undefined,
    16
      query: string | undefined
    17
    ) {
    18
      const url = new URL(`https://api.stripe.com/v1/customers/search`);
    19
      for (const [k, v] of [
    20
        ["limit", limit],
    21
        ["page", page],
    22
        ["query", query],
    23
      ]) {
    24
        if (v !== undefined && v !== "") {
    25
          url.searchParams.append(k, v);
    26
        }
    27
      }
    28
      encodeParams({ expand }).forEach((v, k) => {
    29
        if (v !== undefined && v !== "") {
    30
          url.searchParams.append(k, v);
    31
        }
    32
      });
    33
      const response = await fetch(url, {
    34
        method: "GET",
    35
        headers: {
    36
          "Content-Type": "application/x-www-form-urlencoded",
    37
          Authorization: "Bearer " + auth.token,
    38
        },
    39
        body: undefined,
    40
      });
    41
      if (!response.ok) {
    42
        const text = await response.text();
    43
        throw new Error(`${response.status} ${text}`);
    44
      }
    45
      return await response.json();
    46
    }
    47
    
    
    48
    function encodeParams(o: any) {
    49
      function iter(o: any, path: string) {
    50
        if (Array.isArray(o)) {
    51
          o.forEach(function (a) {
    52
            iter(a, path + "[]");
    53
          });
    54
          return;
    55
        }
    56
        if (o !== null && typeof o === "object") {
    57
          Object.keys(o).forEach(function (k) {
    58
            iter(o[k], path + "[" + k + "]");
    59
          });
    60
          return;
    61
        }
    62
        data.push(path + "=" + o);
    63
      }
    64
      const data: string[] = [];
    65
      Object.keys(o).forEach(function (k) {
    66
        if (o[k] !== undefined) {
    67
          iter(o[k], k);
    68
        }
    69
      });
    70
      return new URLSearchParams(data.join("&"));
    71
    }
    72