virling Pricing Self-host Demo Changelog Agencies GitHub Start free trial
blog / 2026-11-24

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_id field, or null when it was not set. Your server sets it on create.
  • Webhook: data.object.client_reference_id in the Checkout Session events, such as checkout.session.completed.
  • Payment Links: the client_reference_id query parameter on the buy.stripe.com URL.
  • Pricing table: the client-reference-id attribute, 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.metadata or subscription_data.metadata.
  • You need to change it later. The update endpoint takes metadata and does not list client_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:

  1. metadata.virling_vid on the charge.
  2. metadata.virling_vid or client_reference_id on the Checkout Session that created it. The metadata wins when both are set.
  3. metadata.virling_vid on 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.

Start free trial

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.

Surfaceclient_reference_id?Where the visitor ID goesWebhook event that carries itWhat Virling reads
Checkout Session from your serverYes, on createclient_reference_id, or metadata.virling_vid when the field holds your own IDcheckout.session.completedThe session, found from the charge's PaymentIntent, or from the subscription for its first invoice
Payment LinkYes, as a URL parameter?client_reference_id= on the buy.stripe.com link. The Virling script adds it.checkout.session.completedThe session the link created, found the same way
Pricing tableYes, as an attributeclient-reference-id on <stripe-pricing-table>. The script does not add it, so set it from the cookie or window.virling?.vid?.().checkout.session.completedThe session the table created, found the same way
Elements with your own PaymentIntentNometadata.virling_vid on the PaymentIntent, set on create. Stripe copies it to the charge.payment_intent.succeeded and charge.succeededThe charge's metadata, then the PaymentIntent's
Invoice without a subscriptionNometadata.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 metadataThe 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:

SurfacePaymentsWhat was in client_reference_idRevenueIn Virling
Payment Link41The visitor ID, added by the script$1,476Credited to the visits behind them
Pricing table18Nothing: the attribute was not set$648Unattributed
Checkout Session from the app23The app's user ID$828Unattributed: the ID matched no visit
Total82$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_id in the API and the URL, and client-reference-id as 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_id is read as a visitor ID, matches no visit, and the payment shows as Unattributed unless the session's metadata.virling_vid is 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.

Start free trial See the demo