Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Accounts, Test Mode and API Keys

Learn how a Stripe account is organised, the difference between test and live mode, publishable, secret and restricted keys, and how to load keys safely from the environment.

Before you write any payment code you need an account and a set of API keys. The keys are the credentials your server uses to talk to Stripe, so getting them right, and keeping them secret, is the first security decision of any payment project. This lesson explains what each key is for, how test and live mode differ, and the habits that stop keys from leaking.

Test mode and live mode

Every Stripe account has two completely separate environments, often called sandboxes or modes:

  • Test mode moves no real money. Cards are fake (such as the well-known test card numbers), and you can create, refund and delete things freely.
  • Live mode charges real cards and pays real bank accounts.

The two modes share nothing: a customer, product or subscription created in test mode does not exist in live mode. When you go live you recreate your products and prices (or script that), and you swap the keys.

You can tell which environment a key belongs to by its prefix:

Key Prefix Where it may be used
Publishable, test pk_test_ Browser or mobile app
Publishable, live pk_live_ Browser or mobile app
Secret, test sk_test_ Your server only
Secret, live sk_live_ Your server only
Restricted, test/live rk_test_ / rk_live_ Your server, with limited permissions

Publishable versus secret keys

The publishable key identifies your account to Stripe's browser code. It can only do harmless things, such as turning card details into a token that Stripe holds. It is safe to put in HTML and JavaScript that every visitor can read.

The secret key can do everything: create charges, issue refunds, read customer data, move money. Anyone who has it controls your Stripe account. It must live only on your server, in an environment variable or secrets manager, never in front-end code, never in a Git repository, never in a mobile app.

A restricted key is a secret key with a permission list. A reporting script that only needs to read charges can use a restricted key with "Charges: read" and nothing else. If it leaks, the damage is limited. Prefer restricted keys for every service that does not need full power.

Three kinds of key

Publishable pk_ - safe in the browser
Secret sk_ - server only
Restricted rk_ - limited powers
1 / 2

Loading a key in a real project

In a real Node.js server you read the key from the environment and hand it to the official SDK. This is read-only here, because the stripe npm package is not available in our runner.

JavaScript
// Real Stripe usage (read-only).
import Stripe from "stripe";

const key = process.env.STRIPE_SECRET_KEY;
if (!key) {
  throw new Error("STRIPE_SECRET_KEY is not set");
}

const stripe = new Stripe(key);

Locally you put STRIPE_SECRET_KEY=sk_test_... in a .env file that Git ignores. In production you set it in the host's secret store. Your code never contains the literal key.

A safety net: validate the key at startup

A frequent and expensive mistake is running test keys in production (nobody gets paid) or live keys on a developer laptop (real cards get charged). A small startup check can catch both. This is a stand-in for the check you would write yourself; it needs no network.

Node.js Validate a key before use (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Never print a secret

Logs are copied into tickets, chats and monitoring tools. If a log line contains sk_live_..., treat the key as leaked. Make it a habit to mask secrets before logging anything that might contain them, such as configuration objects or request headers.

Node.js Mask secrets in logs (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

What to do if a key leaks

  1. Roll the key in the Dashboard immediately (Developers, API keys). Stripe lets you keep the old key alive for a short expiry window so you can deploy the replacement without downtime.
  2. Check the event log for unfamiliar API requests during the exposure.
  3. Search your repository history; deleting the line in a new commit does not remove the key from old commits.
  4. Add secret scanning to your CI so it cannot happen quietly again. Stripe also scans public GitHub repositories and may alert you.

Account settings worth setting on day one

  • Business name and support contact: these appear on card statements and receipts.
  • A statement descriptor customers recognise; unrecognised charges become disputes (see Refunds, Disputes and Payouts).
  • Two-factor authentication for every team member, with roles instead of shared logins.

Next you will use a key to make your first API call: Your First API Request.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key may safely appear in front-end JavaScript?

    Only the publishable key (pk_) is designed for browsers. Secret and restricted keys must stay on the server.
  2. 2. You created a product in test mode. What happens when you switch to live keys?

    Test and live data are completely separate. Recreate products and prices in live mode.
  3. 3. A reporting script only reads charges. Which credential is best?

    Restricted keys follow least privilege: if the script leaks, an attacker still cannot refund or create payments.
  4. 4. A secret key was committed to Git. What is the right first step?

    Old commits still contain the key. Rolling it invalidates it. Cleaning history comes afterwards.

Summary

  • Test and live mode are separate worlds with different keys and data.
  • Publishable keys (pk_) are public; secret (sk_) and restricted (rk_) keys stay on the server.
  • Load keys from the environment, validate their mode at startup, and mask them in logs.
  • If a key leaks, roll it first and investigate afterwards.

Next: Your First API Request