Testing the Stripe API in test mode

Stripe gives every account a complete test mode: a separate set of keys, test cards that succeed or fail on demand, and no real money involved. It's a great place to learn the API, and the right place to test your integration's edge cases before a real customer finds them.

Step 1: Keys and environment

In the Stripe Dashboard, switch to test mode and copy the secret key from Developers → API keys. It starts with sk_test_.

VariableValue
baseUrlhttps://api.stripe.com/v1
stripeKeysk_test_…, marked as secret

On the collection's Authorization tab, choose Bearer token with {{stripeKey}}.

Step 2: Form-encoded bodies

Stripe's API takes form-encoded bodies, with brackets for nested fields. In the request's Body tab, choose x-www-form-urlencoded and add the fields as rows:

Output
POST {{baseUrl}}/customers

name=Test Customer
email={{$randomExampleEmail}}
metadata[source]=api-test
JavaScript
pm.test('customer created', () => pm.response.to.have.status(200));
pm.environment.set('customerId', pm.response.json().id);

Stripe answers creates with 200, not 201.

Step 3: A successful payment

Create and confirm a PaymentIntent with a test payment method in one call:

Output
POST {{baseUrl}}/payment_intents

amount=4200
currency=eur
customer={{customerId}}
payment_method=pm_card_visa
payment_method_types[]=card
confirm=true
JavaScript
const pi = pm.response.json();
pm.test('payment succeeded', () => {
  pm.response.to.have.status(200);
  pm.expect(pi.status).to.equal('succeeded');
  pm.expect(pi.amount_received).to.equal(4200);
});
pm.environment.set('paymentIntentId', pi.id);

Step 4: Declines and errors

The interesting tests are the failures. Duplicate the request and swap the payment method:

Payment methodWhat happens
pm_card_chargeDeclinedGeneric decline
pm_card_chargeDeclinedInsufficientFundsDeclined: insufficient funds
pm_card_chargeDeclinedExpiredCardDeclined: expired card
JavaScript
pm.test('decline is a 402 card_error', () => {
  pm.response.to.have.status(402);
  const err = pm.response.json().error;
  pm.expect(err.type).to.equal('card_error');
  pm.expect(err.decline_code).to.be.a('string');
});

These are the responses your checkout has to turn into a helpful message, so it's worth knowing their exact shape. Stripe's testing docs list many more test cards, including ones that require 3D Secure.

Other errors to look at once:

  • No key or a wrong key → 401 with error.type: invalid_request_error.
  • A missing required field, such as no currency → 400 naming the parameter in error.param.

Step 5: Idempotency keys

Stripe supports an Idempotency-Key header on every POST. Generate one per logical operation:

JavaScript
// Collection pre-request script
if (pm.request.method === 'POST') {
  pm.request.headers.set('Idempotency-Key', pm.variables.replaceIn('{{$randomUUID}}'));
}

Send the same key twice and Stripe returns the first result instead of charging again. How to test idempotency keys shows the full set of checks.

Step 6: Refund and clean up

Output
POST {{baseUrl}}/refunds

payment_intent={{paymentIntentId}}
JavaScript
pm.test('refund succeeded', () => pm.expect(pm.response.json().status).to.equal('succeeded'));

Test data piles up in test mode. You can delete it all from the Dashboard (Developers → Test data → Delete all test data) whenever it gets in the way.

Pin the API version

Stripe dates its API versions. Your account has a default, and a request can ask for a specific one with the Stripe-Version header. Set it in the collection, to the version your integration uses, so your tests see the same responses your code does.

Run it in CI

Terminal
requestarium run-collection "Stripe" --env StripeTest --env-var stripeKey=STRIPE_TEST_KEY