Metadata you set on a Stripe Checkout Session is empty when you read it
metadata is {} on payment_intent.succeeded after setting it on the Checkout SessionShort answer
Stripe does not copy metadata between objects on its own. Metadata set on a Checkout Session stays on the session; the PaymentIntent behind it only gets metadata you pass in payment_intent_data.metadata. Read it from the checkout.session.completed event, or set it on the PaymentIntent too, and use a secret key when you retrieve it, because client-side keys get metadata redacted.
Fix it fast
1Find out which cause you have
Print the type of every event your webhook receives and the metadata it carries. The event type tells you which object you are reading, and whether you set metadata on that object.
@app.post("/webhook") def webhook(): event = stripe.Webhook.construct_event( request.data, request.headers["Stripe-Signature"], WEBHOOK_SECRET) print(event["type"], dict(event["data"]["object"].get("metadata", {}))) return "", 2002Match what you see, then jump to its fix
- 1
checkout.session.completed shows your keys; payment_intent.succeeded or charge.succeeded shows {}.Metadata was set on the session, but you read it from the PaymentIntent or chargeFix - 2
The log shows only payment_intent.* or charge.* events, never checkout.session.completed.The handler is not subscribed to checkout.session.completedFix - 3
Retrieving the same session on the server shows the metadata, but a client-side call does not.You retrieve the session with a publishable keyFix
- 1
What does the error look like?
metadata is {} on payment_intent.succeeded after setting it on the Checkout Sessionevent.data.object.metadata.order_id is None in the webhook handler
Cause 1 of 3
Metadata was set on the session, but you read it from the PaymentIntent or charge
1Check
An object's metadata is not copied to related objects. Stripe only copies in a few listed cases (PaymentIntent to Charge, Payment Link to Checkout Session, and similar). Checkout Session to PaymentIntent is not one of them unless you use payment_intent_data.
grep -n "metadata" app.py2Fix
Either read the metadata from the checkout.session.completed event, or also set it on the PaymentIntent that the session creates (payment_intent_data.metadata; for subscriptions use subscription_data.metadata).
session = stripe.checkout.Session.create(
mode="payment",
line_items=[{"price": PRICE_ID, "quantity": 1}],
success_url="https://example.com/success",
metadata={"order_id": "6943"},
payment_intent_data={"metadata": {"order_id": "6943"}},
)3Verify
Pay with a test card and look at both events.
stripe listen --forward-to localhost:4242/webhookConfirmed by docs.stripe.com
Cause 2 of 3
The handler is not subscribed to checkout.session.completed
1Check
Stripe sends the session's metadata inside the checkout.session.completed event, and only to endpoints configured to receive that event type.
stripe listen --forward-to localhost:4242/webhook2Fix
Add checkout.session.completed to the endpoint's enabled events and branch on event type in the handler.
if event["type"] == "checkout.session.completed":
session = event["data"]["object"]
order_id = session["metadata"]["order_id"]3Verify
Create a test session and complete it; the handler logs the order id.
stripe trigger checkout.session.completedConfirmed by docs.stripe.com
Cause 3 of 3
You retrieve the session with a publishable key
1Check
Stripe returns metadata only for requests made with a secret key. Requests with a publishable key, such as Stripe.js or mobile SDK calls, get it redacted.
grep -rn "pk_" static/ templates/ app.py2Fix
Retrieve the session in your Flask route with your secret key and send the browser only what it needs.
stripe.api_key = os.environ["STRIPE_SECRET_KEY"]
session = stripe.checkout.Session.retrieve(session_id)
print(session["metadata"])3Verify
Call the route and check that the metadata keys are present.
curl -s http://localhost:5000/session/cs_test_123Confirmed by docs.stripe.com
How do I stop it coming back?
- Keep metadata small: Stripe allows 50 keys, key names up to 40 characters and values up to 500 characters. Store a database id, not the whole order.
- Decide per integration which object holds the id you need, and read from that object only.
- Never put card or bank details in metadata.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
Related fixes
Payments
Stripe webhook: no signatures found matching the expected signature
No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
Stripe signs the exact bytes it sends, using that endpoint's own secret.
5 causes, each with a fixPayments
firebase deploy fails because the Stripe client has no API key
Neither apiKey nor config.authenticator provided
The Stripe client is built from a key that is empty at that moment.
3 causes, each with a fixPayments
Stripe Elements shows console warnings or errors in a Next.js checkout
Could not find Elements context; You need to wrap the part of your app that calls useElements() in an <Elements> provider.
Most React Stripe.js warnings come from four mistakes: calling loadStripe inside a component, using useStripe or useElements outside the Elements provider, using the objects before they have loaded, and a Content Security Policy that blocks js.stripe.com.
4 causes, each with a fixPayments
Stripe says No such checkout.session when you retrieve a session
resource_missing
A resource_missing error means the ID is not visible to the key you used.
3 causes, each with a fixPayments
Stripe says there is no such token when your server uses it
livemode_mismatch
Stripe answers with a missing-resource error when the ID you send does not exist for the key you use.
3 causes, each with a fixPayments
A payment method is missing from the Stripe Payment Element, or the call to exclude it fails
Excluding apple_pay, google_pay or link with excluded_payment_method_types returns an error
With automatic payment methods, Stripe decides which methods the Payment Element shows.
5 causes, each with a fix