Skip to main content

Installation

Set 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

Find 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

gravity.getAds() never throws. On any failure, it returns { ads: [] } silently. Safe to fire-and-forget.

Full example

FAQ

No. gravityPixel ships inside @gravity-ai/api. Just npm install @gravity-ai/api and import it.
Yes. It uses standard APIs and falls back gracefully when Node-specific modules aren’t available.
No. Fire gravity.getAds() in parallel with your LLM call. It never throws, so failures are silent.
Set GRAVITY_API_KEY as an environment variable. The Gravity class reads it automatically. You can also pass it via new Gravity({ apiKey: '...' }).