Stripe "No such customer" error: test/live, deleted, Connect
Stripe "No such customer" means your key cannot see that cus_ ID: wrong mode, another account, a deleted customer or a missing Stripe-Account header.
The short answer
Stripe returns No such customer: 'cus_...' when the API key making the request cannot see a customer with that ID. The customer usually exists, just somewhere else: in the other mode, in another Stripe account or sandbox, or on a Connect account. The fix is to make the key, the mode and the account match the place where the ID was created, or to create a fresh customer and store the new ID.
Behind the message sits the error code resource_missing. Stripe's reference uses it for any ID it cannot accept, whether the object is missing or the ID belongs to a different kind of object. Two of the causes below also add a hint to the message, so read the whole line before you start digging.
Match your message to a cause
| What you see | Most likely cause | First thing to check |
|---|---|---|
No such customer: 'cus_X'; a similar object exists in test mode, but a live mode key was used to make this request. | The ID was created with a test key and your app now uses a live key | Which key production reads, and where the stored ID came from |
...a similar object exists in live mode, but a test mode key was used... | The reverse: live data, test key, often a staging copy of production | The key on the staging host |
Plain No such customer: 'cus_X' | Another account or sandbox, or a connected account | Run the retrieve command below with the same key |
Retrieve works but returns "deleted": true | The customer was deleted | Who deleted it, and whether your database still points at it |
No such customer: '' or a value that is not a cus_ ID | Your code sent an empty or wrong value | The column or variable that supplies the ID |
A worked example: the copied database
A common way to hit this error has nothing to do with Stripe settings. Picture a small invoicing app, call it Ledgerly, built against a Stripe sandbox. During development every sign-up created a Stripe customer and the app saved the cus_ ID on the user row. On launch day the developer copied the development database to production to keep a few demo accounts, then switched the host to a live secret key.
The first time a demo user clicks Upgrade, the app passes the saved ID to Checkout as customer. The live key has never seen it, and Stripe answers with the test mode hint. Nothing is wrong with the live account. The data in the database belongs to a different world.
The fix in that case is to clear the Stripe customer IDs on every row that came from development, then let the app create a new customer the next time each user pays. Stripe's test mode and live mode keep separate objects, so there is no way to move a sandbox customer to live. You can only recreate it.
Step 1: ask Stripe what the key can see
Run these with the exact key your deployed app uses. Read it from the host's variables rather than from your local .env, because the two are often different. Never paste a live secret key into a chat or a screenshot; the first eight characters are enough to tell the mode.
# The prefix tells you the mode: sk_test_ / rk_test_ or sk_live_ / rk_live_
echo "${STRIPE_SECRET_KEY:0:8}"
# Can this key see the customer from the error?
curl https://api.stripe.com/v1/customers/cus_PpL2x7Qm9aR4tS \
-u "$STRIPE_SECRET_KEY:"
# What customers can it see? Compare with the Dashboard you have open.
curl -G https://api.stripe.com/v1/customers \
-u "$STRIPE_SECRET_KEY:" \
-d limit=3Step 2: work through the four causes
Test against live is the most frequent. Stripe states that test and live mode keys, requests and objects are only available within their own mode, and the error code livemode_mismatch exists for exactly this. If the message carries the similar object hint, you already know the answer. Decide which mode the request should run in, then fix whichever side is wrong: the key on the host, or the stored ID.
Another account or sandbox gives the plain message with no hint, because Stripe will not tell one account about objects in another. This happens when a freelancer built the app on their own account, when you have more than one sandbox, or when an old project's key is still on the host. Compare the customers returned by the list command with the customer list in the Dashboard for the account you expect. If they differ, the key is from somewhere else.
A deleted customer behaves differently from the other cases. Stripe's API reference says deletion is permanent, cannot be undone and immediately cancels any active subscriptions. Unlike most objects, a deleted customer can still be retrieved, and the response contains "deleted": true with a subset of the old fields. What you cannot do is use it: Stripe says deleting a customer prevents any further operations, such as adding a new subscription. So the retrieve in step 1 succeeds while the real request fails. Create a new customer and replace the stored ID.
Connect is the last case. On a platform, a customer can belong to the platform or to a connected account. Server-side requests for a connected account carry the Stripe-Account header with that account's acct_ ID. A customer created with the header lives on the connected account, so asking for it without the header, or with a different account in the header, returns the plain error. Retrieve it with the header to confirm, then make every call for that customer use the same account. If the key cannot reach the account in the header at all, Stripe returns account_invalid with a 403 instead, which is a different problem.
# Connect: retrieve the customer in the connected account's context
curl https://api.stripe.com/v1/customers/cus_PpL2x7Qm9aR4tS \
-u "$STRIPE_SECRET_KEY:" \
-H "Stripe-Account: acct_1QxYzExample"When the ID in the error is empty or odd
Read the value inside the quotes. An empty pair of quotes means your code passed an empty string, which usually comes from a user row that never got a Stripe customer, or a session object that was read before it loaded. A value that starts with acct_, sub_ or pi_ means a field mix-up in your code. Neither is a Stripe problem, and changing keys will not help.
In Checkout, the customer parameter is optional. If you leave it out, a subscription-mode session creates a new customer for you, and a payment-mode session can be told to always create one with customer_creation. So the safest pattern is simple: pass customer only when you hold an ID you have checked, and otherwise let Checkout create one and save the ID it reports.
Make your app recover by itself
A few defensive habits stop this error reaching a customer, even after a messy migration:
- Store the mode next to the ID. Save
livemodefrom the customer object in the same row. If the stored value does not match the key in use, treat the ID as missing and create a new customer. - Catch
resource_missingon customer calls. When the failing parameter is the customer, clear the stored ID, create a fresh customer for that user and retry once. Log it, because a burst of these means a deeper mix-up. - Listen for
customer.deleted. When someone deletes a customer in the Dashboard, your webhook can clear the ID straight away, so nobody hits the error later. - Never seed production from a development database that holds Stripe IDs. Seed with users only, or strip the Stripe columns during the copy.
- Keep one key per environment on the host, and name the variable the same way everywhere so a staging key cannot end up in production.
Confirm the fix
Repeat the retrieve command with the production key and the ID now stored for the affected user. It should return a customer whose livemode matches the key and with no deleted field. Then repeat the action that failed, such as opening Checkout. In the Dashboard, the API request log for that key shows the request and its 200 response, and the customer appears in the customer list of the right account and mode.
If you went live recently, this error tends to travel with two others: the price ID that only exists in test mode, and a webhook endpoint still pointing at the sandbox. The test to live checklist covers both. Shipyard creates the Stripe products, prices and webhook endpoint for a launch in test mode first and moves them to live in a separate step, which keeps keys and IDs from the two modes apart, but cleaning customer IDs out of a copied database stays a job for your own code.
Sources
- Stripe docs: Error codesdocs.stripe.com
- Stripe API reference: Delete a customerdocs.stripe.com
- Stripe API reference: Retrieve a customerdocs.stripe.com
- Stripe docs: Making API calls for connected accountsdocs.stripe.com
- Stripe API reference: Create a Checkout Sessiondocs.stripe.com
Launch in an hour, not a weekend
Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.