• Stars
    star
    3,874
  • Rank 11,314 (Top 0.3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated 12 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js library for the Stripe API.

Stripe Node.js Library

Version Build Status Coverage Status Downloads Try on RunKit

The Stripe Node library provides convenient access to the Stripe API from applications written in server-side JavaScript.

For collecting customer and payment information in the browser, use Stripe.js.

Documentation

See the stripe-node API docs for Node.js.

See video demonstrations covering how to use the library.

Requirements

Node 12 or higher.

Installation

Install the package with:

npm install stripe --save
# or
yarn add stripe

Usage

The package needs to be configured with your account's secret key, which is available in the Stripe Dashboard. Require it with the key's value:

const stripe = require('stripe')('sk_test_...');

stripe.customers.create({
  email: '[email protected]',
})
  .then(customer => console.log(customer.id))
  .catch(error => console.error(error));

Or using ES modules and async/await:

import Stripe from 'stripe';
const stripe = new Stripe('sk_test_...');

const customer = await stripe.customers.create({
  email: '[email protected]',
});

console.log(customer.id);

Usage with TypeScript

As of 8.0.1, Stripe maintains types for the latest API version.

Import Stripe as a default import (not * as Stripe, unlike the DefinitelyTyped version) and instantiate it as new Stripe() with the latest API version.

import Stripe from 'stripe';
const stripe = new Stripe('sk_test_...', {
  apiVersion: '2022-11-15',
});

const createCustomer = async () => {
  const params: Stripe.CustomerCreateParams = {
    description: 'test customer',
  };

  const customer: Stripe.Customer = await stripe.customers.create(params);

  console.log(customer.id);
};
createCustomer();

You can find a full TS server example in stripe-samples.

Using old API versions with TypeScript

Types can change between API versions (e.g., Stripe may have changed a field from a string to a hash), so our types only reflect the latest API version.

We therefore encourage upgrading your API version if you would like to take advantage of Stripe's TypeScript definitions.

If you are on an older API version (e.g., 2019-10-17) and not able to upgrade, you may pass another version and use a comment like // @ts-ignore stripe-version-2019-10-17 to silence type errors here and anywhere the types differ between your API version and the latest. When you upgrade, you should remove these comments.

We also recommend using // @ts-ignore if you have access to a beta feature and need to send parameters beyond the type definitions.

Using expand with TypeScript

Expandable fields are typed as string | Foo, so you must cast them appropriately, e.g.,

const paymentIntent: Stripe.PaymentIntent = await stripe.paymentIntents.retrieve(
  'pi_123456789',
  {
    expand: ['customer'],
  }
);
const customerEmail: string = (paymentIntent.customer as Stripe.Customer).email;

Using Promises

Every method returns a chainable promise which can be used instead of a regular callback:

// Create a new customer and then create an invoice item then invoice it:
stripe.customers
  .create({
    email: '[email protected]',
  })
  .then((customer) => {
    // have access to the customer object
    return stripe.invoiceItems
      .create({
        customer: customer.id, // set the customer id
        amount: 2500, // 25
        currency: 'usd',
        description: 'One-time setup fee',
      })
      .then((invoiceItem) => {
        return stripe.invoices.create({
          collection_method: 'send_invoice',
          customer: invoiceItem.customer,
        });
      })
      .then((invoice) => {
        // New invoice created on a new customer
      })
      .catch((err) => {
        // Deal with an error
      });
  });

Usage with Deno

As of 11.16.0, stripe-node provides a deno export target. In your Deno project, import stripe-node using an npm specifier:

Import using npm specifiers:

import Stripe from 'npm:stripe';

Please see https://github.com/stripe-samples/stripe-node-deno-samples for more detailed examples and instructions on how to use stripe-node in Deno.

Configuration

Initialize with config object

The package can be initialized with several options:

import ProxyAgent from 'https-proxy-agent';

const stripe = Stripe('sk_test_...', {
  apiVersion: '2019-08-08',
  maxNetworkRetries: 1,
  httpAgent: new ProxyAgent(process.env.http_proxy),
  timeout: 1000,
  host: 'api.example.com',
  port: 123,
  telemetry: true,
});
Option Default Description
apiVersion null Stripe API version to be used. If not set, stripe-node will use the latest version at the time of release.
maxNetworkRetries 0 The amount of times a request should be retried.
httpAgent null Proxy agent to be used by the library.
timeout 80000 Maximum time each request can take in ms.
host 'api.stripe.com' Host that requests are made to.
port 443 Port that requests are made to.
protocol 'https' 'https' or 'http'. http is never appropriate for sending requests to Stripe servers, and we strongly discourage http, even in local testing scenarios, as this can result in your credentials being transmitted over an insecure channel.
telemetry true Allow Stripe to send latency telemetry.

Note Both maxNetworkRetries and timeout can be overridden on a per-request basis.

Configuring Timeout

Timeout can be set globally via the config object:

const stripe = Stripe('sk_test_...', {
  timeout: 20 * 1000, // 20 seconds
});

And overridden on a per-request basis:

stripe.customers.create(
  {
    email: '[email protected]',
  },
  {
    timeout: 1000, // 1 second
  }
);

Configuring For Connect

A per-request Stripe-Account header for use with Stripe Connect can be added to any method:

// List the balance transactions for a connected account:
stripe.balanceTransactions.list(
  {
    limit: 10,
  },
  {
    stripeAccount: 'acct_foo',
  }
);

Configuring a Proxy

To use stripe behind a proxy you can pass an https-proxy-agent on initialization:

if (process.env.http_proxy) {
  const ProxyAgent = require('https-proxy-agent');

  const stripe = Stripe('sk_test_...', {
    httpAgent: new ProxyAgent(process.env.http_proxy),
  });
}

Network retries

Automatic network retries can be enabled with the maxNetworkRetries config option. This will retry requests n times with exponential backoff if they fail due to an intermittent network problem. Idempotency keys are added where appropriate to prevent duplication.

const stripe = Stripe('sk_test_...', {
  maxNetworkRetries: 2, // Retry a request twice before giving up
});

Network retries can also be set on a per-request basis:

stripe.customers.create(
  {
    email: '[email protected]',
  },
  {
    maxNetworkRetries: 2, // Retry this specific request twice before giving up
  }
);

Examining Responses

Some information about the response which generated a resource is available with the lastResponse property:

customer.lastResponse.requestId; // see: https://stripe.com/docs/api/request_ids?lang=node
customer.lastResponse.statusCode;

request and response events

The Stripe object emits request and response events. You can use them like this:

const stripe = require('stripe')('sk_test_...');

const onRequest = (request) => {
  // Do something.
};

// Add the event handler function:
stripe.on('request', onRequest);

// Remove the event handler function:
stripe.off('request', onRequest);

request object

{
  api_version: 'latest',
  account: 'acct_TEST',              // Only present if provided
  idempotency_key: 'abc123',         // Only present if provided
  method: 'POST',
  path: '/v1/customers',
  request_start_time: 1565125303932  // Unix timestamp in milliseconds
}

response object

{
  api_version: 'latest',
  account: 'acct_TEST',              // Only present if provided
  idempotency_key: 'abc123',         // Only present if provided
  method: 'POST',
  path: '/v1/customers',
  status: 402,
  request_id: 'req_Ghc9r26ts73DRf',
  elapsed: 445,                      // Elapsed time in milliseconds
  request_start_time: 1565125303932, // Unix timestamp in milliseconds
  request_end_time: 1565125304377    // Unix timestamp in milliseconds
}

Webhook signing

Stripe can optionally sign the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party. You can read more about it here.

Please note that you must pass the raw request body, exactly as received from Stripe, to the constructEvent() function; this will not work with a parsed (i.e., JSON) request body.

You can find an example of how to use this with various JavaScript frameworks in examples/webhook-signing folder, but here's what it looks like:

const event = stripe.webhooks.constructEvent(
  webhookRawBody,
  webhookStripeSignatureHeader,
  webhookSecret
);

Testing Webhook signing

You can use stripe.webhooks.generateTestHeaderString to mock webhook events that come from Stripe:

const payload = {
  id: 'evt_test_webhook',
  object: 'event',
};

const payloadString = JSON.stringify(payload, null, 2);
const secret = 'whsec_test_secret';

const header = stripe.webhooks.generateTestHeaderString({
  payload: payloadString,
  secret,
});

const event = stripe.webhooks.constructEvent(payloadString, header, secret);

// Do something with mocked signed event
expect(event.id).to.equal(payload.id);

Writing a Plugin

If you're writing a plugin that uses the library, we'd appreciate it if you instantiated your stripe client with appInfo, eg;

const stripe = require('stripe')('sk_test_...', {
  appInfo: {
    name: 'MyAwesomePlugin',
    version: '1.2.34', // Optional
    url: 'https://myawesomeplugin.info', // Optional
  }
});

Or using ES modules or TypeScript:

const stripe = new Stripe(apiKey, {
  appInfo: {
    name: 'MyAwesomePlugin',
    version: '1.2.34', // Optional
    url: 'https://myawesomeplugin.info', // Optional
  }
});

This information is passed along when the library makes calls to the Stripe API.

Auto-pagination

We provide a few different APIs for this to aid with a variety of node versions and styles.

Async iterators (for-await-of)

If you are in a Node environment that has support for async iteration, such as Node 10+ or babel, the following will auto-paginate:

for await (const customer of stripe.customers.list()) {
  doSomething(customer);
  if (shouldStop()) {
    break;
  }
}

autoPagingEach

If you are in a Node environment that has support for await, such as Node 7.9 and greater, you may pass an async function to .autoPagingEach:

await stripe.customers.list().autoPagingEach(async (customer) => {
  await doSomething(customer);
  if (shouldBreak()) {
    return false;
  }
});
console.log('Done iterating.');

Equivalently, without await, you may return a Promise, which can resolve to false to break:

stripe.customers
  .list()
  .autoPagingEach((customer) => {
    return doSomething(customer).then(() => {
      if (shouldBreak()) {
        return false;
      }
    });
  })
  .then(() => {
    console.log('Done iterating.');
  })
  .catch(handleError);

autoPagingToArray

This is a convenience for cases where you expect the number of items to be relatively small; accordingly, you must pass a limit option to prevent runaway list growth from consuming too much memory.

Returns a promise of an array of all items across pages for a list request.

const allNewCustomers = await stripe.customers
  .list({created: {gt: lastMonth}})
  .autoPagingToArray({limit: 10000});

Request latency telemetry

By default, the library sends request latency telemetry to Stripe. These numbers help Stripe improve the overall latency of its API for all users.

You can disable this behavior if you prefer:

const stripe = new Stripe('sk_test_...', {
  telemetry: false,
});

Beta SDKs

Stripe has features in the beta phase that can be accessed via the beta version of this package. We would love for you to try these and share feedback with us before these features reach the stable phase. The beta versions can be installed in one of two ways

  • To install the latest beta version, run the command npm install stripe@beta --save
  • To install a specific beta version, replace the term "beta" in the above command with the version number like npm install [email protected] --save

Note There can be breaking changes between beta versions. Therefore we recommend pinning the package version to a specific beta version in your package.json file. This way you can install the same version each time without breaking changes unless you are intentionally looking for the latest beta version.

We highly recommend keeping an eye on when the beta feature you are interested in goes from beta to stable so that you can move from using a beta version of the SDK to the stable version.

The versions tab on the stripe page on npm lists the current tags in use. The beta tag here corresponds to the the latest beta version of the package.

If your beta feature requires a Stripe-Version header to be sent, use the apiVersion property of config object to set it:

const stripe = new Stripe('sk_test_...', {
  apiVersion: '2022-08-01; feature_beta=v3',
});

Support

New features and bug fixes are released on the latest major version of the stripe package. If you are on an older major version, we recommend that you upgrade to the latest in order to use the new features and bug fixes including those for security vulnerabilities. Older major versions of the package will continue to be available for use, but will not be receiving any updates.

More Information

Development

Run all tests:

$ yarn install
$ yarn test

If you do not have yarn installed, you can get it with npm install --global yarn.

The tests also depends on stripe-mock, so make sure to fetch and run it from a background terminal (stripe-mock's README also contains instructions for installing via Homebrew and other methods):

go get -u github.com/stripe/stripe-mock
stripe-mock

Run a single test suite without a coverage report:

$ yarn mocha-only test/Error.spec.ts

Run a single test (case sensitive) in watch mode:

$ yarn mocha-only test/Error.spec.ts --grep 'Populates with type' --watch

If you wish, you may run tests using your Stripe Test API key by setting the environment variable STRIPE_TEST_API_KEY before running the tests:

$ export STRIPE_TEST_API_KEY='sk_test....'
$ yarn test

Run prettier:

Add an editor integration or:

$ yarn fix

More Repositories

1

stripe-php

PHP library for the Stripe API.
PHP
3,736
star
2

stripe-go

Go library for the Stripe API.
Go
2,144
star
3

stripe-ios

Stripe iOS SDK
Swift
2,110
star
4

stripe-ruby

Ruby library for the Stripe API.
Ruby
1,952
star
5

react-stripe-js

React components for Stripe.js and Stripe Elements
TypeScript
1,757
star
6

veneur

A distributed, fault-tolerant pipeline for observability data
Go
1,734
star
7

stripe-python

Python library for the Stripe API.
Python
1,669
star
8

stripe-cli

A command-line tool for Stripe
Go
1,609
star
9

stripe-mock

stripe-mock is a mock HTTP server that responds like the real Stripe API. It can be used instead of Stripe's testmode to make test suites integrating with Stripe faster and less brittle.
Go
1,374
star
10

stripe-dotnet

Stripe.net is a sync/async .NET 4.6.1+ client, and a portable class library for stripe.com.
C#
1,362
star
11

stripe-android

Stripe Android SDK
Kotlin
1,277
star
12

stripe-react-native

React Native library for Stripe.
TypeScript
1,272
star
13

smokescreen

A simple HTTP proxy that fogs over naughty URLs
Go
1,081
star
14

elements-examples

Stripe Elements examples.
HTML
996
star
15

stripe-java

Java library for the Stripe API.
Java
818
star
16

skycfg

Skycfg is an extension library for the Starlark language that adds support for constructing Protocol Buffer messages.
Go
646
star
17

stripe-connect-rocketrides

Sample on-demand platform built on Stripe: Connect onboarding for pilots, iOS app for passengers to request rides.
Swift
634
star
18

stripe-js

Loading wrapper for Stripe.js
TypeScript
625
star
19

poncho

Easily create REST APIs
Ruby
516
star
20

rainier

Bayesian inference in Scala.
Scala
433
star
21

openapi

An OpenAPI specification for the Stripe API.
391
star
22

pg-schema-diff

Go library for diffing Postgres schemas and generating SQL migrations
Go
364
star
23

stripe-billing-typographic

โšก๏ธTypographic is a webfont service (and demo) built with Stripe Billing.
JavaScript
216
star
24

subprocess

A port of Python's subprocess module to Ruby
Ruby
208
star
25

carbon-removal-source-materials

Source materials supporting Stripe Climate carbon removal purchases (http://stripe.com/climate)
190
star
26

stripe-apps

Stripe Apps lets you embed custom user experiences directly in the Stripe Dashboard and orchestrate the Stripe API.
TypeScript
148
star
27

dagon

Tools for rewriting and optimizing DAGs (directed-acyclic graphs) in Scala
Scala
148
star
28

bonsai

Beautiful trees, without the landscaping.
Scala
141
star
29

ios-dashboard-ui

[DEPRECATED] UI components from the Stripe Dashboard iOS app
Swift
138
star
30

vscode-stripe

Stripe for Visual Studio Code
TypeScript
123
star
31

stripe.github.io

A landing page for Stripe's GitHub organization
HTML
117
star
32

stripe-terminal-react-native

React Native SDK for Stripe Terminal
TypeScript
108
star
33

example-mobile-backend

A simple, easy-to-deploy backend that you can use to demo our example mobile apps.
Ruby
106
star
34

stripe-terminal-ios

Stripe Terminal iOS SDK
Objective-C
102
star
35

stripe-demo-connect-roastery-saas-platform

Roastery demo SaaS platform using Stripe Connect
JavaScript
94
star
36

stripe-terminal-android

Stripe Terminal Android SDK
Java
93
star
37

example-terminal-backend

A simple, easy-to-deploy backend that you can use to run the Stripe Terminal example apps
Ruby
88
star
38

stripe-terminal-js-demo

Demo app for the Stripe Terminal JS SDK
JavaScript
82
star
39

stripe-postman

Postman collection for Stripe's API
77
star
40

unilog

A logger for use with daemontools.
Go
77
star
41

goforit

A feature flags client library for Go
Go
70
star
42

stripe-connect-custom-rocketdeliveries

Sample on-demand platform built on Stripe Connect: Custom Accounts and Connect Onboarding for deliveries. https://rocketdeliveries.io
JavaScript
69
star
43

tracer-objc

Generic record & playback framework for Objective-C
Objective-C
52
star
44

mobile-viewport-control

Dynamically control the mobile viewport
JavaScript
48
star
45

log4j-remediation-tools

Tools for remediating the recent log4j2 RCE vulnerability (CVE-2021-44228)
Go
41
star
46

terminal-js

Loading wrapper for the Terminal JS SDK
TypeScript
34
star
47

stripe-connect-furever-demo

Code sample demo built on Stripe Connect embedded components.
TypeScript
34
star
48

stripe-identity-react-native

React Native library for Stripe Identity
TypeScript
33
star
49

stripe-reachability

A bash script to test access to the Stripe API
Shell
32
star
50

krl

OpenSSH Key Revocation List support for Go
Go
30
star
51

stripe-magento2-releases

27
star
52

react-connect-js

React components for Connect.js and Connect embedded components
TypeScript
22
star
53

connect-js

Loading wrapper for Connect.js
TypeScript
21
star
54

stripe-mirakl-connector

Official Stripe Mirakl Connector
PHP
13
star
55

salesforce-connector-examples

Stripe Salesforce Connector examples
Apex
12
star
56

checkout-sales-demo

Sales demo of Stripe Checkout with different locales around the world.
HTML
12
star
57

stripe-ios-spm

Swift Package Manager mirror for the Stripe iOS SDK. See http://github.com/stripe/stripe-ios for details.
12
star
58

.github

Stripe uses the Contributor Covenant Code of Conduct for our open-source community.
8
star
59

homebrew-stripe-mock

Homebrew tap for stripestub
Ruby
7
star
60

scoop-stripe-cli

7
star
61

homebrew-stripe-cli

Ruby
7
star
62

stripe-sfcc-b2c-connector

JavaScript
7
star
63

stripe-commercetools-connect-app

TypeScript
5
star
64

ssf-ruby

A Ruby client for the Sensor Sensibility Format
Ruby
5
star
65

terminal-connector-releases

Release binaries for Stripe Terminal connectors
3
star
66

open-banking-v2-docs

Open banking docs for V2 APIs
CSS
2
star
67

vscode-endsmart

vscode extension to smartly add `end` keywords
TypeScript
1
star