> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trygravity.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ad serving API

> The public Gravity ad serving API: the endpoints, the hosts, authentication, and common errors.

Use the ad serving API to request ads and to send user feedback on ads. For the terms on this page, see [Core concepts](https://docs.trygravity.com/introduction/concepts.md). To manage an account with code, see the [Advertiser Dashboard API](https://docs.trygravity.com/api-reference/advertiser/overview.md) and the [Publisher Dashboard API](https://docs.trygravity.com/api-reference/publisher/overview.md). To send conversions from your backend, see the [Conversions API](https://docs.trygravity.com/advertisers/server-conversions.md) (`POST /gateway/events`).

## Endpoints

| Endpoint | Host | Purpose |
| - | - | - |
| `POST /api/v1/ad` | `server.trygravity.com` | Get ads that match the conversation. This is the main endpoint. |
| `POST /ad-feedback` | `platform.trygravity.com` | Send the reaction of a user to an ad (thumbs up or thumbs down). |
| `GET /health` | `server.trygravity.com` | Check that the ad server is available. |
| `POST /api/v1/index/session/messages` | `server.trygravity.com` | Send conversation messages to the Gravity Index. See [Conversation context](https://docs.trygravity.com/gravity-index/conversation-context.md). |

> [!NOTE]
>
> Direct HTTP integrations of `/api/v1/ad` require a signed agreement with Gravity. Send an email to [support@trygravity.com](mailto:support@trygravity.com) to request access. The HTTP API serves production ads unless the request sends `"testAd": true`. The SDKs request test ads by default.

The SDKs call `/api/v1/ad` for you. They prepare the request context, record impressions, and return an empty result when a request fails.

## Base URL

```
https://server.trygravity.com
```

> [!NOTE]
>
> `server.trygravity.com` and `server.trygravity.ai` point to the same load balancer and serve the same engine, so either host works. The SDKs and Gravity's own services use `https://server.trygravity.ai` by default. The examples on this page use `.com`.

## Authentication

Each endpoint needs your Gravity API key. Get the key from your [dashboard](https://app.trygravity.com) under **Settings → Platform Settings** (AI platforms) or **Settings → Organization** (advertisers).

> [!WARNING]
>
> Keep your API key on your server. Do not put it in client code or in a public repository.

For `POST /api/v1/ad`, send the key as a Bearer token:

```bash theme={null}
curl -X POST https://server.trygravity.com/api/v1/ad \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [{"role": "user", "content": "How do I set up a PostgreSQL database?"}],
    "sessionId": "sess_abc123",
    "placements": [{"placement": "below_response", "placement_id": "YOUR_PLACEMENT_ID"}],
    "testAd": true
  }'
```

For `POST /ad-feedback`, send the key in the `X-API-Key` header. See [Ad feedback](https://docs.trygravity.com/engine/ad-feedback.md).

## Common errors

| Status | Meaning | What to do |
| - | - | - |
| `204` | No ad. The request had no match, took more than 4 seconds, or failed on the server. The body is empty. | Hide the ad slot. |
| `401` | The API key is missing or not valid. | Check the `Authorization` header and the key. |
| `413` | The request body is larger than 1 MB. | Send fewer or shorter messages. |
| `422` | The request body does not match the schema. The `detail` field names each problem. | Fix the fields that `detail` names. |
| `429` | Too many requests. | Wait for the number of seconds in the `Retry-After` header, then try again. |

Each endpoint page lists its own errors.

## Health check

```bash theme={null}
curl https://server.trygravity.com/health
```

When the server is ready, the response is `200`:

```json theme={null}
{"status": "ok", "ready": true, "release_sha": "221d530..."}
```

While the server starts, the response is `503` with `{"status": "starting", "ready": false}`.

Use this endpoint for uptime checks. Rate limits do not apply to it.

## Next steps

> [card] [Contextual ads endpoint](https://docs.trygravity.com/engine/contextual-ads.md)
>
> The full request and response reference for `POST /api/v1/ad`.

> [card] [Ad feedback](https://docs.trygravity.com/engine/ad-feedback.md)
>
> Let users react to ads with a thumbs up or a thumbs down.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.