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.
Installation
GRAVITY_API_KEY in your server environment.
How the pieces fit together
Client — prepare context
gravityContext() runs in your chat component. It captures sessionId, userId, and device signals (screen, timezone, locale) and returns a plain object your server can read.Server — fetch ads
gravity.getAds(req, messages, placements) reads the context from the request body, adds the client IP, and POSTs to https://server.trygravity.ai/api/v1/ad. Runs in parallel with your LLM call.Gravity — match and return
The engine matches the conversation against active campaigns, runs an auction, and returns an ad object per requested placement.
Client — prepare context
In your chat component, prepare user and device signals:gravityContext() auto-detects device signals (screen size, timezone, language) on the client. Required parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | Unique session identifier |
user.userId | string | Yes | Stable per-user identifier. Thrown at runtime if missing — pass "anonymous" (or any constant) yourself if you genuinely don’t have one. Gets normalized to id on the wire. |
Server — fetch ads
Gravity constructor accepts:
| Parameter | Type | Default | Description |
|---|---|---|---|
apiKey | string | GRAVITY_API_KEY env var | Your Gravity API key |
gravityApi | string | Production URL | Gravity ad endpoint URL |
timeoutMs | number | 3000 | Abort after this many ms |
relevancy | number | 0.2 | Minimum relevancy threshold, 0.0–1.0. Lower = more ads with weaker contextual matches |
production | boolean | false | false returns test ads (no billing) |
excludedTopics | string[] | undefined | Topics to exclude from ad matching |
React — render ads
GravityAd component handles impression tracking automatically via IntersectionObserver.
| Prop | Type | Description |
|---|---|---|
ad | Ad | The ad object from gravity.getAds() |
variant | "card" | "inline" | "minimal" | "bubble" | "contextual" | "native" | "footnote" | "quote" | "suggestion" | "accent" | "side-panel" | "labeled" | "spotlight" | "embed" | "split-action" | "pill" | "banner" | "divider" | "toolbar" | "tooltip" | "notification" | "hyperlink" | "text-link" | Rendering style. When an experiment is active and variant is omitted, the component uses the engine-assigned renderer. |
Impression tracking
If you render ads with a custom component instead ofGravityAd, fire the impression pixel when the ad becomes visible:
clickUrl link.
What’s next
Experiments
Run A/B tests on ad creative and rendering. The engine assigns arms; the SDK renders them.
FAQ
Does the ad request block my LLM response?
Does the ad request block my LLM response?
No. Fire
gravity.getAds() in parallel with your LLM call using Promise.allSettled(). It never throws, so failures are silent.Where does the API key go?
Where does the API key go?
Set
GRAVITY_API_KEY as an environment variable on your server. The Gravity class reads it automatically. You can also pass it via new Gravity({ apiKey: '...' }).Do I need @gravity-ai/react?
Do I need @gravity-ai/react?
Only if you want pre-built React components. For custom rendering, just use the ad objects from
@gravity-ai/api directly.