Stripe client_reference_id: what it is and how to use it
By Devendra Variya, co-founder of Virling. Published 2026-11-24, updated 2026-12-01.
Stripe's client_reference_id is a string of your own, up to 200 characters, that you put on a Checkout Session so you can match the payment to a user, cart or visit in your system. Set it when your server creates the session, as a query parameter on a Payment Link, or as an attribute of a pricing table. Read it back from the checkout.session.completed webhook. Use metadata instead when you need more than one value, or need the value on the payment itself.
The IDs on this page, such as user_1042, are made up.
What the field is, and its limits
Stripe describes it as a unique string to reference the Checkout Session, such as a customer ID or a cart ID, that you can use to reconcile the session with your own systems (Checkout Session object). Stripe's docs give these limits:
- Length: up to 200 characters, wherever you set it (create a Checkout Session).
- Characters on a Payment Link, pricing table or buy button: letters, digits, dashes and underscores. An invalid value is dropped without an error and the page keeps working (Payment Link URL parameters).
- Characters through the API: none listed. Keep to the same set anyway, so one ID works everywhere.
- No secrets. Stripe asks you not to put passwords or API keys in the parameter.
Where client_reference_id appears
- Checkout Session: the
client_reference_idfield, or null when it was not set. Your server sets it on create. - Webhook:
data.object.client_reference_idin the Checkout Session events, such ascheckout.session.completed. - Payment Links: the
client_reference_idquery parameter on thebuy.stripe.comURL. - Pricing table: the
client-reference-idattribute, with dashes, on<stripe-pricing-table>(pricing table docs). - Buy button: the same attribute on
<stripe-buy-button>(buy button docs).
It is not on the PaymentIntent, the Charge or the Subscription, so events such as payment_intent.succeeded do not carry it.
Set it on a Checkout Session
Your server knows who is paying, so it sets the ID on create:
const session = await stripe.checkout.sessions.create({
client_reference_id: user.id, // such as "user_1042"
line_items: [{ price: "price_...", quantity: 1 }],
mode: "payment",
success_url: "https://yourdomain.com/thanks",
});Set it on a Payment Link or pricing table
A Payment Link takes it from the URL:
<a href="https://buy.stripe.com/abc123?client_reference_id=user_1042">Buy now</a>
When the ID is known only in the browser, add it to the link in JavaScript:
const url = new URL("https://buy.stripe.com/abc123");
url.searchParams.set("client_reference_id", user.id);
document.querySelector("#buy").href = url.href;A pricing table takes it as an attribute:
<stripe-pricing-table pricing-table-id="prctbl_..." publishable-key="pk_live_..." client-reference-id="user_1042"> </stripe-pricing-table>
To carry the marketing source this way, see Stripe Payment Link UTM tracking and Stripe pricing table UTM tracking.
Read it in a webhook handler
This Express handler verifies the signature on the raw body, then handles both events from Stripe's fulfillment guide. The second one covers payment methods that settle later:
// Register this route before app.use(express.json())
app.post("/webhook", express.raw({ type: "application/json" }), async (req, res) => {
let event;
try {
event = stripe.webhooks.constructEvent(
req.body,
req.headers["stripe-signature"],
process.env.STRIPE_WEBHOOK_SECRET,
);
} catch {
return res.sendStatus(400);
}
if (
event.type === "checkout.session.completed" ||
event.type === "checkout.session.async_payment_succeeded"
) {
const session = event.data.object;
if (session.client_reference_id && session.payment_status !== "unpaid") {
// Your function: must be safe to run twice for the same session
await fulfill(session.client_reference_id, session.id);
}
}
res.sendStatus(200);
});Without a webhook, find the session from the payment and read the field:
const sessions = await stripe.checkout.sessions.list({
payment_intent: "pi_...",
limit: 1,
});
const id = sessions.data[0]?.client_reference_id;When to use metadata instead
Metadata on a Checkout Session holds up to 50 keys, with key names up to 40 characters and values up to 500 (Stripe metadata docs). Choose it when:
- You need several values, such as a plan, a team ID and UTM tags. See Stripe Checkout UTM parameters.
- You need the value on the payment or subscription. Session metadata is not copied, so set
payment_intent_data.metadataorsubscription_data.metadata. - You need to change it later. The update endpoint takes
metadataand does not listclient_reference_id. - You take payments without Checkout. A PaymentIntent of your own has metadata and no
client_reference_id.
Choose client_reference_id when the ID has to travel through a Payment Link, a pricing table or a buy button. You can use both on one session.
How Virling reads it
Virling credits a Stripe payment to the visits behind it. It connects with a read-only restricted key, so it needs no webhook handler, and looks for a visitor ID in this order:
metadata.virling_vidon the charge.metadata.virling_vidorclient_reference_idon the Checkout Session that created it. The metadata wins when both are set.metadata.virling_vidon its PaymentIntent.
The visitor ID is in the first-party virling_vid cookie that the script sets, and server-side revenue tracking covers reading it on your server. Put it in client_reference_id, or keep that field for your own ID and put the visitor ID in metadata:
const session = await stripe.checkout.sessions.create({
client_reference_id: user.id,
metadata: { virling_vid: req.cookies.virling_vid },
line_items: [{ price: "price_...", quantity: 1 }],
mode: "subscription",
success_url: "https://yourdomain.com/thanks",
});On Payment Links there is nothing to write: the script adds client_reference_id to every buy.stripe.com link on the page, unless the link already has one. The checkout snippet generator writes the code for your setup, the steps are in the Stripe docs, and the full setup is on the Stripe integration page. The buyer's return from Stripe to your success_url continues the visit in Virling if it is under 30 minutes since the last event, while GA4 needs a setting for it, as checkout.stripe.com referral traffic explains.
Quick reference: every Stripe surface
Where the visitor ID goes on each way of taking a Stripe payment, which webhook event shows it, and what Virling reads. Virling reads the charges and events lists with its restricted key, so you need no webhook for it; the event column is for your own handler.
| Surface | client_reference_id? | Where the visitor ID goes | Webhook event that carries it | What Virling reads |
|---|---|---|---|---|
| Checkout Session from your server | Yes, on create | client_reference_id, or metadata.virling_vid when the field holds your own ID | checkout.session.completed | The session, found from the charge's PaymentIntent, or from the subscription for its first invoice |
| Payment Link | Yes, as a URL parameter | ?client_reference_id= on the buy.stripe.com link. The Virling script adds it. | checkout.session.completed | The session the link created, found the same way |
| Pricing table | Yes, as an attribute | client-reference-id on <stripe-pricing-table>. The script does not add it, so set it from the cookie or window.virling?.vid?.(). | checkout.session.completed | The session the table created, found the same way |
| Elements with your own PaymentIntent | No | metadata.virling_vid on the PaymentIntent, set on create. Stripe copies it to the charge. | payment_intent.succeeded and charge.succeeded | The charge's metadata, then the PaymentIntent's |
| Invoice without a subscription | No | metadata.virling_vid on the invoice's PaymentIntent, after you finalize the invoice and before it is paid. Invoice metadata is not read. | payment_intent.succeeded and charge.succeeded; invoice.paid has only the invoice's own metadata | The charge's metadata, then the PaymentIntent's, or a match by payer email |
A worked example: one ID, three surfaces
Example data, made up to show the check. An imaginary SaaS sells a $36 monthly plan through a Payment Link on its blog, a pricing table on its pricing page, and a Checkout Session its app creates for signed-in users. One month of first payments looked like this in Virling:
| Surface | Payments | What was in client_reference_id | Revenue | In Virling |
|---|---|---|---|---|
| Payment Link | 41 | The visitor ID, added by the script | $1,476 | Credited to the visits behind them |
| Pricing table | 18 | Nothing: the attribute was not set | $648 | Unattributed |
| Checkout Session from the app | 23 | The app's user ID | $828 | Unattributed: the ID matched no visit |
| Total | 82 | $2,952 | $1,476 credited |
Half of the revenue had a source. Two changes fix the rest for new payments: a client-reference-id attribute on the pricing table, and metadata.virling_vid on the app's sessions next to the user ID it already sends. Payments made before the fix keep no visitor ID and stay Unattributed. The checkout snippet generator has the Stripe code for Checkout Sessions, PaymentIntents and Payment Links, and the session with both fields is in the code under How Virling reads it.
Usual mistakes
- An email address on a Payment Link. The
@and the dot are not allowed, so Stripe drops the value and you get null. - The wrong spelling. It is
client_reference_idin the API and the URL, andclient-reference-idas an HTML attribute. - Reading it from the wrong event. Payment, invoice and subscription events do not have it. Read it from the session, and save it with the subscription ID if you need it at renewal.
- Trusting it from a link. A buyer can edit a URL before paying. Check that the ID exists, and do not use it as proof of who the buyer is.
- Expecting it on every session. A Payment Link opened from an email or a QR code carries only what is in that URL. Handle null.
- Parsing the body before verifying. Signature checks need the raw body (Stripe webhook docs).
- Running fulfillment twice. Stripe's guide warns that it can run more than once for one session, so make it safe to repeat.
- Your own ID where the visitor ID should be. With Virling, a user ID in
client_reference_idis read as a visitor ID, matches no visit, and the payment shows as Unattributed unless the session'smetadata.virling_vidis set.
Frequently asked questions
What is client_reference_id in Stripe?
It is a string you set on a Checkout Session to tie it to a record in your own system, such as a customer ID or a cart ID. Stripe stores it on the session and includes it in the checkout.session.completed event. It is optional, and null when you do not set it.
What characters and length does client_reference_id allow?
Up to 200 characters. On Payment Links, pricing tables and buy buttons, only letters, digits, dashes and underscores are allowed, and Stripe drops an invalid value without an error. The API reference for Checkout Sessions gives only the length.
Should I use client_reference_id or metadata?
Use client_reference_id for one ID, above all on a Payment Link, a pricing table or a buy button, where the buyer's page can set it. Use metadata when you need several values, or values on the PaymentIntent or Subscription. Session metadata is not copied to those, so set payment_intent_data.metadata or subscription_data.metadata.
Is client_reference_id on the PaymentIntent or Subscription?
No. It is a field of the Checkout Session only. To get it from a payment, list Checkout Sessions with the payment_intent or subscription filter and read the session.
Can I search for a client_reference_id in Stripe?
Not through the API. Listing Checkout Sessions has no client_reference_id filter, and Checkout Sessions are not among the resources in the Search API docs. Save the session ID with your own ID when you create the session or when the webhook arrives.
Can I change client_reference_id after the session is created?
The update endpoint for Checkout Sessions does not list client_reference_id among its parameters, so set it when you create the session. It does accept metadata, so a value you need to change later belongs there.
See which visit is behind each Stripe payment
Virling is open source under AGPL. Self-host it free, or use the hosted version with a 14-day free trial. It connects to Stripe, Paddle, Shopify, WooCommerce, Polar, LemonSqueezy, Dodo, Creem and RevenueCat.