# Stripe Node.js Library [![Version](https://img.shields.io/npm/v/stripe.svg)](https://www.npmjs.org/package/stripe) [![Build Status](https://travis-ci.org/stripe/stripe-node.svg?branch=master)](https://travis-ci.org/stripe/stripe-node) [![Coverage Status](https://coveralls.io/repos/github/stripe/stripe-node/badge.svg)](https://coveralls.io/github/stripe/stripe-node) [![Downloads](https://img.shields.io/npm/dm/stripe.svg)](https://www.npmjs.com/package/stripe) [![Try on RunKit](https://badge.runkitcdn.com/stripe.svg)](https://runkit.com/npm/stripe) The Stripe Node library provides convenient access to the Stripe API from applications written in server-side JavaScript. Please keep in mind that this package is for use with server-side Node that uses Stripe secret keys. To maintain PCI compliance, tokenization of credit card information should always be done with [Stripe.js][stripe-js] on the client side. This package should not be used for that purpose. ## Documentation See the [`stripe-node` API docs](https://stripe.com/docs/api/node#intro) for Node.js. ## Installation Install the package with: ```sh npm install stripe --save ``` ## Usage The package needs to be configured with your account's secret key which is available in your [Stripe Dashboard][api-keys]. Require it with the key's value: ```js const stripe = require('stripe')('sk_test_...'); const customer = await stripe.customers.create({ email: 'customer@example.com', }); ``` Or using ES modules, this looks more like: ```js import Stripe from 'stripe'; const stripe = Stripe('sk_test_...'); //… ``` On older versions of Node, you can use [promises](#using-promises) or [callbacks](#using-callbacks) instead of `async`/`await`. ## Initialize with config object The package can be initialized with several options: ```js 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 the account's default version will be used. | | `maxNetworkRetries` | 0 | The amount of times a request should be [retried](#network-retries). | | `httpAgent` | `null` | [Proxy](#configuring-a-proxy) agent to be used by the library. | | `timeout` | 120000 (Node default timeout) | [Maximum time each request can take in ms.](#configuring-timeout) | | `host` | `'api.stripe.com'` | Host that requests are made to. | | `port` | 443 | Port that requests are made to. | | `telemetry` | `true` | Allow Stripe to send latency [telemetry](#request-latency-telemetry) | Note: Both `maxNetworkRetries` and `timeout` can be overridden on a per-request basis. `timeout` can be updated at any time with [`stripe.setTimeout`](#configuring-timeout). ### Usage with TypeScript Stripe does not currently maintain typings for this package, but there are community typings available from DefinitelyTyped. To install: ```sh npm install --dev @types/stripe ``` To use: ```ts // Note `* as` and `new Stripe` for TypeScript: import * as Stripe from 'stripe'; const stripe = new Stripe('sk_test_...'); const customer: Promise< Stripe.customers.ICustomer > = stripe.customers.create(/* ... */); ``` ### Using Promises Every method returns a chainable promise which can be used instead of a regular callback: ```js // Create a new customer and then a new charge for that customer: stripe.customers .create({ email: 'foo-customer@example.com', }) .then((customer) => { return stripe.customers.createSource(customer.id, { source: 'tok_visa', }); }) .then((source) => { return stripe.charges.create({ amount: 1600, currency: 'usd', customer: source.customer, }); }) .then((charge) => { // New charge created on a new customer }) .catch((err) => { // Deal with an error }); ``` ### Using callbacks On versions of Node.js prior to v7.9: ```js var stripe = require('stripe')('sk_test_...'); stripe.customers.create( { email: 'customer@example.com', }, function(err, customer) { if (err) { // Deal with an error (will be `null` if no error occurred). } // Do something with created customer object console.log(customer.id); } ); ``` ### Configuring Timeout Request timeout is configurable (the default is Node's default of 120 seconds): ```js stripe.setTimeout(20000); // in ms (this is 20 seconds) ``` Timeout can also be set globally via the config object: ```js const stripe = Stripe('sk_test_...', { timeout: 2000, }); ``` And on a per-request basis: ```js stripe.customers.create( { email: 'customer@example.com', }, { timeout: 1000, } ); ``` If `timeout` is set globally via the config object, the value set in a per-request basis will be favored. ### Configuring For Connect A per-request `Stripe-Account` header for use with [Stripe Connect][connect] can be added to any method: ```js // Retrieve the balance for a connected account: stripe.balance .retrieve({ stripeAccount: 'acct_foo', }) .then((balance) => { // The balance object for the connected account }) .catch((err) => { // Error }); ``` ### Configuring a Proxy An [https-proxy-agent][https-proxy-agent] can be configured with `setHttpAgent`. To use stripe behind a proxy you can pass to sdk on initialization: ```js if (process.env.http_proxy) { const ProxyAgent = require('https-proxy-agent'); const stripe = Stripe('sk_test_...', { httpProxy: 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](https://stripe.com/docs/api/idempotent_requests) are added where appropriate to prevent duplication. ```js 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: ```js stripe.customers.create( { email: 'customer@example.com', }, { 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: ```js charge.lastResponse.requestId; // see: https://stripe.com/docs/api/request_ids?lang=node charge.lastResponse.statusCode; ``` ### `request` and `response` events The Stripe object emits `request` and `response` events. You can use them like this: ```js 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 ```js { api_version: 'latest', account: 'acct_TEST', // Only present if provided idempotency_key: 'abc123', // Only present if provided method: 'POST', path: '/v1/charges', request_start_time: 1565125303932 // Unix timestamp in milliseconds } ``` #### `response` object ```js { api_version: 'latest', account: 'acct_TEST', // Only present if provided idempotency_key: 'abc123', // Only present if provided method: 'POST', path: '/v1/charges', 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](https://stripe.com/docs/webhooks#signatures). 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 [Express](https://expressjs.com/) in the [`examples/webhook-signing`](examples/webhook-signing) folder, but here's what it looks like: ```js 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: ```js 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 identified using `stripe.setAppInfo()`: ```js stripe.setAppInfo({ 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 As of stripe-node 6.11.0, you may auto-paginate list methods. 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](https://github.com/tc39/proposal-async-iteration#the-async-iteration-statement-for-await-of), such as Node 10+ or [babel](https://babeljs.io/docs/en/babel-plugin-transform-async-generator-functions), the following will auto-paginate: ```js 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`: ```js 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: ```js stripe.customers .list() .autoPagingEach((customer) => { return doSomething(customer).then(() => { if (shouldBreak()) { return false; } }); }) .then(() => { console.log('Done iterating.'); }) .catch(handleError); ``` If you prefer callbacks to promises, you may also use a `next` callback and a second `onDone` callback: ```js stripe.customers.list().autoPagingEach( function onItem(customer, next) { doSomething(customer, function(err, result) { if (shouldStop(result)) { next(false); // Passing `false` breaks out of the loop. } else { next(); } }); }, function onDone(err) { if (err) { console.error(err); } else { console.log('Done iterating.'); } } ); ``` If your `onItem` function does not accept a `next` callback parameter _or_ return a Promise, the return value is used to decide whether to continue (`false` breaks, anything else continues). #### `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. ```js 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: ```js stripe.setTelemetryEnabled(false); ``` ## More Information - [REST API Version](https://github.com/stripe/stripe-node/wiki/REST-API-Version) - [Error Handling](https://github.com/stripe/stripe-node/wiki/Error-Handling) - [Passing Options](https://github.com/stripe/stripe-node/wiki/Passing-Options) - [Using Stripe Connect](https://github.com/stripe/stripe-node/wiki/Using-Stripe-Connect-with-node.js) ## Development Run all tests: ```bash $ yarn install $ yarn test ``` If you do not have `yarn` installed, you can get it with `npm install --global yarn`. Run a single test suite without a coverage report: ```bash $ yarn mocha-only test/Error.spec.js ``` Run a single test (case sensitive) in watch mode: ```bash $ yarn mocha-only test/Error.spec.js --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: ```bash $ export STRIPE_TEST_API_KEY='sk_test....' $ yarn test ``` Run prettier: Add an [editor integration](https://prettier.io/docs/en/editors.html) or: ```bash $ yarn fix ``` [api-keys]: https://dashboard.stripe.com/account/apikeys [connect]: https://stripe.com/connect [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent [stripe-js]: https://stripe.com/docs/stripe.js