WittCode💻

How to Connect Node to Stripe

By

Learn how to connect a Node application to Stripe using the Stripe API. We will also go over the Stripe developer dashboard, how to create a Stripe customer, and the Stripe publishable key vs. secret key.

Table of Contents 📖

Generating Stripe Keys

To connect a Node application to Stripe, we need to generate both a publishable key and a secret key using the Stripe website.

https://stripe.com

These keys are used to communicate with the Stripe API and identify our application. To generate these keys, first create a Stripe account. Creating this account will include providing details about our application/business such as:

  • Specifying the business location and type type.
  • Filling out personal details such as name, email, birth date, home address, and phone number.
  • Entering more business information including VAT number, industry, business website, product description.
  • Describe how our business fulfills orders.
  • Add bank information such as the currency, account number, sort code, etc. to receive payments.
  • Specifying the product category to determine much tax to collect on Stripe transactions.

INFO: If we don't specify a bank account we can set things up but only in test mode. We won't be able to accept payments until a bank account is set up.

After specifying this information, there should be a publishable key and secret key available at the following URL.

https://dashboard.stripe.com/test/apikeys

The keys should look similar to the following:

Publishable key: pk_test_adsfasdfERGBH56756BASDFAxvbdg23
Secret key: sk_test_aweirtuyhADSFVB435646Bsdfgkjhfd

The publishable key is used in the client side code to tokenize payment information using Checkout or Stripe.js. The secret key is used in the backend code to send requests to the Stripe API. Therefore, the secret key is very sensitive and needs to be kept secret.

INFO: If the secret key was exposed, whoever has it could refund charges, cancel subscriptions, etc.

Project Initialization

Now we can set up our Node project. First, lets initialize it as an npm project by running npm init es6 -y.

npm init es6 -y

Now lets place our Stripe keys in an environment variable file called .env.

STRIPE_PUBLISHABLE_KEY=pk_test_adsfasdfERGBH56756BASDFAxvbdg23
STRIPE_SECRET_KEY=sk_test_aweirtuyhADSFVB435646Bsdfgkjhfd

Next lets set the entry point to our application and add a simple start script inside package.json.

"main": "src/index.js",
...
"scripts": {
  "start": "node --env-file .env ."
},

INFO: Note that to use the --env-file flag we need to be using Node version 20.11.0 or higher.

Connecting Node to Stripe

To connect Node to Stripe, we need to use the npm package stripe. This package wraps the Stripe API, allowing us to use it to create customers, retrieve invoices, etc.

npm i stripe

Next, we need to import the package and create a Stripe instance, providing the secret key to the constructor.

import Stripe from "stripe";

const STRIPE = new Stripe(process.env.STRIPE_SECRET_KEY);

We can now use this Stripe instance to contact the Stripe API to create customers, retrieve invoices, etc. To demonstrate, lets create a main function that creates a customer and then logs out their details.

async function main() {
  const newCustomer = await STRIPE.customers.create({
    email: 'customer@example.com',
    name: 'John Doe'
  });
  console.log(newCustomer);
}

main();

After running the program, output should be similar to the following:

{
  id: 'cus_QBlDmDBgjf0DcN',
  object: 'customer',
  address: null,
  balance: 0,
  created: 1716893727,
  currency: null,
  default_source: null,
  delinquent: false,
  description: null,
  discount: null,
  email: 'customer@example.com',
  invoice_prefix: '59FF4A6F',
  invoice_settings: {
    custom_fields: null,
    default_payment_method: null,
    footer: null,
    rendering_options: null
  },
  livemode: false,
  metadata: {},
  name: 'John Doe',
  phone: null,
  preferred_locales: [],
  shipping: null,
  tax_exempt: 'none',
  test_clock: null
}

Navigating to the Stripe website, we can also see the presence of this customer inside the dashboard. Check at the following URL.

https://dashboard.stripe.com/test/customers
How to Connect Node to Stripe