Skip to main content

Stripe client_reference_id: a plain guide

What to put in client_reference_id, what Stripe drops without telling you, where it comes back, and copy-ready examples for links and Checkout.

Written by
The Totems team
Published
Updated
Reading time
3 minutes

Short answer

What goes in client_reference_id?

Any text you choose that helps you find the buyer later: a visitor id, a cart id or a customer id. On Payment Links it may hold letters, numbers, dashes and underscores, up to 200 characters. Stripe silently drops anything else. It comes back on the Checkout Session and in checkout.session.completed.

Who this is for. Anyone who found this field in Stripe and wants to use it without breaking something.

On this page

client_reference_id is a luggage tag. You tie it on when the buyer goes into Stripe. You read it when they come out.

What is allowed

Stripe's page for Payment Links gives three rules. Letters, numbers, dashes and underscores only. Up to 200 characters. If a value breaks a rule, it is silently dropped, and the payment page still works.

Kept

  • v_51Xa8Kq2
  • cart-6943
  • user_00192
  • price-video-description

Dropped

  • alex@example.com (has @ and a dot)
  • price video (has a space)
  • a/b/c (has slashes)
  • Anything over 200 characters
Figure 1Values for a Payment Link. The right column is dropped, and the buyer never notices.

Where it goes and comes back

  1. Your button

    Adds the tag

    v_51Xa

  2. Payment Link

    In the address

    client_reference_id

  3. Session

    Stored by Stripe

    cs_test_…

  4. Event

    session.completed

    v_51Xa

Figure 2The tag goes in on the address and comes out in the event.Made-up example

It sits on the Checkout Session. That is a different object from the payment. Stripe says metadata does not copy between objects by itself, apart from a few named cases. So if a tool reads only payments, it won't see your tag.

Add it to every buy button on your page. This reads an id you saved earlier.

Your page
<a class="buy" href="https://buy.stripe.com/YOUR_LINK">Buy</a>
<script>
  const id = localStorage.getItem("visitor_id"); // saved only after a yes
  if (id && /^[A-Za-z0-9_-]{1,200}$/.test(id)) {
    for (const a of document.querySelectorAll("a.buy")) {
      const u = new URL(a.href);
      u.searchParams.set("client_reference_id", id);
      a.href = u;
    }
  }
</script>

Copy-ready: Checkout

Your server
await stripe.checkout.sessions.create({
  mode: "subscription",
  line_items: [{ price: "price_123", quantity: 1 }],
  success_url: "https://studio.example/thanks",
  client_reference_id: visitorId,
  subscription_data: { metadata: { visitor_id: visitorId } },
});

For a one-time payment use payment_intent_data in place of subscription_data.

Read it

cs_test_a1B2

client_reference_id
v_51Xa
metadata.visitor_id
v_51Xa
payment_intent
pi_3Kq
customer_details.email
alex@example.com
payment_status
paid
Figure 3A test event for a made-up buyer. Blank here means the tag was dropped or never sent.Made-up example

Test with a value you know. A blank field looks exactly like success.

A checklist

CheckPass if
Only letters, numbers, - and _Yes
Under 200 charactersYes
New for each visitorYes
Nothing private in itYes
You read it from the session, not the paymentYes
A buyer with no id still paysYes

What can go wrong

  • Email as the id. It has an @ and a dot, so it is dropped on a Payment Link. It's also private.
  • The link is shared. A buyer copies your address with their id and sends it to a friend. Now two people carry one id. Check the email on the payment.
  • Two tools, one field. A booking plugin and a tracking tool can't both use it.
  • Reading too early. Some payment methods confirm days later. Wait for paid.

When not to use Totems

If your own system already knows who is logged in, put your user id in the field and you are done. You don't need a tracking tool.

Totems is for when the buyer is a stranger until they pay. Its script passes the visitor id to the checkouts you choose and matches it when the payment arrives. If you use the field for something else, the Stripe guide shows how to use metadata only. That connection is not yet tested with a live account.

Sources

  1. Stripe: Track a payment link (client_reference_id)Checked
  2. Stripe: The Checkout Session objectChecked
  3. Stripe: MetadataChecked
Read as MarkdownFound a mistake? Tell us: First version, with the limits on Stripe's pages.
Questions

More questions.

Can't find it? Ask us.

Can the buyer see it?

On a Payment Link it is part of the address, so yes. Never put a password, an email or anything private in it.

Can two buyers share one value?

Stripe won't stop you on a link you reuse. But then you can't tell them apart. Use a new value for each visitor.

Is it on the payment or the customer?

It is on the Checkout Session. If you need it on the payment too, also set payment_intent_data.metadata.

What if my shop plugin already uses it?

Leave it alone. Put your id in metadata instead, with your own key, such as visitor_id.

Next note

No. 03

How to connect Stripe payments to your content

Pass one id from your page to Stripe, receive it in a webhook, and join it to the post that sent the buyer. Working code and a test you can run today.

Payments and StripeNo. 14

Stripe Payment Links or Checkout: which tracks better?

4 minute read

YouTube moneyNo. 01

How to track which YouTube video made a sale

8 minute read

Put one link in your next post and follow it. 7 days free.

Start free trialView demo