Stripe "No such price: a similar object exists in test mode, but a live mode key was used"
By AI App Medic · Updated · Stripe · Payments · Lovable · Going live
Your app took payments in test mode without a problem. You switched to live keys to start charging real customers, and the Buy button now does nothing, shows a generic error, or the Edge Function returns a 500. The function logs or the Stripe request log show this:
No such price: 'price_1QxAbCDeFgHiJkLmNoPqRsTu'; a similar object exists in test mode,
but a live mode key was used to make this request.
type: invalid_request_error
code: resource_missing
param: line_items[0][price]
The same error shows up for other objects: No such customer: 'cus_...', No such product, No such coupon, No such promotion code. Sometimes the modes are swapped and it reads "a similar object exists in live mode, but a test mode key was used".
The message is the diagnosis. The ID is real. Your app asked for it in the wrong mode.
Why it happens
Stripe runs every request either in test mode (now called a sandbox in newer accounts) or in live mode, and each mode has its own keys and its own data. Products, prices, customers, coupons, payment links, webhook endpoints, tax rates and the customer portal settings all exist separately in each mode, with different IDs. A price_ ID you created while testing does not exist for a live key.
Lovable, Bolt, Cursor and v0 all start with test mode, which is the right call. The test IDs then end up hard-coded in a pricing component, in an Edge Function, in an environment variable or in your database. Going live means swapping all of them together, and the AI assistant usually swaps only the keys when you ask it to "switch Stripe to live".
Step 1: read the failed request in Stripe
Switch the Stripe Dashboard to live mode (turn off the test mode or sandbox toggle), then open Developers, Logs. On newer dashboards this lives in Workbench at the bottom of the page. Filter by status 400 and open the failed POST /v1/checkout/sessions or POST /v1/subscriptions request. You get three useful facts:
- The object type and ID.
price_,cus_,prod_or a coupon. This tells you which value is still from test mode. - The parameter.
line_items[0][price]means the price,customermeans a stored customer ID,discounts[0][coupon]means a coupon. - The key. The log shows which API key made the call, so you can see whether the server is really using the live key you think it is.
If the request does not appear in the live log at all, the server is still on a test key. Switch the dashboard back to test mode and you will probably find it there.
Step 2: check every key, in every place the app reads it
The prefix tells you the mode:
- Secret key, server only:
sk_test_orsk_live_. - Restricted key, server only:
rk_test_orrk_live_. - Publishable key, browser:
pk_test_orpk_live_.
The secret key usually sits in Supabase Edge Function secrets (Project Settings, Edge Functions, Secrets, or the Stripe settings in Lovable, which write to the same place). List what is set without printing values:
supabase secrets list
supabase secrets set STRIPE_SECRET_KEY=sk_live_...
The publishable key and any price IDs the front end uses are build-time variables such as VITE_STRIPE_PUBLISHABLE_KEY or NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY. They are baked into the JavaScript when the site is built, so changing them on Netlify or Vercel does nothing until you rebuild. Check the Production scope, not only Preview. Bolt env variables undefined on Netlify and Next.js env variables undefined on Vercel explain the build step in detail.
A live secret key must never be in front-end code or in a VITE_ or NEXT_PUBLIC_ variable. If the AI put it there, rotate the key in Stripe first. See exposed API keys in Vite apps.
To confirm what a deployed function really uses, log the mode, never the key:
const key = Deno.env.get('STRIPE_SECRET_KEY') ?? '';
console.log('stripe mode:', key.startsWith('sk_live_') || key.startsWith('rk_live_') ? 'live' : 'test');
Step 3: create the products and prices in live mode
You do not have to rebuild the catalogue by hand. In the Stripe Dashboard, in test mode, open each product and use Copy to live mode. Stripe creates a new live product with new prices. The copy is one-off: later edits to the test product do not carry over.
Then switch to live mode, open the copied product and copy the new price_ IDs. They are different from the test ones. Do the same for coupons, promotion codes, shipping rates and tax rates if the checkout uses them.
Step 4: replace every test ID the app stores
This is the step that gets skipped. Search the whole project, including the functions folder:
grep -rnE "price_|prod_|cus_|promo_|plink_" src supabase/functions app lib 2>/dev/null
Move every hard-coded price ID into configuration. A small map in the Edge Function, filled from secrets, keeps the browser from choosing arbitrary prices:
// supabase/functions/create-checkout/index.ts
import Stripe from 'npm:stripe@17';
const stripe = new Stripe(Deno.env.get('STRIPE_SECRET_KEY')!);
// The browser sends a plan name, never a raw price ID.
const PRICES: Record<string, string | undefined> = {
pro_monthly: Deno.env.get('STRIPE_PRICE_PRO_MONTHLY'),
pro_yearly: Deno.env.get('STRIPE_PRICE_PRO_YEARLY'),
};
Deno.serve(async (req) => {
const { plan } = await req.json();
const price = PRICES[plan];
if (!price) return new Response('Unknown plan', { status: 400 });
// ... look up the signed-in user here, see Step 5 for the customer
const session = await stripe.checkout.sessions.create({
mode: 'subscription',
line_items: [{ price, quantity: 1 }],
success_url: 'https://yourapp.com/billing?status=success',
cancel_url: 'https://yourapp.com/billing',
});
return Response.json({ url: session.url });
});
Then set the live values with supabase secrets set STRIPE_PRICE_PRO_MONTHLY=price_... and redeploy the function.
If you would rather not manage two sets of IDs at all, use lookup keys. Give the test price and the live price the same lookup key in the dashboard (for example pro_monthly), and resolve it at runtime:
const { data } = await stripe.prices.list({ lookup_keys: ['pro_monthly'], active: true });
if (!data.length) throw new Error('No active price with lookup key pro_monthly in this mode');
const price = data[0].id;
Every Stripe object also has a livemode field. A cheap guard at startup catches a mix-up before a customer does:
const p = await stripe.prices.retrieve(price);
const keyIsLive = (Deno.env.get('STRIPE_SECRET_KEY') ?? '').includes('_live_');
if (p.livemode !== keyIsLive) throw new Error('Stripe price and key are in different modes');
Step 5: deal with test customer IDs in your database
If the error says No such customer: 'cus_...', the app saved a Stripe customer ID for each user while you were testing, usually in a stripe_customer_id column on profiles. Those customers do not exist in live mode. You, and anyone who tried the app during testing, hit the error on the first live checkout.
Two ways out. Clear the test IDs once, after checking which rows you are about to change:
select id, stripe_customer_id from public.profiles where stripe_customer_id is not null;
update public.profiles set stripe_customer_id = null where stripe_customer_id is not null;
Only do that if no real live customers exist yet. Once you have any, make the function self-healing instead: try the stored customer, and create a new one if Stripe says it is missing or deleted:
async function getCustomerId(userId: string, email: string, stored: string | null) {
if (stored) {
try {
const c = await stripe.customers.retrieve(stored);
if (!('deleted' in c && c.deleted)) return stored;
} catch (err) {
if ((err as { code?: string }).code !== 'resource_missing') throw err;
}
}
const c = await stripe.customers.create({ email, metadata: { user_id: userId } });
// save c.id to profiles.stripe_customer_id with the service role client
return c.id;
}
Keeping a separate column per mode, or a separate Supabase project for testing, avoids the problem next time.
Step 6: the live webhook and the customer portal
Webhook endpoints are also per mode. After the checkout works, payments can succeed in Stripe while your app never upgrades anyone, because only the test endpoint exists. In live mode, add an endpoint pointing at your production function, subscribe it to the same events (checkout.session.completed, customer.subscription.updated, customer.subscription.deleted, invoice.paid and so on), and store its new signing secret:
supabase secrets set STRIPE_WEBHOOK_SECRET=whsec_...
The live secret is different from the test one and from the one the Stripe CLI prints. If the webhook then returns 400 or 401, follow Stripe webhook not working in a Supabase Edge Function.
If you use the Stripe customer portal for "Manage subscription", save the portal settings once in live mode (Settings, Billing, Customer portal). Until you do, creating a portal session in live mode fails with an error saying no default configuration has been created.
Step 7: test with a real card
Test cards such as 4242 4242 4242 4242 are declined in live mode, so the only real test is a real purchase. Buy the cheapest plan with your own card, then check four things: the payment appears in the live dashboard, the webhook delivery shows 200, your database row was updated, and the app gave you the plan. Refund the payment from the dashboard afterwards.
If live payments fail with a message about the account not being able to make live charges, the Stripe account is not fully activated yet. Finish the business and bank details in the dashboard first.
Prevention
- Keep keys, price IDs and the webhook secret together in configuration, never in source files, with one complete set per environment.
- Have the browser send a plan name, not a price ID, and map it on the server.
- Use lookup keys or a startup check on
livemodeso a mismatch fails loudly at deploy time. - Write a short go-live checklist: secret key, publishable key, prices, coupons, webhook endpoint, webhook secret, portal settings, rebuild, real purchase. Run it every time you change modes.
FAQ
What does 'a similar object exists in test mode, but a live mode key was used' mean?
The ID your app sent is real, but it belongs to test mode, and the request was made with a live secret key. Stripe keeps test and live data completely separate, so the live account has no object with that ID and answers with an invalid_request_error whose code is resource_missing.
I changed both keys to live. Why do I still get No such price?
The keys are only half of it. The price ID itself was created in test mode and does not exist in live mode. Create the price in live mode, copy its new ID, put it where the app reads it and redeploy. If the error mentions a customer instead, a test customer ID saved in your database is being reused.
Can I use the same price ID in test and live mode?
No. Copy to live mode creates a new live product with new price IDs. If you do not want to keep two sets of IDs, give both prices the same lookup key, such as pro_monthly, and fetch the price by lookup key at runtime.
What does 'a similar object exists in live mode, but a test mode key was used' mean?
The reverse case. A local build, a preview deployment or a test environment still uses a test key, while the ID it sends was created in live mode. Give each environment its own set of keys and IDs and keep them in the same mode.
Do I need a new webhook when I go live?
Yes. Webhook endpoints and their signing secrets are separate per mode. Create the endpoint again in live mode, subscribe it to the same events and store its whsec_ secret. Without it, payments succeed but your app never hears about them.
Related guides
- Stripe webhook not working in a Supabase Edge Function: 401s, signature errors and missing upgrades
- Bolt.new works in preview but breaks on Netlify: missing environment variables
- Supabase Edge Function returns 401 "Invalid JWT": find who rejected the call and fix it
- Lovable app slow first load: find the huge bundle and cut it down
- Lovable app shows a blank white page after deploy: causes and fixes