@marginfront/sdk 0.0.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/LICENSE +21 -0
- package/README.md +310 -0
- package/dist/index.d.mts +1448 -0
- package/dist/index.d.ts +1448 -0
- package/dist/index.js +1365 -0
- package/dist/index.mjs +1311 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 MarginFront
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# @marginfront/sdk
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40marginfront%2Fsdk)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
Official Node.js SDK for MarginFront. Simplify integration with MarginFront's billing, customer management, and usage tracking services.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @marginfront/sdk
|
|
12
|
+
# or
|
|
13
|
+
yarn add @marginfront/sdk
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { MarginFrontClient } from '@marginfront/sdk';
|
|
20
|
+
|
|
21
|
+
const client = new MarginFrontClient('mf_sk_your_secret_key');
|
|
22
|
+
|
|
23
|
+
// Track a usage event
|
|
24
|
+
await client.trackEvent({
|
|
25
|
+
agentId: 'agent_123',
|
|
26
|
+
customerExternalId: 'customer_456',
|
|
27
|
+
signalName: 'api_call',
|
|
28
|
+
quantity: 1,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Documentation
|
|
33
|
+
|
|
34
|
+
- [Configuration Options](#configuration)
|
|
35
|
+
- [Event Tracking Examples](#event-tracking)
|
|
36
|
+
- [Usage Cost Tracking](#usage-cost-tracking)
|
|
37
|
+
- [Customer Management](#customer-management)
|
|
38
|
+
- [Error Handling](#error-handling)
|
|
39
|
+
- [Advanced Features](#advanced-features)
|
|
40
|
+
|
|
41
|
+
## Configuration
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
const client = new MarginFrontClient('mf_sk_your_secret_key', {
|
|
45
|
+
baseUrl: 'https://api.marginfront.com/v1',
|
|
46
|
+
timeout: 10000,
|
|
47
|
+
retries: 3,
|
|
48
|
+
retryDelay: 300,
|
|
49
|
+
logging: {
|
|
50
|
+
enabled: true,
|
|
51
|
+
level: 'info',
|
|
52
|
+
},
|
|
53
|
+
telemetry: {
|
|
54
|
+
enabled: true,
|
|
55
|
+
sampleRate: 1,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await client.connect();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Usage Examples
|
|
63
|
+
|
|
64
|
+
### Event Tracking
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// Simple event tracking
|
|
68
|
+
await client.trackEvent({
|
|
69
|
+
agentId: 'agent_123',
|
|
70
|
+
customerExternalId: 'customer_456',
|
|
71
|
+
signalName: 'api_call',
|
|
72
|
+
quantity: 1,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Event with metadata and usage cost tracking
|
|
76
|
+
await client.trackEvent({
|
|
77
|
+
agentId: 'agent_123',
|
|
78
|
+
customerExternalId: 'customer_456',
|
|
79
|
+
signalName: 'email_sent',
|
|
80
|
+
quantity: 1,
|
|
81
|
+
metadata: {
|
|
82
|
+
eventId: 'email_sent',
|
|
83
|
+
usageCost: [
|
|
84
|
+
{
|
|
85
|
+
serviceName: 'LLM',
|
|
86
|
+
units: 7,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
serviceName: 'Intuit',
|
|
90
|
+
units: 1,
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Batch tracking
|
|
97
|
+
await client.trackEvent({
|
|
98
|
+
records: [
|
|
99
|
+
{
|
|
100
|
+
customerExternalId: 'customer_456',
|
|
101
|
+
agentId: 'agent_123',
|
|
102
|
+
signalName: 'api_call',
|
|
103
|
+
quantity: 10,
|
|
104
|
+
metadata: {
|
|
105
|
+
endpoint: '/api/v1/search',
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
customerExternalId: 'customer_789',
|
|
110
|
+
agentId: 'agent_123',
|
|
111
|
+
signalName: 'storage',
|
|
112
|
+
quantity: 100,
|
|
113
|
+
usageDate: new Date('2023-08-15'),
|
|
114
|
+
metadata: {
|
|
115
|
+
storageType: 'object',
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Usage Cost Tracking
|
|
123
|
+
|
|
124
|
+
Track detailed cost breakdown through the `usageCost` field in metadata:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
await client.trackEvent({
|
|
128
|
+
agentId: 'agent_123',
|
|
129
|
+
customerExternalId: 'customer_456',
|
|
130
|
+
signalName: 'lead_generated',
|
|
131
|
+
quantity: 1,
|
|
132
|
+
metadata: {
|
|
133
|
+
eventId: 'lead_generated',
|
|
134
|
+
usageCost: [
|
|
135
|
+
{
|
|
136
|
+
serviceName: 'wfloengine',
|
|
137
|
+
units: 1,
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The `usageCost` field structure:
|
|
145
|
+
|
|
146
|
+
| Field | Type | Description |
|
|
147
|
+
| ----------- | ------ | ---------------------------------------- |
|
|
148
|
+
| serviceName | string | Name of the service (e.g., 'LLM', 'TTS') |
|
|
149
|
+
| units | number | Number of units consumed |
|
|
150
|
+
|
|
151
|
+
### Customer Management
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// Create a customer
|
|
155
|
+
const customer = await client.customers.create({
|
|
156
|
+
name: 'Acme Corp',
|
|
157
|
+
email: 'billing@acmecorp.com',
|
|
158
|
+
externalId: 'acme-123',
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// List customers
|
|
162
|
+
const customers = await client.customers.list({
|
|
163
|
+
limit: 10,
|
|
164
|
+
page: 1,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Get, update and delete customers
|
|
168
|
+
const customer = await client.customers.get('customer_id');
|
|
169
|
+
await client.customers.update('customer_id', { name: 'Updated Name' });
|
|
170
|
+
await client.customers.delete('customer_id');
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Invoices
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
// List invoices with filters
|
|
177
|
+
const { invoices, totalResults } = await client.invoices.list({
|
|
178
|
+
customerId: 'cust_123',
|
|
179
|
+
status: 'pending',
|
|
180
|
+
page: 1,
|
|
181
|
+
limit: 20,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Get a specific invoice
|
|
185
|
+
const invoice = await client.invoices.get('inv_abc');
|
|
186
|
+
console.log(`Invoice ${invoice.invoiceNumber}: $${invoice.totalAmount}`);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Analytics
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
const analytics = await client.analytics.usage({
|
|
193
|
+
startDate: '2024-01-01',
|
|
194
|
+
endDate: '2024-01-31',
|
|
195
|
+
groupBy: 'daily',
|
|
196
|
+
customerId: 'cust_123',
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
console.log(`Total usage: ${analytics.summary.totalQuantity}`);
|
|
200
|
+
console.log(`Total cost: $${analytics.summary.totalCost}`);
|
|
201
|
+
|
|
202
|
+
// Time series data
|
|
203
|
+
analytics.data.forEach((point) => {
|
|
204
|
+
console.log(`${point.date}: ${point.quantity} units, $${point.cost}`);
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Subscriptions
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
// List subscriptions
|
|
212
|
+
const { subscriptions } = await client.subscriptions.list({
|
|
213
|
+
status: 'active',
|
|
214
|
+
customerId: 'cust_123',
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Get subscription with usage details
|
|
218
|
+
const sub = await client.subscriptions.get('sub_abc');
|
|
219
|
+
console.log(`Usage this period: ${sub.usage.totalQuantity}`);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Portal Sessions
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
// Create a portal session (requires secret key mf_sk_*)
|
|
226
|
+
const session = await client.portalSessions.create({
|
|
227
|
+
customerId: 'cust_123',
|
|
228
|
+
returnUrl: 'https://myapp.com/account',
|
|
229
|
+
features: ['invoices', 'usage', 'subscriptions'],
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Redirect customer to the portal
|
|
233
|
+
res.redirect(session.url);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Error Handling
|
|
237
|
+
|
|
238
|
+
```typescript
|
|
239
|
+
import {
|
|
240
|
+
MarginFrontClient,
|
|
241
|
+
MarginFrontError,
|
|
242
|
+
AuthenticationError,
|
|
243
|
+
RateLimitError,
|
|
244
|
+
ValidationError,
|
|
245
|
+
} from '@marginfront/sdk';
|
|
246
|
+
|
|
247
|
+
try {
|
|
248
|
+
await client.connect();
|
|
249
|
+
await client.trackEvent({
|
|
250
|
+
/* ... */
|
|
251
|
+
});
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (error instanceof AuthenticationError) {
|
|
254
|
+
console.error(`Authentication failed. Request ID: ${error.requestId}`);
|
|
255
|
+
} else if (error instanceof RateLimitError) {
|
|
256
|
+
console.error(`Rate limit exceeded. Retry after ${error.retryAfter}s`);
|
|
257
|
+
} else if (error instanceof ValidationError) {
|
|
258
|
+
console.error(`Validation error: ${error.message}`);
|
|
259
|
+
} else if (error instanceof MarginFrontError) {
|
|
260
|
+
console.error(`API Error (${error.statusCode}): ${error.message}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## Advanced Features
|
|
266
|
+
|
|
267
|
+
### Request Retries
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
const client = new MarginFrontClient('mf_sk_your_secret_key', {
|
|
271
|
+
retries: 3,
|
|
272
|
+
retryDelay: 300,
|
|
273
|
+
});
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Logging & Telemetry
|
|
277
|
+
|
|
278
|
+
The SDK includes a telemetry system that tracks API request performance and usage patterns:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
const client = new MarginFrontClient('mf_sk_your_secret_key', {
|
|
282
|
+
logging: {
|
|
283
|
+
enabled: true,
|
|
284
|
+
level: 'debug',
|
|
285
|
+
handler: (level, message, data) => {
|
|
286
|
+
myLoggingSystem.log(level, message, data);
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
telemetry: {
|
|
290
|
+
enabled: true,
|
|
291
|
+
sampleRate: 0.5, // Track 50% of requests
|
|
292
|
+
handler: (metrics) => {
|
|
293
|
+
myMonitoringSystem.trackApiRequest(metrics);
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
You can access telemetry statistics programmatically:
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
// Get current statistics
|
|
303
|
+
const stats = client.getTelemetryStats();
|
|
304
|
+
console.log(`Total Requests: ${stats.requestCount}`);
|
|
305
|
+
console.log(`Success Rate: ${(stats.successRate * 100).toFixed(2)}%`);
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## License
|
|
309
|
+
|
|
310
|
+
MIT License. See [LICENSE](./LICENSE) for details.
|