# How to fix the 'no such customer' error in Stripe

- Tool: Stripe
- Difficulty: Beginner
- Time required: 10 minutes
- Compatibility: Stripe API v2024-12+, Node.js 18+
- Last updated: March 2026

## TL;DR

The 'No such customer' error in Stripe means you're referencing a customer ID that doesn't exist in the current mode. The most common cause is creating a customer in test mode but trying to charge them in live mode, or vice versa. Fix it by ensuring your API keys and customer IDs are from the same Stripe environment.

## Why Stripe Says 'No Such Customer'

Stripe test mode and live mode maintain completely separate databases. A customer created with sk_test_ only exists in test mode. If you try to reference that customer ID (cus_...) using a live mode key (sk_live_), Stripe returns 'No such customer'. This also happens when a customer was deleted, the ID is mistyped, or you're referencing a customer from a different Stripe account.

## Before you start

- A Stripe account with access to the Dashboard
- Node.js 18+ with the Stripe npm package installed
- A stored customer ID to verify

## Step-by-step guide

### 1. Identify which mode you're operating in

Check your API key prefix. sk_test_ means test mode; sk_live_ means live mode. All data created with one key type is invisible to the other.

```
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
console.log('Mode:', process.env.STRIPE_SECRET_KEY.startsWith('sk_test_') ? 'TEST' : 'LIVE');
```

**Expected result:** Console outputs 'TEST' or 'LIVE' confirming your current mode.

### 2. Look up the customer in the Dashboard

Go to Stripe Dashboard → Customers → search for the customer ID (cus_...). Make sure the test/live mode toggle at the top matches the mode where the customer was created. If you're in test mode, you won't see live mode customers.

**Expected result:** You find the customer in the correct mode, or confirm it doesn't exist in the mode you're using.

### 3. Verify the customer exists via API

Use stripe.customers.retrieve() to check if the customer ID is valid for your current API key. This confirms whether the mismatch is mode-related or the customer was deleted.

```
async function checkCustomer(customerId) {
  try {
    const customer = await stripe.customers.retrieve(customerId);
    if (customer.deleted) {
      console.log('Customer was deleted');
    } else {
      console.log('Customer found:', customer.email);
    }
  } catch (err) {
    if (err.code === 'resource_missing') {
      console.error('Customer does not exist in this mode');
    } else {
      console.error('Error:', err.message);
    }
  }
}

checkCustomer('cus_your_customer_id');
```

**Expected result:** The function confirms whether the customer exists, was deleted, or is missing from the current mode.

### 4. Fix the mode mismatch in your application

Ensure your app uses the correct API key for each environment. Store test keys in your development .env and live keys in production environment variables. Never mix them.

```
# .env.development
STRIPE_SECRET_KEY=sk_test_51ABC...

# .env.production
STRIPE_SECRET_KEY=sk_live_51ABC...
```

**Expected result:** Your development environment uses test keys and production uses live keys. No cross-mode references occur.

### 5. Add a safety check before using customer IDs

Add a validation step that catches mismatched customer references early, before they cause errors in payment flows.

```
async function safeGetCustomer(customerId) {
  try {
    const customer = await stripe.customers.retrieve(customerId);
    if (customer.deleted) {
      throw new Error(`Customer ${customerId} has been deleted`);
    }
    return customer;
  } catch (err) {
    if (err.code === 'resource_missing') {
      throw new Error(
        `Customer ${customerId} not found. Check if you're using the correct test/live API key.`
      );
    }
    throw err;
  }
}
```

**Expected result:** Your code provides clear error messages that help identify mode mismatches instead of failing with a generic Stripe error.

## Complete code example

File: `customer-check.js`

```javascript
require('dotenv').config();
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

const isTestMode = process.env.STRIPE_SECRET_KEY.startsWith('sk_test_');
console.log(`Running in ${isTestMode ? 'TEST' : 'LIVE'} mode`);

async function safeGetCustomer(customerId) {
  if (!customerId || !customerId.startsWith('cus_')) {
    throw new Error(`Invalid customer ID format: ${customerId}`);
  }

  try {
    const customer = await stripe.customers.retrieve(customerId);
    if (customer.deleted) {
      throw new Error(`Customer ${customerId} has been deleted`);
    }
    return customer;
  } catch (err) {
    if (err.code === 'resource_missing') {
      throw new Error(
        `Customer ${customerId} not found in ${isTestMode ? 'test' : 'live'} mode. ` +
        'Verify the customer exists in this environment.'
      );
    }
    throw err;
  }
}

async function createPaymentForCustomer(customerId, amount, currency) {
  const customer = await safeGetCustomer(customerId);
  console.log(`Creating payment for ${customer.email}`);

  const paymentIntent = await stripe.paymentIntents.create({
    amount,
    currency: currency || 'usd',
    customer: customerId,
    automatic_payment_methods: { enabled: true },
  });

  return paymentIntent;
}

module.exports = { safeGetCustomer, createPaymentForCustomer };
```

## Common mistakes

- **Creating customers in test mode and charging them with live mode keys** — undefined Fix: Test and live mode are completely separate databases. Customers created with sk_test_ only exist in test mode. You must recreate customers in live mode for production.
- **Storing the customer ID but not the mode it was created in** — undefined Fix: Always store which Stripe environment a customer belongs to in your database, or use separate database fields for test and live customer IDs.
- **Assuming deleted customers still exist** — undefined Fix: Check customer.deleted in the API response. Deleted customers return a minimal object with deleted: true instead of a resource_missing error.
- **Copying customer IDs from a different Stripe account** — undefined Fix: Customer IDs are unique per Stripe account. A cus_ ID from one account doesn't exist in another, even if both are in the same mode.

## Best practices

- Always use matching API keys and data — test keys with test data, live keys with live data
- Store the Stripe environment (test/live) alongside customer IDs in your database
- Validate customer IDs before using them in payment flows
- Use a helper function that provides clear error messages for missing customers
- Log the current API mode at server startup for quick debugging
- When going live, create new customers instead of trying to migrate test data

## Frequently asked questions

### Can I move a customer from test mode to live mode?

No. Test and live mode are completely separate. You need to create new customers in live mode. There is no migration path between test and live data in Stripe.

### Why does my customer ID work locally but not in production?

Your local environment likely uses sk_test_ while production uses sk_live_. Customers created in test mode don't exist in live mode. Create new customers using your live key.

### How can I tell if a customer was deleted?

Call stripe.customers.retrieve(id). If the customer was deleted, the response includes a 'deleted: true' field. It does not throw a resource_missing error — only customers that never existed or belong to another mode throw that.

### What does the cus_ prefix mean?

All Stripe customer IDs start with cus_ followed by a unique alphanumeric string. The prefix doesn't indicate test or live mode — you must check the API key used to create or access the customer.

### I accidentally deleted a customer in Stripe. Can I recover them?

No. Stripe does not support undeleting customers. You need to create a new customer. Their payment methods, subscriptions, and invoices associated with the deleted customer are also removed.

---

Source: https://www.rapidevelopers.com/stripe-guide/how-to-fix-no-such-customer-error-in-stripe
© RapidDev — https://www.rapidevelopers.com/stripe-guide/how-to-fix-no-such-customer-error-in-stripe
