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_.
| Variable | Value |
|---|---|
baseUrl | https://api.stripe.com/v1 |
stripeKey | sk_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:
POST {{baseUrl}}/customers
name=Test Customer
email={{$randomExampleEmail}}
metadata[source]=api-testpm.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:
POST {{baseUrl}}/payment_intents
amount=4200
currency=eur
customer={{customerId}}
payment_method=pm_card_visa
payment_method_types[]=card
confirm=trueconst 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 method | What happens |
|---|---|
pm_card_chargeDeclined | Generic decline |
pm_card_chargeDeclinedInsufficientFunds | Declined: insufficient funds |
pm_card_chargeDeclinedExpiredCard | Declined: expired card |
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 →
401witherror.type: invalid_request_error. - A missing required field, such as no
currency→400naming the parameter inerror.param.
Step 5: Idempotency keys
Stripe supports an Idempotency-Key header on every POST. Generate one per logical operation:
// 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
POST {{baseUrl}}/refunds
payment_intent={{paymentIntentId}}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
requestarium run-collection "Stripe" --env StripeTest --env-var stripeKey=STRIPE_TEST_KEY