Node.js library for the Stripe API.
JavaScript
Latest commit a5d4f12 Sep 15, 2017 @brandur-stripe brandur-stripe committed on GitHub Merge pull request #392 from mantoni/patch-1
Minimum requirement is Node 4

README.md

Stripe Node.js Library

Version Build Status Downloads Try on RunKit

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 on the client side. This package should not be used for that purpose.

Documentation

See the Node API docs.

Installation

Install the package with:

npm install stripe --save

Usage

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

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

stripe.customers.create(
  { email: 'customer@example.com' },
  function(err, customer) {
    err; // null if no error occurred
    customer; // the created customer object
  }
);

On ES6, this looks more like:

import stripePackage from 'stripe';
const stripe = stripePackage('sk_test_...');

Using Promises

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

// Create a new customer and then a new charge for that customer:
stripe.customers.create({
  email: 'foo-customer@example.com'
}).then(function(customer){
  return stripe.customers.createSource(customer.id, {
    source: 'tok_visa'
  });
}).then(function(source) {
  return stripe.charges.create({
    amount: 1600,
    currency: 'usd',
    customer: source.customer
  });
}).then(function(charge) {
  // New charge created on a new customer
}).catch(function(err) {
  // Deal with an error
});

Configuring Timeout

Request timeout is configurable (the default is Node's default of 120 seconds):

stripe.setTimeout(20000); // in ms (this is 20 seconds)

Configuring For Connect

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

// Retrieve the balance for a connected account:
stripe.balance.retrieve({
  stripe_account: 'acct_foo'
}).then(function(balance) {
  // The balance object for the connected account
}).catch(function(err) {
  // Error
});

Configuring a Proxy

An https-proxy-agent can be configured with setHttpAgent.

To use stripe behind a proxy you can pass to sdk:

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

Examining Responses

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

charge.lastResponse.requestId // see: https://stripe.com/docs/api/node#request_ids
charge.lastResponse.statusCode

request and response events

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

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

function 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/charges'
}

response object

{
  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
}

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.

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

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

Writing a Plugin

If you're writing a plugin that uses the library, we'd appreciate it if you identified using stripe.setAppInfo():

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.

More Information

Development

Run all tests:

$ npm install
$ npm test

Run a single test suite:

$ npm run mocha -- test/Error.spec.js

Run a single test (case sensitive):

$ npm run mocha -- test/Error.spec.js --grep 'Populates with type'

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....'
$ npm test