@gravity-ai/api 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @gravity-ai/api
2
2
 
3
- The official Node.js/TypeScript SDK for the Gravity AI advertising API. Fetch contextually relevant ads based on conversation content.
3
+ The official Node.js/TypeScript SDK for the Gravity AI advertising API.
4
4
 
5
5
  ## Installation
6
6
 
@@ -8,304 +8,11 @@ The official Node.js/TypeScript SDK for the Gravity AI advertising API. Fetch co
8
8
  npm install @gravity-ai/api
9
9
  ```
10
10
 
11
- > **Note:** Requires Node.js 18+
11
+ ## Documentation
12
12
 
13
- ## Quick Start
13
+ For full documentation, examples, and API reference, visit:
14
14
 
15
- ```typescript
16
- import { Client } from '@gravity-ai/api';
17
-
18
- const client = new Client('your-api-key');
19
-
20
- const response = await client.getAd({
21
- messages: [
22
- { role: 'user', content: 'What are some good hiking trails?' },
23
- { role: 'assistant', content: 'Here are some popular trails...' }
24
- ],
25
- sessionId: 'session-123', // Recommended
26
- userId: 'user-456', // Recommended
27
- });
28
-
29
- if (response) {
30
- const ad = response.ads[0];
31
- console.log(ad.adText);
32
- }
33
- ```
34
-
35
- ## Migrating from v0
36
-
37
- If you're upgrading from a previous version, the `getAd()` response format has changed:
38
-
39
- ```typescript
40
- // Before (v0)
41
- const ad = await client.getAd({ messages });
42
- if (ad) {
43
- console.log(ad.adText);
44
- }
45
-
46
- // After (v1)
47
- const response = await client.getAd({ messages, sessionId: '...' });
48
- if (response) {
49
- const ad = response.ads[0];
50
- console.log(ad.adText);
51
- }
52
- ```
53
-
54
- The response is now an object with an `ads` array instead of a single ad object.
55
-
56
- ---
57
-
58
- ## Client Configuration
59
-
60
- ### Basic Initialization
61
-
62
- ```typescript
63
- import { Client } from '@gravity-ai/api';
64
-
65
- const client = new Client('your-api-key');
66
- ```
67
-
68
- ### Advanced Configuration
69
-
70
- ```typescript
71
- import { Client } from '@gravity-ai/api';
72
-
73
- const client = new Client('your-api-key', {
74
- // Topics to exclude globally (optional)
75
- excludedTopics: ['politics', 'religion'],
76
-
77
- // Minimum relevancy threshold 0-1 (optional)
78
- relevancy: 0.8,
79
- });
80
- ```
81
-
82
- #### Configuration Options
83
-
84
- | Option | Type | Default | Description |
85
- |--------|------|---------|-------------|
86
- | `endpoint` | `string` | `'https://server.trygravity.ai'` | API endpoint URL |
87
- | `excludedTopics` | `string[]` | `[]` | Topics to exclude from ad matching |
88
- | `relevancy` | `number \| null` | `null` | Minimum relevancy score (0-1) |
89
-
90
- ---
91
-
92
- ## `getAd()` — Fetch Contextual Ads
93
-
94
- Fetch ads based on conversation context. Requires `messages` array.
95
-
96
- ```typescript
97
- const response = await client.getAd({
98
- messages: [
99
- { role: 'user', content: 'I need help finding a new laptop.' },
100
- { role: 'assistant', content: 'What is your budget?' }
101
- ],
102
- sessionId: 'session-123', // Recommended
103
- userId: 'user-456', // Recommended
104
- numAds: 1, // 1-3, default 1
105
- });
106
-
107
- if (response) {
108
- const ad = response.ads[0];
109
- console.log(ad.adText);
110
- }
111
- ```
112
-
113
- ### Full Request with All Options
114
-
115
- ```typescript
116
- const response = await client.getAd({
117
- // Required: conversation messages
118
- messages: [
119
- { role: 'user', content: 'I need help finding a new laptop.' },
120
- { role: 'assistant', content: 'What is your budget?' }
121
- ],
122
-
123
- // Recommended: session/user tracking (improves ad relevance & revenue)
124
- sessionId: 'session-123',
125
- userId: 'user-456',
126
-
127
- // Optional: user information for targeting
128
- user: {
129
- uid: 'user-123', // Unique user identifier
130
- gender: 'male', // 'male' | 'female' | 'other'
131
- age: '25-34', // Age range string
132
- keywords: 'tech,gadgets', // User interest keywords
133
- },
134
-
135
- // Optional: device information
136
- device: {
137
- ip: '1.2.3.4', // User IP address
138
- country: 'US', // ISO country code
139
- ua: 'Mozilla/5.0...', // User agent string
140
- os: 'macOS', // Operating system
141
- ifa: 'device-ad-id', // Advertising identifier
142
- },
143
-
144
- // Optional: ad request settings
145
- excludedTopics: ['politics'], // Topics to exclude
146
- relevancy: 0.8, // Min relevancy threshold (0-1)
147
- numAds: 1, // Number of ads (1-3)
148
- testAd: false, // Return test ad when true
149
-
150
- // Optional: custom fields (open-ended, passed to matching)
151
- interests: ['coding', 'apple', 'software development'],
152
- summary: 'User wants a laptop for software development',
153
- });
154
- ```
155
-
156
- ---
157
-
158
- ## Request Parameters
159
-
160
- | Parameter | Type | Required | Description |
161
- |-----------|------|----------|-------------|
162
- | `messages` | `MessageObject[]` | Yes | Conversation history |
163
- | `sessionId` | `string` | Recommended | Session identifier for tracking |
164
- | `userId` | `string` | Recommended | User identifier for frequency capping |
165
- | `device` | `DeviceObject` | - | Device/location info |
166
- | `user` | `UserObject` | - | User targeting info |
167
- | `excludedTopics` | `string[]` | - | Topics to exclude |
168
- | `relevancy` | `number` | - | Min relevancy (0-1) |
169
- | `numAds` | `number` | - | Number of ads (1-3, default 1) |
170
- | `testAd` | `boolean` | - | Return test ad when true |
171
-
172
- ---
173
-
174
- ## Response Types
175
-
176
- ### AdResponse
177
-
178
- Returned by `getAd()`.
179
-
180
- ```typescript
181
- interface AdResponse {
182
- ads: Ad[]; // Array of ads
183
- numAds: number; // Number of ads returned
184
- totalPayout?: number; // Total payout across all ads
185
- }
186
-
187
- interface Ad {
188
- adText: string; // Ad copy text
189
- adId: string; // Unique ad identifier
190
- title?: string; // Ad title
191
- brandName?: string; // Brand name
192
- brandImage?: string; // Brand logo URL
193
- url?: string; // Landing page URL
194
- favicon?: string; // Favicon URL
195
- impUrl?: string; // Impression tracking URL
196
- clickUrl?: string; // Click-through URL
197
- payout?: number; // Payout amount
198
- }
199
- ```
200
-
201
- ---
202
-
203
- ## Common Types
204
-
205
- ### Message Object
206
-
207
- ```typescript
208
- interface MessageObject {
209
- role: 'user' | 'assistant';
210
- content: string;
211
- }
212
- ```
213
-
214
- ### User Object
215
-
216
- ```typescript
217
- interface UserObject {
218
- uid?: string; // Unique user ID
219
- gender?: 'male' | 'female' | 'other'; // User gender
220
- age?: string; // Age range (e.g., '25-34')
221
- keywords?: string; // Interest keywords
222
- }
223
- ```
224
-
225
- ### Device Object
226
-
227
- ```typescript
228
- interface DeviceObject {
229
- ip: string; // IP address
230
- country: string; // ISO country code
231
- ua: string; // User agent
232
- os?: string; // Operating system
233
- ifa?: string; // Advertising ID
234
- }
235
- ```
236
-
237
- ---
238
-
239
- ## Error Handling
240
-
241
- The client handles errors gracefully and returns `null` on failure. Errors are logged to the console.
242
-
243
- ```typescript
244
- const response = await client.getAd({ messages, sessionId: '...' });
245
-
246
- // Returns null on:
247
- // - Network errors
248
- // - API errors (4xx, 5xx)
249
- // - No relevant ad (204)
250
- // - Invalid response data
251
-
252
- if (!response) {
253
- // Handle gracefully - don't break the user experience
254
- }
255
- ```
256
-
257
- ## TypeScript
258
-
259
- Full TypeScript support with exported types:
260
-
261
- ```typescript
262
- import { Client, ClientParams } from '@gravity-ai/api';
263
- import type {
264
- AdParams,
265
- Ad,
266
- AdResponse,
267
- MessageObject,
268
- DeviceObject,
269
- UserObject,
270
- } from '@gravity-ai/api';
271
- ```
272
-
273
- ## Using with React
274
-
275
- For React applications, consider using the companion package `@gravity-ai/react` which provides ready-to-use components with automatic tracking:
276
-
277
- ```bash
278
- npm install @gravity-ai/api @gravity-ai/react
279
- ```
280
-
281
- ```tsx
282
- import { Client } from '@gravity-ai/api';
283
- import { AdBanner } from '@gravity-ai/react';
284
-
285
- const client = new Client('your-api-key');
286
-
287
- function ChatApp() {
288
- const [ad, setAd] = useState(null);
289
-
290
- useEffect(() => {
291
- client.getAd({
292
- messages,
293
- sessionId: 'session-123',
294
- userId: 'user-456',
295
- }).then(res => setAd(res?.ads[0] || null));
296
- }, [messages]);
297
-
298
- return <AdBanner ad={ad} theme="dark" />;
299
- }
300
- ```
301
-
302
- ## Best Practices
303
-
304
- 1. **Always include `sessionId` and `userId`** — These directly impact publisher revenue through better frequency capping and ad relevance.
305
-
306
- 2. **Handle null responses gracefully** — Don't break the user experience when no ad is available.
307
-
308
- 3. **Fire impression url** — Use the `impUrl` when the ad becomes visible to properly track impressions.
15
+ **[https://www.trygravity.ai/api](https://www.trygravity.ai/api)**
309
16
 
310
17
  ## License
311
18
 
package/dist/index.d.mts CHANGED
@@ -8,6 +8,58 @@ type Role = 'user' | 'assistant';
8
8
  * @description Used for demographic targeting of advertisements
9
9
  */
10
10
  type Gender = 'male' | 'female' | 'other';
11
+ /**
12
+ * Placement positions for ad rendering
13
+ * @description Specifies where ads should appear relative to the AI response
14
+ */
15
+ type Placement = 'above_response' | 'below_response' | 'inline_response' | 'left_response' | 'right_response';
16
+ /**
17
+ * Individual ad placement specification
18
+ * @description Defines a single ad slot with its position and optional tracking ID
19
+ * @example
20
+ * ```typescript
21
+ * const placement: PlacementObject = {
22
+ * placement: 'below_response',
23
+ * placement_id: 'sidebar-1'
24
+ * };
25
+ * ```
26
+ */
27
+ interface PlacementObject {
28
+ /** Position where the ad should appear (required) */
29
+ placement: Placement;
30
+ /** Optional tracking ID for this specific ad slot */
31
+ placement_id?: string;
32
+ }
33
+ /**
34
+ * Describes how your app will display the ad
35
+ * @description Contains placement array and rendering capabilities for ad customization
36
+ * @example
37
+ * ```typescript
38
+ * const renderContext: RenderContextObject = {
39
+ * placements: [
40
+ * { placement: 'below_response' },
41
+ * { placement: 'right_response', placement_id: 'sidebar' }
42
+ * ],
43
+ * max_ad_length: 200
44
+ * };
45
+ * ```
46
+ */
47
+ interface RenderContextObject {
48
+ /** Where you plan to show the ad(s). Array of 1-3 placements, must match numAds. */
49
+ placements: PlacementObject[];
50
+ /** Character limit you can display, so we don't send copy that gets truncated */
51
+ max_ad_length?: number;
52
+ /** Whether you can render markdown-formatted text */
53
+ supports_markdown?: boolean;
54
+ /** Whether you can render clickable links */
55
+ supports_links?: boolean;
56
+ /** Whether you can display images (brand logos, product images) */
57
+ supports_images?: boolean;
58
+ /** Whether you can render CTA buttons */
59
+ supports_cta_button?: boolean;
60
+ /** Additional render context properties (supports JSON objects) */
61
+ [key: string]: PlacementObject[] | string | number | boolean | object | null | undefined;
62
+ }
11
63
  /**
12
64
  * Represents a single message in a conversation
13
65
  * @description Used to provide conversation context for contextual ad targeting
@@ -39,16 +91,18 @@ interface MessageObject {
39
91
  * ```
