Skip to main content

What you’re going to do

  1. Create an AI platform account and grab your API key.
  2. Install the SDK.
  3. Fire ad requests from your server in parallel with your LLM call.
  4. Render the ad in your chat UI.
  5. Drop the web pixel on your site.
  6. Flip to production when it looks right.
Copy the commands, change a couple of IDs, ship it.
Throughout this guide we use the SDK (@gravity-ai/api + @gravity-ai/react). If you’d rather hit the HTTP API directly, see the API Reference. Same engine, you just own the rendering and tracking.

1. Create your account

  1. Sign up at app.trygravity.ai/publisher/signup.
  2. Create an AI platform account for your app.
  3. From your dashboard, copy the API key and the Pixel ID.
Save the API key as GRAVITY_API_KEY in your server environment.

2. Install the SDK

3. Prepare context on the client

Call gravityContext() in your chat component so the server gets the session ID and device signals (the end user’s User-Agent, IP, etc.) alongside the messages.
Don’t skip this — and forward gravity_context all the way to the ad request. The device object’s ua and ip are required — they power fraud/bot detection plus the Device and Geography breakdowns in your dashboard, and requests missing them are rejected with HTTP 400. If your backend calls Gravity without forwarding the client’s device, the ad request carries your server’s UA/IP instead of the end user’s. The SDK’s getAds(req, …) reads gravity_context from the request body for you — just make sure your client actually posts it. See Device signals.

4. Fetch ads on the server

Fire the ad request in parallel with your LLM call. It never throws — failures return an empty array.

5. Render the ad

GravityAd automatically handles the placement design, impression tracking, click attribution, disclosure, and fallback.

6. Install the pixel (required)

The Gravity pixel is required for attribution and payouts. Without it, Gravity can’t measure visitor journeys or tie ad performance back to real outcomes. Drop this on every page:
See the Pixel page for verification steps and the in-app browser install if your native app opens ad links in a WebView.

7. Verify, then go live

By default, new Gravity() returns test ads — no billing, safe to iterate. You’ll see the same test creative every time. When you’re ready for real traffic:
That’s it. You’re now serving real ads and earning revenue.

Full integration guide

The long version: lifecycle, placements, error handling, framework-specific examples.

Going live checklist

Everything to verify before flipping production: true.

What you didn’t have to think about

  • Retries — the SDK handles transient failures silently.
  • Timeouts — 3 seconds by default. Configurable.
  • Impression / click tracking — fired by GravityAd. Attribution is automatic.
  • Experiments — Gravity can test designs without publisher code changes. See Experiments.
  • Ad quality — Gravity handles the creative; you never hand-pick advertisers.

Troubleshooting

Expected when no campaign matches the conversation — the engine returns 204 No Content and the SDK resolves to { ads: [] }. Gracefully hide the ad slot. Lower the relevancy threshold to fill more aggressively if you want looser matches.
You’re still on test mode. Pass production: true to the constructor or set production=True in Python.
If you’re using GravityAd, it fires on visibility via IntersectionObserver — make sure the ad is actually rendering in the viewport. If you render ads with a custom component, you have to fire the impression pixel yourself: new Image().src = ad.impUrl when the ad becomes visible.