Developer Documentation

Quickstart

Integrating with an AI coding agent?
Point Claude Code, Cursor, or Codex at https://adsrouter.ai/llms-full.txt — it contains every endpoint, auth header, JSON schema, Koah bridge snippet, and beacon rule in one self-contained file.

Go from zero to serving contextual ads with first-party viewability tracking in under five minutes.

1. Create a Property & Copy Your Secret

  1. Sign in at https://app.adsrouter.ai/signup (existing FreeRouter accounts work immediately via email OTP).
  2. Open Properties → New Property and enter your app name and domain or bundle ID.
  3. Copy your one-time property_secret (prefixed ar_sec_) and public property_key (prefixed ar_pub_). Keep ar_sec_ on your backend server.
  4. Optional: enable Test Mode on the property while developing so test impressions are isolated from production stats.

2. Request an Ad (Server-to-Server)

Call POST https://api.adsrouter.ai/v1/ad from your backend after (or in parallel with) your LLM response. Always forward the end user's IP and User-Agent so GeoIP, device targeting, and S2S networks (Gravity, Thrad) work accurately.

cURL

curl -X POST https://api.adsrouter.ai/v1/ad \
  -H "Authorization: Bearer $ADSROUTER_PROPERTY_SECRET" \
  -H "Content-Type: application/json" \
  -d '{
    "placement": "chat_below_answer",
    "messages": [
      { "role": "user", "content": "What is the best espresso machine under $500?" },
      { "role": "assistant", "content": "The Breville Bambino and Gaggia Classic are top picks..." }
    ],
    "user": {
      "pub_user_id": "user_42"
    },
    "device": {
      "ip": "203.0.113.10",
      "ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"
    }
  }'

Node.js (fetch)

const res = await fetch('https://api.adsrouter.ai/v1/ad', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.ADSROUTER_PROPERTY_SECRET}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    placement: 'chat_below_answer',
    messages,
    user: { pub_user_id: req.user?.id },
    device: {
      ip: req.headers['x-forwarded-for']?.split(',')[0]?.trim() || req.ip,
      ua: req.headers['user-agent'],
      accept_language: req.headers['accept-language'],
    },
  }),
});

const decision = await res.json();
// Return `decision` alongside your chat response to the frontend

Python (requests)

import os, requests

resp = requests.post(
    "https://api.adsrouter.ai/v1/ad",
    headers={
        "Authorization": f"Bearer {os.environ['ADSROUTER_PROPERTY_SECRET']}",
        "Content-Type": "application/json",
    },
    json={
        "placement": "chat_below_answer",
        "messages": messages,
        "user": {"pub_user_id": user_id},
        "device": {"ip": client_ip, "ua": client_ua},
    },
    timeout=4.0,
)
decision = resp.json()

3. Render & Fire Beacons in the Browser

Drop in our zero-dependency browser helper to handle both render_mode: "creative" (with automatic 50% viewport IntersectionObserver beacons) and render_mode: "client_sdk" (Koah bridge):

<div id="ad-slot"></div>
<script src="https://api.adsrouter.ai/sdk/adsrouter-client.js"></script>
<script>
  // Pass the JSON returned by your backend from POST /v1/ad
  window.AdsRouter.render(document.getElementById('ad-slot'), decision);
</script>

Building a custom React, Vue, or native mobile renderer? See Tracking, Caps & Identity for exact beacon and click rules.