40
92
  */
41
93
  interface DeviceObject {
42
- /** User's IP address for geo-targeting */
94
+ /** User's IP address for geo-targeting (required) */
43
95
  ip: string;
96
+ /** Browser user-agent string (optional for non-web publishers like IDEs, CLIs, mobile apps) */
97
+ ua?: string;
44
98
  /** ISO 3166-1 alpha-2 country code (e.g., 'US', 'GB', 'DE') */
45
- country: string;
46
- /** User agent string from the browser */
47
- ua: string;
99
+ country?: string;
48
100
  /** Operating system name (e.g., 'macOS', 'Windows', 'iOS', 'Android') */
49
101
  os?: string;
50
102
  /** Identifier for Advertisers (mobile advertising ID) */
51
103
  ifa?: string;
104
+ /** Additional device properties (timezone, locale, browser, device_type, screen dimensions, etc.) */
105
+ [key: string]: string | number | boolean | undefined;
52
106
  }
53
107
  /**
54
108
  * User profile information for ad targeting
@@ -64,7 +118,7 @@ interface DeviceObject {
64
118
  * ```
65
119
  */
66
120
  interface UserObject {
67
- /** Unique user identifier for frequency capping and tracking */
121
+ /** Unique user identifier for improving ad relevance */
68
122
  uid?: string;
69
123
  /** User's gender for demographic targeting */
70
124
  gender?: Gender;
@@ -72,6 +126,8 @@ interface UserObject {
72
126
  age?: string;
73
127
  /** Comma-separated keywords representing user interests */
74
128
  keywords?: string;
129
+ /** Additional user properties (email, subscription_tier, user_interests, company_size, etc.) */
130
+ [key: string]: string | string[] | number | boolean | Gender | undefined;
75
131
  }
76
132
  /**
77
133
  * Parameters for requesting an advertisement
@@ -84,7 +140,12 @@ interface UserObject {
84
140
  * { role: 'assistant', content: 'What is your budget?' }
85
141
  * ],
86
142
  * sessionId: 'session-123',
143
+ * numAds: 1,
144
+ * render_context: {
145
+ * placements: [{ placement: 'below_response' }]
146
+ * },
87
147
  * userId: 'user-456',
148
+ * testAd: true,
88
149
  * user: { gender: 'male', age: '25-34' },
89
150
  * device: { ip: '1.2.3.4', country: 'US', ua: 'Mozilla/5.0...' },
90
151
  * excludedTopics: ['politics'],
@@ -95,8 +156,12 @@ interface UserObject {
95
156
  interface AdParams {
96
157
  /** Array of conversation messages for contextual targeting (required) */
97
158
  messages: MessageObject[];
98
- /** Session identifier for tracking user sessions */
99
- sessionId?: string;
159
+ /** Session identifier for ad relevance (required) */
160
+ sessionId: string;
161
+ /** Render context with placements array (required). Length of placements must match numAds. */
162
+ render_context: RenderContextObject;
163
+ /** Number of ads to return (1-3). Must match render_context.placements length. */
164
+ numAds?: number;
100
165
  /** Unique user identifier */
101
166
  userId?: string;
102
167
  /** Device and location information */
@@ -107,8 +172,6 @@ interface AdParams {
107
172
  excludedTopics?: string[];
108
173
  /** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
109
174
  relevancy?: number | null;
110
- /** Number of ads to return (1-3, default 1) */
111
- numAds?: number;
112
175
  /** Returns a test ad when true */
113
176
  testAd?: boolean;
114
177
  /**
@@ -185,8 +248,12 @@ interface ApiErrorResponse {
185
248
  * Base fields shared across all ad requests.
186
249
  */
187
250
  interface AdRequestBase {
188
- /** Session identifier for tracking user sessions */
189
- sessionId?: string;
251
+ /** Session identifier for ad relevance (required) */
252
+ sessionId: string;
253
+ /** Render context with placements array (required). Length of placements must match numAds. */
254
+ render_context: RenderContextObject;
255
+ /** Number of ads to return (1-3). Must match render_context.placements length. */
256
+ numAds?: number;
190
257
  /** Unique user identifier */
191
258
  userId?: string;
192
259
  /** Device and location information */
@@ -197,8 +264,6 @@ interface AdRequestBase {
197
264
  excludedTopics?: string[];
198
265
  /** Minimum relevancy score threshold (0-1) */
199
266
  relevancy?: number | null;
200
- /** Number of ads to return (1-3, default 1) */
201
- numAds?: number;
202
267
  /** Returns a test ad when true */
203
268
  testAd?: boolean;
204
269
  /** Additional custom fields */
@@ -221,13 +286,16 @@ interface NonContextualAdParams extends AdRequestBase {
221
286
  /**
222
287
  * POST /api/v1/bid
223
288
  * @description Two-phase bid request. Returns bid price and bidId.
224
- * @note Does NOT extend AdRequestBaseV1 - has its own field set.
225
289
  */
226
290
  interface BidParams {
227
291
  /** Array of conversation messages (required) */
228
292
  messages: MessageObject[];
229
- /** Session identifier */
230
- sessionId?: string;
293
+ /** Session identifier for ad relevance (required) */
294
+ sessionId: string;
295
+ /** Render context with placements array (required). Length of placements must match numAds. */
296
+ render_context: RenderContextObject;
297
+ /** Number of ads to return (1-3). Must match render_context.placements length. */
298
+ numAds?: number;
231
299
  /** Unique user identifier */
232
300
  userId?: string;
233
301
  /** Chat/conversation identifier */
@@ -304,6 +372,10 @@ interface ClientParams {
304
372
  * { role: 'user', content: 'What laptop should I buy?' }
305
373
  * ],
306
374
  * sessionId: 'session-123',
375
+ * numAds: 1,
376
+ * render_context: {
377
+ * placements: [{ placement: 'below_response' }]
378
+ * }
307
379
  * });
308
380
  *
309
381
  * if (response) {
@@ -370,6 +442,10 @@ declare class Client {
370
442
  * { role: 'assistant', content: 'What is your budget range?' }
371
443
  * ],
372
444
  * sessionId: 'session-123',
445
+ * numAds: 1,
446
+ * render_context: {
447
+ * placements: [{ placement: 'below_response' }]
448
+ * },
373
449
  * userId: 'user-456',
374
450
  * });
375
451
  *
@@ -384,6 +460,11 @@ declare class Client {
384
460
  * const response = await client.getAd({
385
461
  * messages: [...],
386
462
  * sessionId: 'session-123',
463
+ * numAds: 1,
464
+ * render_context: {
465
+ * placements: [{ placement: 'below_response' }],
466
+ * max_ad_length: 200
467
+ * },
387
468
  * userId: 'user-456',
388
469
  * user: {
389
470
  * uid: 'user-123',
@@ -402,7 +483,12 @@ declare class Client {
402
483
  *
403
484
  * @example Handling the response
404
485
  * ```typescript
405
- * const response = await client.getAd({ messages, sessionId: '...' });
486
+ * const response = await client.getAd({
487
+ * messages,
488
+ * sessionId: '...',
489
+ * numAds: 1,
490
+ * render_context: { placements: [{ placement: 'below_response' }] }
491
+ * });
406
492
  *
407
493
  * if (response) {
408
494
  * const ad = response.ads[0];
@@ -433,10 +519,10 @@ declare class Client {
433
519
  * @description Fetches ads without context matching. Useful for brand awareness placements.
434
520
  * Returns null if no ad is available or on error.
435
521
  *
436
- * @param params - Optional request parameters
522
+ * @param params - Request parameters (sessionId required)
437
523
  * @returns Promise resolving to AdResponse or null if no ad available
438
524
  */
439
- nonContextualAd(params?: NonContextualAdParams): Promise<AdResponse | null>;
525
+ nonContextualAd(params: NonContextualAdParams): Promise<AdResponse | null>;
440
526
  /**
441
527
  * Request a bid price for contextual ad placement
442
528
  *
@@ -472,4 +558,4 @@ declare class Client {
472
558
  private handleError;
473
559
  }
474
560
 
475
- export { type Ad, type AdParams, type AdRequestBase, type AdResponse, type ApiErrorResponse, type BidParams, type BidResponse, Client, type ClientParams, type DeviceObject, type Gender, type MessageObject, type NonContextualAdParams, type RenderParams, type Role, type SummaryAdParams, type UserObject };
561
+ export { type Ad, type AdParams, type AdRequestBase, type AdResponse, type ApiErrorResponse, type BidParams, type BidResponse, Client, type ClientParams, type DeviceObject, type Gender, type MessageObject, type NonContextualAdParams, type Placement, type PlacementObject, type RenderContextObject, type RenderParams, type Role, type SummaryAdParams, type UserObject };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,58 @@ type Role = 'user' | 'assistant';
8
8
  * @description Used for demographic targeting of advertisements
9
9
  */
10
10
  type Gender = 'male' | 'female' | 'other';
11
+ /**
12
+ * Placement positions for ad rendering
13
+ * @description Specifies where ads should appear relative to the AI response
14
+ */
15
+ type Placement = 'above_response' | 'below_response' | 'inline_response' | 'left_response' | 'right_response';
16
+ /**
17
+ * Individual ad placement specification
18
+ * @description Defines a single ad slot with its position and optional tracking ID
19
+ * @example
20
+ * ```typescript
21
+ * const placement: PlacementObject = {
22
+ * placement: 'below_response',
23
+ * placement_id: 'sidebar-1'
24
+ * };
25
+ * ```
26
+ */
27
+ interface PlacementObject {
28
+ /** Position where the ad should appear (required) */
29
+ placement: Placement;
30
+ /** Optional tracking ID for this specific ad slot */
31
+ placement_id?: string;
32
+ }
33
+ /**
34
+ * Describes how your app will display the ad
35
+ * @description Contains placement array and rendering capabilities for ad customization
36
+ * @example
37
+ * ```typescript
38
+ * const renderContext: RenderContextObject = {
39
+ * placements: [
40
+ * { placement: 'below_response' },
41
+ * { placement: 'right_response', placement_id: 'sidebar' }
42
+ * ],
43
+ * max_ad_length: 200
44
+ * };
45
+ * ```
46
+ */
47
+ interface RenderContextObject {
48
+ /** Where you plan to show the ad(s). Array of 1-3 placements, must match numAds. */
49
+ placements: PlacementObject[];
50
+ /** Character limit you can display, so we don't send copy that gets truncated */
51
+ max_ad_length?: number;
52
+ /** Whether you can render markdown-formatted text */
53
+ supports_markdown?: boolean;
54
+ /** Whether you can render clickable links */
55
+ supports_links?: boolean;
56
+ /** Whether you can display images (brand logos, product images) */
57
+ supports_images?: boolean;
58
+ /** Whether you can render CTA buttons */
59
+ supports_cta_button?: boolean;
60
+ /** Additional render context properties (supports JSON objects) */
61
+ [key: string]: PlacementObject[] | string | number | boolean | object | null | undefined;
62
+ }
11
63
  /**
12
64
  * Represents a single message in a conversation
13
65
  * @description Used to provide conversation context for contextual ad targeting
@@ -39,16 +91,18 @@ interface MessageObject {
39
91
  * ```
40
92
  */
41
93
  interface DeviceObject {
42
- /** User's IP address for geo-targeting */
94
+ /** User's IP address for geo-targeting (required) */
43
95
  ip: string;
96
+ /** Browser user-agent string (optional for non-web publishers like IDEs, CLIs, mobile apps) */
97
+ ua?: string;
44
98
  /** ISO 3166-1 alpha-2 country code (e.g., 'US', 'GB', 'DE') */
45
- country: string;
46
- /** User agent string from the browser */
47
- ua: string;
99
+ country?: string;
48
100
  /** Operating system name (e.g., 'macOS', 'Windows', 'iOS', 'Android') */
49
101
  os?: string;
50
102
  /** Identifier for Advertisers (mobile advertising ID) */
51
103
  ifa?: string;
104
+ /** Additional device properties (timezone, locale, browser, device_type, screen dimensions, etc.) */
105
+ [key: string]: string | number | boolean | undefined;
52
106
  }
53
107
  /**
54
108
  * User profile information for ad targeting
@@ -64,7 +118,7 @@ interface DeviceObject {
64
118
  * ```
65
119
  */
66
120
  interface UserObject {
67
- /** Unique user identifier for frequency capping and tracking */
121
+ /** Unique user identifier for improving ad relevance */
68
122
  uid?: string;
69
123
  /** User's gender for demographic targeting */
70
124
  gender?: Gender;
@@ -72,6 +126,8 @@ interface UserObject {
72
126
  age?: string;
73
127
  /** Comma-separated keywords representing user interests */
74
128
  keywords?: string;
129
+ /** Additional user properties (email, subscription_tier, user_interests, company_size, etc.) */
130
+ [key: string]: string | string[] | number | boolean | Gender | undefined;
75
131
  }
76
132
  /**
77
133
  * Parameters for requesting an advertisement
@@ -84,7 +140,12 @@ interface UserObject {
84
140
  * { role: 'assistant', content: 'What is your budget?' }
85
141
  * ],
86
142
  * sessionId: 'session-123',
143
+ * numAds: 1,
144
+ * render_context: {
145
+ * placements: [{ placement: 'below_response' }]
146
+ * },
87
147
  * userId: 'user-456',
148
+ * testAd: true,
88
149
  * user: { gender: 'male', age: '25-34' },
89
150
  * device: { ip: '1.2.3.4', country: 'US', ua: 'Mozilla/5.0...' },
90
151
  * excludedTopics: ['politics'],
@@ -95,8 +156,12 @@ interface UserObject {
95
156
  interface AdParams {
96
157
  /** Array of conversation messages for contextual targeting (required) */
97
158
  messages: MessageObject[];
98
- /** Session identifier for tracking user sessions */
99
- sessionId?: string;
159
+ /** Session identifier for ad relevance (required) */
160
+ sessionId: string;
161
+ /** Render context with placements array (required). Length of placements must match numAds. */
162
+ render_context: RenderContextObject;
163
+ /** Number of ads to return (1-3). Must match render_context.placements length. */
164
+ numAds?: number;
100
165
  /** Unique user identifier */
101
166
  userId?: string;
102
167
  /** Device and location information */
@@ -107,8 +172,6 @@ interface AdParams {
107
172
  excludedTopics?: string[];
108
173
  /** Minimum relevancy score threshold (0-1). Higher = more relevant but fewer ads */
109
174
  relevancy?: number | null;
110
- /** Number of ads to return (1-3, default 1) */
111
- numAds?: number;
112
175
  /** Returns a test ad when true */
113
176
  testAd?: boolean;
114
177
  /**
@@ -185,8 +248,12 @@ interface ApiErrorResponse {
185
248
  * Base fields shared across all ad requests.
186
249
  */
187
250
  interface AdRequestBase {
188
- /** Session identifier for tracking user sessions */
189
- sessionId?: string;
251
+ /** Session identifier for ad relevance (required) */
252
+ sessionId: string;
253
+ /** Render context with placements array (required). Length of placements must match numAds. */
254
+ render_context: RenderContextObject;
255
+ /** Number of ads to return (1-3). Must match render_context.placements length. */
256
+ numAds?: number;
190
257
  /** Unique user identifier */
191
258
  userId?: string;
192
259
  /** Device and location information */
@@ -197,8 +264,6 @@ interface AdRequestBase {
197
264
  excludedTopics?: string[];
198
265
  /** Minimum relevancy score threshold (0-1) */
199
266
  relevancy?: number | null;
200
- /** Number of ads to return (1-3, default 1) */
201
- numAds?: number;
202
267
  /** Returns a test ad when true */
203
268
  testAd?: boolean;
204
269
  /** Additional custom fields */
@@ -221,13 +286,16 @@ interface NonContextualAdParams extends AdRequestBase {
221
286
  /**
222
287
  * POST /api/v1/bid
223
288
  * @description Two-phase bid request. Returns bid price and bidId.
224
- * @note Does NOT extend AdRequestBaseV1 - has its own field set.
225
289
  */
226
290
  interface BidParams {
227
291
  /** Array of conversation messages (required) */
228
292
  messages: MessageObject[];
229
- /** Session identifier */
230
- sessionId?: string;
293
+ /** Session identifier for ad relevance (required) */
294
+ sessionId: string;
295
+ /** Render context with placements array (required). Length of placements must match numAds. */
296
+ render_context: RenderContextObject;
297
+ /** Number of ads to return (1-3). Must match render_context.placements length. */
298
+ numAds?: number;
231
299
  /** Unique user identifier */
232
300
  userId?: string;
233
301
  /** Chat/conversation identifier */
@@ -304,6 +372,10 @@ interface ClientParams {
304
372
  * { role: 'user', content: 'What laptop should I buy?' }
305
373
  * ],
306
374
  * sessionId: 'session-123',
375
+ * numAds: 1,
376
+ * render_context: {
377
+ * placements: [{ placement: 'below_response' }]
378
+ * }
307
379
  * });
308
380
  *
309
381
  * if (response) {
@@ -370,6 +442,10 @@ declare class Client {
370
442
  * { role: 'assistant', content: 'What is your budget range?' }
371
443
  * ],
372
444
  * sessionId: 'session-123',
445
+ * numAds: 1,
446
+ * render_context: {
447
+ * placements: [{ placement: 'below_response' }]
448
+ * },
373
449
  * userId: 'user-456',
374
450
  * });
375
451
  *
@@ -384,6 +460,11 @@ declare class Client {
384
460
  * const response = await client.getAd({
385
461
  * messages: [...],
386
462
  * sessionId: 'session-123',
463
+ * numAds: 1,
464
+ * render_context: {
465
+ * placements: [{ placement: 'below_response' }],
466
+ * max_ad_length: 200
467
+ * },
387
468
  * userId: 'user-456',
388
469
  * user: {
389
470
  * uid: 'user-123',
@@ -402,7 +483,12 @@ declare class Client {
402
483
  *
403
484
  * @example Handling the response
404
485
  * ```typescript
405
- * const response = await client.getAd({ messages, sessionId: '...' });
486
+ * const response = await client.getAd({
487
+ * messages,
488
+ * sessionId: '...',
489
+ * numAds: 1,
490
+ * render_context: { placements: [{ placement: 'below_response' }] }
491
+ * });
406
492
  *
407
493
  * if (response) {
408
494
  * const ad = response.ads[0];
@@ -433,10 +519,10 @@ declare class Client {
433
519
  * @description Fetches ads without context matching. Useful for brand awareness placements.
434
520
  * Returns null if no ad is available or on error.
435
521
  *
436
- * @param params - Optional request parameters
522
+ * @param params - Request parameters (sessionId required)
437
523
  * @returns Promise resolving to AdResponse or null if no ad available
438
524
  */
439
- nonContextualAd(params?: NonContextualAdParams): Promise<AdResponse | null>;
525
+ nonContextualAd(params: NonContextualAdParams): Promise<AdResponse | null>;
440
526
  /**
441
527
  * Request a bid price for contextual ad placement
442
528
  *
@@ -472,4 +558,4 @@ declare class Client {
472
558
  private handleError;
473
559
  }
474
560
 
475
- export { type Ad, type AdParams, type AdRequestBase, type AdResponse, type ApiErrorResponse, type BidParams, type BidResponse, Client, type ClientParams, type DeviceObject, type Gender, type MessageObject, type NonContextualAdParams, type RenderParams, type Role, type SummaryAdParams, type UserObject };
561
+ export { type Ad, type AdParams, type AdRequestBase, type AdResponse, type ApiErrorResponse, type BidParams, type BidResponse, Client, type ClientParams, type DeviceObject, type Gender, type MessageObject, type NonContextualAdParams, type Placement, type PlacementObject, type RenderContextObject, type RenderParams, type Role, type SummaryAdParams, type UserObject };
package/dist/index.js CHANGED
@@ -90,6 +90,10 @@ var Client = class {
90
90
  * { role: 'assistant', content: 'What is your budget range?' }
91
91
  * ],
92
92
  * sessionId: 'session-123',
93
+ * numAds: 1,
94
+ * render_context: {
95
+ * placements: [{ placement: 'below_response' }]
96
+ * },
93
97
  * userId: 'user-456',
94
98
  * });
95
99
  *
@@ -104,6 +108,11 @@ var Client = class {
104
108
  * const response = await client.getAd({
105
109
  * messages: [...],
106
110
  * sessionId: 'session-123',
111
+ * numAds: 1,
112
+ * render_context: {
113
+ * placements: [{ placement: 'below_response' }],
114
+ * max_ad_length: 200
115
+ * },
107
116
  * userId: 'user-456',
108
117
  * user: {
109
118
  * uid: 'user-123',
@@ -122,7 +131,12 @@ var Client = class {
122
131
  *
123
132
  * @example Handling the response
124
133
  * ```typescript
125
- * const response = await client.getAd({ messages, sessionId: '...' });
134
+ * const response = await client.getAd({
135
+ * messages,
136
+ * sessionId: '...',
137
+ * numAds: 1,
138
+ * render_context: { placements: [{ placement: 'below_response' }] }
139
+ * });
126
140
  *
127
141
  * if (response) {
128
142
  * const ad = response.ads[0];
@@ -194,10 +208,10 @@ var Client = class {
194
208
  * @description Fetches ads without context matching. Useful for brand awareness placements.
195
209
  * Returns null if no ad is available or on error.
196
210
  *
197
- * @param params - Optional request parameters
211
+ * @param params - Request parameters (sessionId required)
198
212
  * @returns Promise resolving to AdResponse or null if no ad available
199
213
  */
200
- async nonContextualAd(params = {}) {
214
+ async nonContextualAd(params) {
201
215
  try {
202
216
  const body = {
203
217
  ...params,
package/dist/index.mjs CHANGED
@@ -54,6 +54,10 @@ var Client = class {
54
54
  * { role: 'assistant', content: 'What is your budget range?' }
55
55
  * ],
56
56
  * sessionId: 'session-123',
57
+ * numAds: 1,
58
+ * render_context: {
59
+ * placements: [{ placement: 'below_response' }]
60
+ * },
57
61
  * userId: 'user-456',
58
62
  * });
59
63
  *
@@ -68,6 +72,11 @@ var Client = class {
68
72
  * const response = await client.getAd({
69
73
  * messages: [...],
70
74
  * sessionId: 'session-123',
75
+ * numAds: 1,
76
+ * render_context: {
77
+ * placements: [{ placement: 'below_response' }],
78
+ * max_ad_length: 200
79
+ * },
71
80
  * userId: 'user-456',
72
81
  * user: {
73
82
  * uid: 'user-123',
@@ -86,7 +95,12 @@ var Client = class {
86
95
  *
87
96
  * @example Handling the response
88
97
  * ```typescript
89
- * const response = await client.getAd({ messages, sessionId: '...' });
98
+ * const response = await client.getAd({
99
+ * messages,
100
+ * sessionId: '...',
101
+ * numAds: 1,
102
+ * render_context: { placements: [{ placement: 'below_response' }] }
103
+ * });
90
104
  *
91
105
  * if (response) {
92
106
  * const ad = response.ads[0];
@@ -158,10 +172,10 @@ var Client = class {
158
172
  * @description Fetches ads without context matching. Useful for brand awareness placements.
159
173
  * Returns null if no ad is available or on error.
160
174
  *
161
- * @param params - Optional request parameters
175
+ * @param params - Request parameters (sessionId required)
162
176
  * @returns Promise resolving to AdResponse or null if no ad available
163
177
  */
164
- async nonContextualAd(params = {}) {
178
+ async nonContextualAd(params) {
165
179
  try {
166
180
  const body = {
167
181
  ...params,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ai/api",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "description": "Gravity JS SDK for retrieving targeted advertisements",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",