DocsStorefront integrationShow the referring rep
Coming soon Kwik's public API and MCP server are coming soon. These docs are an early preview, in the beginning stages: endpoints, fields and examples are samples and will change before launch. Brands on Kwik today can ask their account manager about integrations.
Read every docs page as one Markdown file, ready to paste into ChatGPT, Claude or any AI assistant.
Storefront integration · Guide 2 of 4Show The Referring Rep's Name And Photo.
Greet shoppers who came in on a Kwik link with "You're shopping with Ava", using the rep's name and photo.
Before you start
- Share-link tracking from the previous guide, so the root loader returns
waveLink. - The share-link lookup address for your store, from your Kwik contact. Save it as
KWIK_SHARELINK_LOOKUP_URL. - One valid share code and one made-up code to test with.
Two separate clocks
| What | Where it lives | How long | What it controls |
|---|---|---|---|
| The rep's profile | localStorage key waveData | 24 hours | Only the banner on the storefront |
| The share code | Cookie _kwik_waveLink and the cart attribute | 30 days | Who gets credit for the order |
An old or missing profile only changes what the banner shows. It never changes attribution. If the lookup is down, the store and checkout keep working.
The share-link lookup
The lookup takes a share code and returns the rep's public profile. The Hydrogen setup expects three fields:
{
"key": "ABC123",
"name": "Ava Martinez",
"photo_url": "https://cdn.example.com/reps/ava.jpg"
}
| Field | What it is |
|---|---|
key | The share code this profile belongs to. Only use the profile if it matches the code you asked for, exactly. |
name | The name to show. |
photo_url | The rep's photo. It can be missing or empty. |
Needs confirmationThe lookup address is not published here. Get it from your Kwik contact, and confirm the field names above with a real code from your brand before you ship. What an unknown code returns (an error status or an empty profile) also needs to be confirmed, so the sample treats both as "no rep".
1. Proxy the lookup through your store
Call the lookup from a small server route. The address stays in your environment, the browser only talks to your own domain, and you can cache good answers for a few minutes.
// app/routes/api.kwik-rep.$code.tsx: a small server route, so the lookup address
// stays in your environment and the browser only ever talks to your own domain.
import {data, type LoaderFunctionArgs} from 'react-router';
const VALID = /^[A-Za-z0-9]{1,64}$/;
export async function loader({params, context}: LoaderFunctionArgs) {
const code = params.code ?? '';
if (!VALID.test(code)) return data({rep: null}, {status: 400});
// KWIK_SHARELINK_LOOKUP_URL comes from Kwik. Example shape: https://<host>/<path>/shareLink
const base = context.env.KWIK_SHARELINK_LOOKUP_URL;
try {
const res = await fetch(`${base}/${encodeURIComponent(code)}`, {signal: AbortSignal.timeout(4000)});
if (!res.ok) return data({rep: null}, {status: 404});
const body = await res.json();
// Only accept a profile for the exact code we asked for (case-sensitive).
if (body?.key !== code || !body?.name) return data({rep: null}, {status: 404});
return data(
{rep: {key: body.key, name: String(body.name), photo_url: body.photo_url || null}},
{headers: {'Cache-Control': 'public, max-age=300'}},
);
} catch {
return data({rep: null}, {status: 502});
}
}
2. The banner component
- Matches the code: a cached or returned profile is used only when its
keyequals the currentwaveLink. - Caches for 24 hours in
localStorageaswaveData, then looks the rep up again. - Replaces the rep when the shopper follows another rep's link, because the code no longer matches.
- Missing photo: shows the rep's initials instead, and falls back to initials if the image fails to load.
- Invalid link: clears
waveDataand shows nothing, so a shopper never sees the wrong rep.
// app/components/ShoppingWith.tsx
// Shows "You're shopping with Ava" for shoppers who arrived on a Kwik link.
// The 24-hour cache only controls this banner. Attribution lives in the 30-day cookie and the cart.
import {useEffect, useState} from 'react';
type Rep = {key: string; name: string; photo_url: string | null};
const KEY = 'waveData';
const DAY_MS = 24 * 60 * 60 * 1000;
function readCache(code: string): Rep | null {
try {
const saved = JSON.parse(localStorage.getItem(KEY) ?? 'null');
if (!saved || saved.key !== code) return null; // another rep's link: replace it
if (Date.now() - saved.savedAt > DAY_MS) return null; // older than 24 hours: refresh it
return saved.rep;
} catch {
return null;
}
}
export function ShoppingWith({waveLink}: {waveLink: string | null}) {
const [rep, setRep] = useState<Rep | null>(null);
const [photoFailed, setPhotoFailed] = useState(false);
useEffect(() => {
if (!waveLink) return setRep(null);
const cached = readCache(waveLink);
if (cached) return setRep(cached);
fetch(`/api/kwik-rep/${encodeURIComponent(waveLink)}`)
.then((r) => (r.ok ? r.json() : {rep: null}))
.then(({rep}) => {
if (rep && rep.key === waveLink) {
localStorage.setItem(KEY, JSON.stringify({key: waveLink, rep, savedAt: Date.now()}));
setRep(rep);
} else {
localStorage.removeItem(KEY); // invalid link: never keep showing the last rep
setRep(null);
}
})
.catch(() => setRep(null)); // lookup down: hide the banner, never block the store
}, [waveLink]);
if (!rep) return null;
const initials = rep.name.split(/\s+/).map((w) => w[0]).join('').slice(0, 2).toUpperCase();
return (
<aside className="shopping-with" aria-label="Your rep">
{rep.photo_url && !photoFailed ? (
<img src={rep.photo_url} alt="" width={40} height={40} onError={() => setPhotoFailed(true)} />
) : (
<span className="shopping-with__initials" aria-hidden="true">{initials}</span>
)}
<p>You're shopping with <strong>{rep.name}</strong></p>
</aside>
);
}
Render it in your layout with the code from the root loader: <ShoppingWith waveLink={rootData.waveLink} />.
Any other website or framework: the logic is the same in plain JavaScript or any framework. Read the code with KwikAttribution.code() from the share-link snippet, call your own server route for the profile, and apply the same rules: match the code, cache for 24 hours, replace on a new link, and hide on any error.
Shopify themes: what changes
- The same component logic works as a plain script. Read the code from the
_kwik_waveLinkcookie that the Referral Attribution app embed sets. - A theme has no server route of its own. Use a Shopify app proxy, or ask your Kwik contact whether the lookup can be called from the browser on your domain.
End-to-end test
- Open
/?waveLink=ABC123in a private window. The banner shows the rep's name and photo. - In dev tools, check
localStorage.waveData: itskeyisABC123. - Set
savedAtinwaveDatato 25 hours ago and reload. The banner looks the rep up again. - Open
/?waveLink=XYZ789(a second valid rep). The banner switches, andwaveDatais replaced. - Open
/?waveLink=NOTREAL1. No banner,waveDatais gone, and add to cart and checkout still work. - Block the photo URL in dev tools. The banner shows initials.
- Finish a test order and check attribution on the order, not the banner. The banner proves the display. The order proves the credit.