ShopGPT SDK for shoppable TV ads
Add a buy button to Connected TV ads with the shopgpt npm package, and update the TV live when a viewer adds to cart.
What it does
ShopGPT is RankUp's AI shopping experience. A shopper opens a product in ShopGPT, asks questions about it in a chat, and adds it to their cart.
The ShopGPT SDK brings that to Connected TV. When a shoppable ad plays, your TV app shows a buy button. The viewer presses it with the remote, the product is added to their cart, and the TV updates live to show the sale.
The SDK is the shopgpt package on npm. It runs in any HTML5 TV app, including Samsung Tizen, LG webOS, Fire TV and Android TV. It needs no native code.
How it works
- The ad starts. Your player reads three values from the VAST ad and passes them to the SDK.
- The SDK creates a session. RankUp looks up the product from its ID and returns its title, image and price.
- Your app shows the button. Use the built-in button or draw your own.
- The viewer presses it. The SDK marks the product as added to cart.
- The TV updates. The SDK polls the session and tells your app when it converts.
Each session also carries a link that opens the same product in ShopGPT on a phone, so a viewer can continue there.
Before you start
- A TV app or web player that runs HTML5 and JavaScript.
- Ads that carry a RankUp product ID. RankUp provides the ID for each product you advertise.
- React 18 or later, only if you use the React components.
Install
npm install shopgpt
The package ships as ES modules with TypeScript types. React is an optional peer dependency.
Quickstart
import { ShopGPTCTV } from "shopgpt";
const ctv = new ShopGPTCTV({ apiBase: "https://app.rankup.diy" });
// Listen for conversions
ctv.onEvent((event) => {
if (event.type === "added_to_cart") showBadge(event.session);
});
// When the ad starts, pass the three VAST values as they are
const { session } = await ctv.createSession({ shoppable, rankupid, cta });
if (!session) return; // not a shoppable ad: play it as a normal ad
// Keep the TV in sync with the session
ctv.pollSession(session.id);
// When the viewer presses the button
await ctv.addToCart(session.id);
The ad parameters
A shoppable ad carries three values. Pass them to createSession unchanged.
| Parameter | Type | Description |
|---|---|---|
shoppable | boolean | Set to false for a normal ad. No session is created |
rankupid | number or string | The RankUp product ID. Required |
cta | string | The button label for this ad, for example "Scan to buy" |
You never send product details. RankUp resolves the title, image and price from rankupid.
Use it with React
Import the React bindings from shopgpt/react.
Drop-in player
ShopGPTCTVPlayer plays the video, creates the session and shows the button.
import { ShopGPTCTVPlayer } from "shopgpt/react";
<ShopGPTCTVPlayer
videoSrc="https://example.com/ad.mp4"
ad={{ shoppable: true, rankupid: 10403886137619, cta: "Scan to buy" }}
cta="send_to_phone"
onConverted={(session) => console.log("sold", session.cartCount)}
/>;
| Prop | Description |
|---|---|
videoSrc | URL of the ad video |
ad | The three ad parameters |
apiBase | RankUp API base URL |
cta | Button preset: add_to_cart (default) or send_to_phone |
ctaLabel | Custom button text. Overrides the preset and the ad's cta |
onConverted | Called with the session when the viewer adds to cart |
Your own player
Use the hook with your existing player, and place the button where you want it.
import { ShopGPTAddToCartButton, useShopGPTShoppableAd } from "shopgpt/react";
function AdOverlay({ ad }) {
const { session, loading } = useShopGPTShoppableAd(ad, {
apiBase: "https://app.rankup.diy",
});
if (loading || !session) return null;
return (
<div className="overlay">
<p>{session.product.title}</p>
<ShopGPTAddToCartButton session={session} label="Buy Now" />
</div>
);
}
| Hook | What it does |
|---|---|
useShopGPTShoppableAd(ad, options) | Creates the session when the ad starts and keeps it up to date. Returns session, loading and create |
useShopGPTSession(sessionId, apiBase) | Follows an existing session. Returns session and error |
Pass autoCreate: false in the options to create the session yourself by calling create().
Customize the button
ShopGPTAddToCartButton has two presets and accepts any custom text or styling.
// Preset labels
<ShopGPTAddToCartButton session={session} cta="add_to_cart" />
<ShopGPTAddToCartButton session={session} cta="send_to_phone" />
// Custom text, styling and a callback
<ShopGPTAddToCartButton
session={session}
label="Buy Now"
className="my-cta"
onCta={(session) => analytics.track("ctv_cta", { sessionId: session.id })}
/>
| Prop | Description |
|---|---|
session | The session from the hook. The button renders nothing without one |
cta | add_to_cart shows "Add to Cart". send_to_phone shows "Send to Phone" |
label | Custom text. Overrides the preset |
qty | Quantity to add. Defaults to 1 |
className | Replaces the default styling |
onAdded | Called when the product is added |
onCta | Called after a successful press |
After a successful press, the button is replaced by an "Added to cart" confirmation.
Client reference
Create one ShopGPTCTV client and reuse it.
const ctv = new ShopGPTCTV({
apiBase: "https://app.rankup.diy",
pollIntervalMs: 2500,
});
| Option | Default | Description |
|---|---|---|
apiBase | The page's own origin | RankUp API base URL. Set this in a TV app |
pollIntervalMs | 2500 | How often pollSession checks the session |
fetchImpl | Global fetch | A custom fetch, for platforms without one |
| Method | Description |
|---|---|
createSession(ad) | Creates a session from the ad parameters. Returns { session, mobileUrl }. session is null for a non-shoppable ad |
getSession(id) | Fetches a session once |
pollSession(id, onUpdate) | Checks the session on an interval and emits events when its status changes. Returns a function that stops polling |
stopPolling() | Stops the current poll |
addToCart(id, { qty }) | Adds the product to the cart and marks the session converted |
markScanned(id) | Marks the session as opened on a phone |
onEvent(listener) | Subscribes to events. Returns a function that unsubscribes |
Events
| Event | When |
|---|---|
session_created | A session was created |
session_updated | The session's status changed |
scanned | The viewer opened the product on a phone |
added_to_cart | The viewer added the product to their cart |
Every event carries the current session.
The session object
{
"id": "session-id",
"status": "created",
"product": {
"id": "product-id",
"title": "Product title",
"brand": "Brand",
"price": "999 INR",
"imageUrl": "https://example.com/image.jpg"
},
"cta": "Scan to buy",
"mobileUrl": "https://shopgpt.rankup.diy/?product_id=...&ctv=...",
"cartCount": 0,
"scanCount": 0,
"createdAt": "2026-01-01T00:00:00.000Z",
"updatedAt": "2026-01-01T00:00:00.000Z"
}
A session moves through these statuses:
| Status | Meaning |
|---|---|
created | The ad is playing and the button is showing |
scanned, viewing | The viewer opened the product on a phone |
added_to_cart | The viewer added the product to their cart |
completed | The purchase flow finished |
expired | The session ran out of time |
A session lasts 10 minutes, which covers one ad spot. Create a new session each time an ad starts.
Continue on a phone
session.mobileUrl opens the product in ShopGPT, where the viewer can chat about it and add it to their cart. To offer this, show the link as a QR code on screen.
When the viewer opens the link or adds to cart on their phone, ShopGPT reports it back to the session. Your TV app receives the scanned and added_to_cart events through pollSession, the same way as a remote press.
REST API
The SDK is a thin wrapper over these endpoints. Call them directly if you cannot use the package. They accept cross-origin requests.
| Method | Path | Purpose |
|---|---|---|
POST | /api/ctv/session | Create a session from { shoppable, rankupid, cta } |
GET | /api/ctv/session/{id} | Read a session |
PATCH | /api/ctv/session/{id} | Set status to scanned, viewing or completed |
POST | /api/ctv/cart | Add to cart with { sessionId, qty } |
GET | /api/ctv/cart?sessionId={id} | List the cart items for a session |
Troubleshooting
| Problem | What to do |
|---|---|
createSession failed: 404 | No product matches that rankupid. Check the ID in the ad |
createSession failed: 400 | The ad did not include a rankupid |
session is null | The ad has shoppable: false. Play it as a normal ad |
getSession failed: 404 | The session expired or the ID is wrong. Create a new session |
| The TV does not update after a press | Call pollSession(session.id), or use useShopGPTShoppableAd, which polls for you |
| Requests go to the wrong host | Set apiBase when you create the client. Without it, the SDK uses the page's own origin |
Related
Last updated