Web: HTTP API
HTTP API for fetching ads and handling interactive actions in web apps.
| Integration | Rendering | Requests and views |
|---|---|---|
| Web SDK | requestAd() | SDK |
| API + renderer | mountSidekickAd() | Application code |
| API + components | Application components | Application code |
Authentication
Exactly one identity required: visitor_id or stable_user_id. Characters: A-Z, a-z, 0-9, _, ., :, -. API key: server-side only, no Origin.
| Caller | Authentication | Identity |
|---|---|---|
| Browser | Platform ID and an allowed site address. The browser supplies Origin. | visitor_id |
| Server | Authorization: Bearer sk_live_… | stable_user_id or visitor_id |
Request an ad
Content-Type: application/json; body ≤64 KiB.
POST /api/v1/web/ads
| Field | Type | Description |
|---|---|---|
platform_idrequired | string | Public platform ID. |
visitor_id | string | Visitor ID, 16-128 characters. Required for browser requests. |
stable_user_id | string | User ID, 1-128 characters. Available for server requests instead of visitor_id. |
request_id | string | Request ID, 1-128 characters. Reusing it does not prevent another ad issuance. |
placement | string | Registered placement name. Defaults to chat. |
language_code | string | Ad language, 1-32 characters. Defaults to en. |
accept_formats | string[] | text, image, response, card, consent, quiz. Defaults to all formats. |
context | object | Ad context. Fields depend on context.type. |
test | boolean | Test ads without billing. Defaults to false. A Test platform always returns demo ads. |
test_format | string | Test ad format when test is true. |
Required fields.
Server request
POST /api/v1/web/adsAuthorization: Bearer sk_live_EXAMPLEContent-Type: application/json{"platform_id": "plt_example","stable_user_id": "customer_42","context": {"question": "Headphones?","answer": "Here are some options…"},"test": true,"test_format": "card"}
Browser request
const response = await fetch("https://sidekick-ads.com/api/v1/web/ads", {method: "POST",credentials: "omit",headers: { "Content-Type": "application/json" },body: JSON.stringify({platform_id: "plt_example",visitor_id: visitorId,request_id: "chat-42-message-7",test: true,test_format: "card"})});if (!response.ok) throw new Error("Ad request failed");const payload = await response.json();
Context
context: optional object. Text fields: plain text, no HTML. No automatic page extraction.
| Type and use | Fields | Limits |
|---|---|---|
conversationAssistant conversation | question: user question; answer: app answer. | Both optional: 0-4,096 and 0-16,384 characters. Default when type is omitted. |
articleArticle | title: title; content: relevant excerpt. | Both required and nonempty: 1-4,096 and 1-16,384 characters. |
feedFeed item | text | Required nonempty text: 1-16,384 characters. |
staticSlot without text | - | Only type. Do not add question, answer or other text fields. |
// Chatconst context = {type: "conversation",question: "Which headphones suit travel?",answer: "Look for noise cancellation and a compact case."};// Article// context: { type: "article", title: "Travel audio", content: "A relevant excerpt…" }// Feed// context: { type: "feed", text: "Text of the current feed item…" }// No surrounding text// context: { type: "static" }
Response
| Field | Type | Description |
|---|---|---|
has_ad | boolean | Whether the response includes an ad. |
ad | object | null | Ad object; null when no ad is available. |
ad.delivery_id | string | Delivery ID for interactions. |
ad.expires_at | string | Ad expiry in ISO 8601 format. |
ad.format | string | Ad format. |
ad.disclosure | string | Required advertising disclosure. |
ad.test | boolean | Whether this is a test ad. |
ad.blocks | object[] | text, media, gallery and buttons blocks in display order. |
ad.view | string? | Token for confirming a live ad view. Absent on test ads. Views |
{"has_ad": false,"ad": null}
Ad blocks
| type | Fields | Rendering |
|---|---|---|
text | spans, role? | Text composed of consecutive spans. role: body, brand or footer. |
media | url, mime, alt?, role? | Image or video. role=logo: logo. Appearance settings control cropping. |
gallery | items | Array of media blocks. All items remain accessible. |
buttons | buttons | All buttons and complete labels; long labels wrap. |
Text span: spans[]
| Field | Type | Value |
|---|---|---|
text | string | Required. Plain text; line breaks preserved. No HTML. |
| bold / italic / underline / strikethrough / code | boolean? | Span style |
url | string? | HTTP(S) |
Button: buttons[]
| Field | Type | Value |
|---|---|---|
| id / text | string | Required |
style | string? | primary / secondary |
| url / action | string | Exactly one. url: navigation; action: original token for /interact. |
Files and links
Media: HTTPS; image/jpeg, image/png, image/webp, image/gif, video/mp4. Video: muted, playsInline. Links: HTTP(S), no prefetch.
{"has_ad": true,"ad": {"delivery_id": "imp_example","expires_at": "2099-01-01T00:00:00.000Z","format": "text","disclosure": "Sponsored","test": true,"blocks": [{"type": "text","role": "body","spans": [{"text": "Try a service for your next trip.","bold": true}]},{"type": "buttons","buttons": [{"id": "primary","text": "Learn more","style": "primary","url": "https://example.com/offer"}]},{"type": "text","role": "footer","spans": [{"text": "Sponsored · Sidekick"}]}]}}
Ready-made card for the API
/my-ads/*: application endpoints. The server must add platform_id, authorization and the original user identity.
<div id="ad-slot"></div><script src="https://sidekick-ads.com/sdk/sidekick-web.js"></script>
// Load /sdk/sidekick-web.js first. These /my-ads/* routes belong to YOUR app.// Your backend adds the publisher key, platform_id and authenticated user ID.function mountServerAd(slot, request) {let destroyed = false, active = true, visible = false, sent = false;let ad = null, timer;const abort = new AbortController();const report = error => console.warn("Ad unavailable", error.message);const post = async (path, body) => {const response = await fetch(path, {method: "POST", credentials: "same-origin", signal: abort.signal,headers: { "Content-Type": "application/json" },body: JSON.stringify(body)});if (!response.ok) throw new Error("Ad request failed: " + response.status);return response.json();};const view = Sidekick.mountSidekickAd(slot, {appearance: { maxWidth: 360, mediaHeight: 200, mediaFit: "cover", mediaPosition: "top", mediaRadius: 12 },onError: report,onAction: async (current, action) => {const result = await post("/my-ads/interact", {delivery_id: current.delivery_id, action});if (result.ok !== true) throw new Error("Interaction failed");return result.ad;}});function checkView() {const eligible = !destroyed && active && visible && !document.hidden &&ad && !ad.test && ad.view && Date.parse(ad.expires_at) > Date.now();if (!eligible || sent) { clearTimeout(timer); timer = undefined; return; }if (timer === undefined) timer = setTimeout(() => {timer = undefined;if (Date.parse(ad.expires_at) <= Date.now()) return;sent = true; observer.disconnect();void post("/my-ads/seen", { delivery_id: ad.delivery_id, view: ad.view }).catch(report);}, 1000);}const observer = new IntersectionObserver(entries => {visible = entries.some(e => e.isIntersecting && e.intersectionRatio >= 0.5);checkView();}, { threshold: [0, 0.5, 1] });document.addEventListener("visibilitychange", checkView);const controls = {setScreenActive(value) { active = value; view.setScreenActive(value); checkView(); },destroy() {destroyed = true; clearTimeout(timer); observer.disconnect(); abort.abort();document.removeEventListener("visibilitychange", checkView); view.destroy();}};// Call once per ad opportunity. Store the returned controls on your component.// This example deliberately has no automatic retries or SPA request cache.void post("/my-ads/ads", request).then(payload => {if (destroyed) return;ad = payload.has_ad ? Sidekick.parseAd(payload.ad) : null;view.setAd(ad);if (ad && !ad.test && ad.view) observer.observe(view.element);}).catch(error => { if (!destroyed) report(error); });return controls;}const card = mountServerAd(document.querySelector("#ad-slot"), {request_id: "chat-42-message-7",context: { type: "static" },test: true, test_format: "card"});// When covering the screen: card.setScreenActive(false).// When removing the component: card.destroy().
| Option or method | Purpose |
|---|---|
appearance / setAppearance(options) | Shared card appearance settings. Updating them does not fetch ads. |
setAd(ad | null) | ad object: render; null: clear the card. |
onAction(ad, action) | Promise of the next ad or null. Errors preserve the last scene; action buttons are disabled without a handler. |
onOpenURL(url) | Optional navigation handler. The default opens a new tab. |
onError(error) | Data, media and action errors. Invalid data or media failures clear the card. |
screenActive / setScreenActive(boolean) | Controls media playback; defaults to true. The wrapper above also pauses view timing. |
element | The card wrapper to observe for visibility. |
destroy() | Removes the card and releases renderer resources. |
Interactions
Requires delivery_id, action and the original user identity. Response: {ok: true, ad}. ad replaces the current scene.
POST /api/v1/web/interact
{"platform_id": "plt_example","stable_user_id": "customer_42","delivery_id": "imp_example","action": "signed-action-from-button"}
View confirmation
| Parameter | Value |
|---|---|
| Visibility | ≥50% for ≥1 continuous second; active tab and screen. |
requestAd() | Automatic |
fetchAd() | trackView(ad) |
| API / mountSidekickAd() | POST /api/v1/web/seen |
| Authorization / identity | Same platform_id and visitor_id or stable_user_id as /ads. |
view | TTL: 1 hour. Repeated confirmation does not duplicate the impression. |
| Response | {ok: true} |
POST /api/v1/web/seen
{"platform_id": "plt_example","stable_user_id": "customer_42","delivery_id": "imp_example","view": "view-receipt-from-ad"}
Errors
| Status | Meaning |
|---|---|
400 | Invalid request fields or user identity. |
401 | Missing or invalid API key. |
403 | Platform disabled, site address not allowed or API key belongs to another publisher. |
409 | View confirmation sent sooner than one second after issuance. |
410 | Action or view receipt is invalid or has expired. |
413 | Request body exceeds 64 KiB. |
429 | Request rate limit exceeded. |
503 | Service temporarily unavailable. |
{ "error": "Unknown placement" }