Installation
GRAVITY_API_KEY in your server environment.
How the pieces fit together
1
Initialize the pixel
gravityPixel() handles session and visitor tracking automatically. Initialize it once when your app starts.2
Prepare context
gravityContext() captures device signals and session info. Pass the pixel’s visitor and session IDs to link tracking with ad requests.3
Fetch ads
gravity.getAds(req, messages, placements) reads the context, adds the client IP, and returns matched ads. Runs in parallel with your LLM call.Initialize the pixel
YOUR_PIXEL_ID in your dashboard under Settings → Platform Settings.
The pixel automatically manages sessions, visitor IDs, and attribution — same as the browser pixel (gr-pix.js), but using local file storage instead of cookies. Session state is persisted and cleaned up automatically on process exit.
Prepare context
gravityContext() auto-detects terminal device signals (OS, timezone, locale). Required parameters:
Fetch ads
Full example
FAQ
Do I need a separate package?
Do I need a separate package?
No.
gravityPixel ships inside @gravity-ai/api. Just npm install @gravity-ai/api and import it.Does this work in Bun and Deno?
Does this work in Bun and Deno?
Yes. It uses standard APIs and falls back gracefully when Node-specific modules aren’t available.
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. 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. The Gravity class reads it automatically. You can also pass it via new Gravity({ apiKey: '...' }).
