This is the long version of the Quickstart. If you just want the 5-minute happy path, start there. This guide covers everything around the edges: the full lifecycle from first line of code to recurring payouts.Documentation Index
Fetch the complete documentation index at: https://docs.trygravity.ai/llms.txt
Use this file to discover all available pages before exploring further.
Lifecycle overview
Design
Decide where ads go in your UI, which placements you need, and whether you’re using the SDK or calling the API directly.
Integrate
Install the SDK. Wire
gravityContext() on the client, gravity.getAds() on the server, GravityAd (or your own renderer) in the UI.Test
Keep
production: false. Verify placements render, impressions fire on visibility, clicks route through clickUrl.Install the pixel
Drop the Gravity pixel onto your site. One
<script> tag — mints a visitor ID the SDK automatically forwards on every ad request, which tightens targeting and attribution. If you also open ad links inside a native in-app browser, add the in-app browser install on top.Go live
Flip
production: true. Watch the going-live checklist.Optimize
Let experiments test ad variants against your real traffic. The engine moves traffic toward the highest-converting options automatically.
Get paid
Monitor payouts in the dashboard. Revenue accrues per impression.
Pick placements
A placement is where an ad sits relative to your UI. Every ad request declares one or more. Common options:| Placement | When to use |
|---|---|
below_response | Under the assistant’s message. Most common in chat UIs. |
above_response | Pinned header above the response. Use sparingly. |
inline_response | Inside the response stream (markdown). |
left_response / right_response | Sidebar next to the response. |
search_result | Within a search result list. |
top_page / bottom_page | Static, page-level placement. |
placement_id you pick — anything identifying the slot ("main", "mobile-footer", etc.). Keep them stable across requests; you’ll see per-slot metrics in the dashboard.
You can request up to 10 placements per call and the engine returns an ad per slot (or null per slot with no fill).
Choose: SDK or API
Both hit the same engine. The SDK is a thin wrapper that handles context preparation, impression tracking, retry, and experiments. The API is what the SDK calls under the hood. Use the SDK when:- You’re on Node, TypeScript, Python, or React
- You want pre-built visual variants
- You want to participate in experiments (which auto-optimize your creative mix)
- You want impression tracking to “just work”
- You’re on an unusual language / runtime
- You render ads in a heavily custom way that can’t use
GravityAd - You’re prototyping or building a CLI / non-UI surface
Typical integration shapes
Next.js / App Router
Express
FastAPI / Python
Error handling
The SDK never throws. Failure modes:| Situation | Result |
|---|---|
| Network error / timeout | { ads: [] } (JS) / AdResult(ads=[]) (Python) |
| 204 No Content (no match) | { ads: [] } |
| Invalid API key | { ads: [] } + warn in logs |
| Downstream engine error | { ads: [] } + warn |
Going live checklist
See Going live. Short version: flipproduction: true, verify impressions land, confirm clicks route through clickUrl, watch the dashboard.
What’s next
Request ads
Server-side patterns in depth.
Show ads
Client rendering, variants, custom renderers.
Pixel
Drop
gr-pix.js on your site — SDK auto-picks up the visitor ID for better attribution. Optional WebView install for native apps.Experiments
How the engine auto-optimizes creative mix for your traffic.