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
Initialize the pixel
gravityPixel() handles session and visitor tracking automatically. Initialize it once when your app starts.Prepare context
gravityContext() captures device signals and session info. Pass the pixel’s visitor and session IDs to link tracking with ad requests.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.
| Parameter | Type | Default | Description |
|---|---|---|---|
pixelId | string | — | Required. Your Gravity pixel ID. |
appName | string | 'terminal' | Application label, used as page title in events. |
eventEndpoint | string | 'https://api.trygravity.ai/track/gr-events' | Event endpoint URL. |
sessionEndpoint | string | 'https://api.trygravity.ai/track/gr-session' | Session endpoint URL. |
sessionTimeoutMinutes | number | 30 | Session idle timeout in minutes. |
attrMaxAgeDays | number | 180 | Max age for persisted attribution in days. |
sessionMaxPages | number | 50 | Max entries in invocation history. |
hashIdentityFields | boolean | true | Hash identity fields with SHA-256 before sending. |
storageDir | string | '~/.gravity' | Custom storage directory for session and attribution data. |
Prepare context
gravityContext() auto-detects terminal device signals (OS, timezone, locale). Required parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
sessionId | string | Yes | From pixel.getSessionId() |
user.userId | string | Yes | Stable per-user identifier |
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: '...' }).