meterflow 0.5.1 → 0.6.0
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 +52 -4
- package/dist/index.cjs +3 -5
- package/dist/index.js +3 -5
- package/dist/index.mjs +3 -5
- package/dist/resources/credits.d.ts +1 -5
- package/dist/resources/credits.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -177,10 +177,10 @@ bal.total_granted; // "5250.000000"
|
|
|
177
177
|
bal.total_consumed; // "1705.000000"
|
|
178
178
|
```
|
|
179
179
|
|
|
180
|
-
**Transactions** — the full history, newest first,
|
|
180
|
+
**Transactions** — the customer's full history, newest first, in one response:
|
|
181
181
|
|
|
182
182
|
```typescript
|
|
183
|
-
const txns = await client.credits.transactions("customer_123"
|
|
183
|
+
const txns = await client.credits.transactions("customer_123");
|
|
184
184
|
for (const t of txns) {
|
|
185
185
|
console.log(t.transaction_type, t.amount, t.balance_after, t.description, t.created_at);
|
|
186
186
|
}
|
|
@@ -224,6 +224,39 @@ for (let i = 0; i < events.length; i += MAX_BATCH_EVENTS) {
|
|
|
224
224
|
}
|
|
225
225
|
```
|
|
226
226
|
|
|
227
|
+
### Record quantities, not calls
|
|
228
|
+
|
|
229
|
+
**A batch of 100 events is still 100 events.** Batching saves HTTP requests, not quota: each item in a `recordBatch` is its own record, and your plan's monthly allowance counts records. The thing that actually moves the number is sending **one event that carries the quantity**.
|
|
230
|
+
|
|
231
|
+
If your meter's aggregation is `sum`, MeterFlow adds up the `value` of its events — so one event with `value: 100` is **one** event against your monthly allowance and **100 units** against the meter. Record per unit of work instead of per API call:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
// ❌ One event per call — 100 calls is 100 events against your plan.
|
|
235
|
+
for (const chunk of completion.chunks) {
|
|
236
|
+
await client.usage.record({
|
|
237
|
+
event_name: "tokens.used",
|
|
238
|
+
customer_external_id: "customer_123",
|
|
239
|
+
value: 1,
|
|
240
|
+
properties: {},
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ✅ One event carrying the quantity — 1 event against your plan, 1,300 units on the meter.
|
|
245
|
+
await client.usage.record({
|
|
246
|
+
event_name: "tokens.used",
|
|
247
|
+
customer_external_id: "customer_123",
|
|
248
|
+
value: 1300, // a `sum` meter adds these up
|
|
249
|
+
properties: { model: "gpt-4o-mini" },
|
|
250
|
+
});
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
This is usually the difference between fitting a plan and outgrowing it: 1,000 users × 100 calls a day is ~3M events a month recorded per call, and ~30k recorded per session. Aggregate in your app over whatever window you can afford to lose on a crash — a request, a job, a minute — then send one event.
|
|
254
|
+
|
|
255
|
+
Two things to keep in mind:
|
|
256
|
+
|
|
257
|
+
- **It only helps a `sum` meter.** A `count` meter ignores `value` and counts occurrences, so there each occurrence has to be its own event. `max`, `min` and `unique_count` read `value` as a measurement, not a quantity to add.
|
|
258
|
+
- **Billing is unchanged.** A metered plan limit charges per unit — the event's `value` — so 100 units cost the same whether they arrived as 100 events or one. You are not rounding anyone's bill by batching.
|
|
259
|
+
|
|
227
260
|
**Summary** — a customer's usage, broken down by meter:
|
|
228
261
|
|
|
229
262
|
```typescript
|
|
@@ -345,6 +378,17 @@ await client.credits.deduct(
|
|
|
345
378
|
|
|
346
379
|
Every write method accepts the option: `credits.grant/deduct`, `usage.record/recordBatch`, `subscriptions.create/update`. Use a key that identifies the *business operation* (order ID, job ID) — not a random value per attempt, which would defeat the purpose.
|
|
347
380
|
|
|
381
|
+
**A key is bound to the body you first used it with.** MeterFlow stores a fingerprint of the request alongside the key, so a retry only replays when it is genuinely the same request. Reuse the key with a *different* body and the call is refused with `ConflictError` (409) instead of quietly handing back the original record:
|
|
382
|
+
|
|
383
|
+
```typescript
|
|
384
|
+
await client.credits.grant({ customer_external_id: "customer_123", amount: 100, metadata: {} }, { idempotencyKey: "welcome-123" });
|
|
385
|
+
|
|
386
|
+
// Same key, different amount → ConflictError. Nothing is written, and the first grant stands.
|
|
387
|
+
await client.credits.grant({ customer_external_id: "customer_123", amount: 250, metadata: {} }, { idempotencyKey: "welcome-123" });
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
That is deliberate: silently returning the first record would turn two different operations into one and report success. One key per operation, reused only to retry that same operation. Keys stay namespaced per environment, so a `mf_test_` and a `mf_live_` key never collide.
|
|
391
|
+
|
|
348
392
|
## Automatic retries
|
|
349
393
|
|
|
350
394
|
The SDK retries transient failures for you — exponential backoff with jitter, 3 attempts by default:
|
|
@@ -377,7 +421,7 @@ Every error carries:
|
|
|
377
421
|
|
|
378
422
|
- **`requestId`** — the API's `X-Request-ID` for that call. Include it when contacting support; it pinpoints the exact request in our logs.
|
|
379
423
|
- **`statusCode`**, **`errorType`**, and **`retryable`** — for programmatic handling and structured logging.
|
|
380
|
-
- **`message`** — the API's own sentence (e.g. `Plan limit reached: the Drip plan includes
|
|
424
|
+
- **`message`** — the API's own sentence (e.g. `Plan limit reached: the Drip plan includes 50,000 usage events per month…`). A `ValidationError` appends its field detail (`Validation failed: amount: must be greater than 0`) and also exposes it structured as **`.fields`** (`{ field, message }[]`).
|
|
381
425
|
|
|
382
426
|
```typescript
|
|
383
427
|
import { MeterFlowError, InsufficientCreditsError, RateLimitError } from "meterflow";
|
|
@@ -440,6 +484,10 @@ Every delivery is a flat JSON object (`Content-Type: application/json`) with two
|
|
|
440
484
|
| `subscription.created` | a subscription is created | `customer_id`, `plan_id`, `subscription_id`, `status` |
|
|
441
485
|
| `subscription.updated` | a subscription is updated | `customer_id`, `subscription_id`, `status` |
|
|
442
486
|
| `limit.reached` | a customer first reaches a **hard** or **soft** limit's included units this billing period (once per meter per period; metered limits report through `credit.deducted` instead) | `customer_id`, `subscription_id`, `feature` (the meter's `event_name`), `meter_id`, `limit_type`, `included`, `used`, `period_start`, `period_end` |
|
|
487
|
+
| `balance.low` | a deduction takes a customer's balance **down through** this endpoint's `low_balance_threshold` — a crossing, so it fires once and not again while they stay below. Needs the threshold set on the webhook; without one it never fires | `customer_id`, `balance`, `threshold`, `transaction_id` |
|
|
488
|
+
| `balance.exhausted` | a balance reaches zero, **or** a request is refused for want of credits (a deduction returning 402, or `entitlements.check` answering `insufficient_credits`). Unlike `balance.low` this repeats: every refused request is its own blocked request | `customer_id`, `balance`, `reason` (`balance_zero` / `insufficient_credits`), `requested_amount` and `feature` where they apply, `transaction_id` on the zero case |
|
|
489
|
+
| `subscription.renewed` | a billing period rolls forward (the renewer; no `subscription.updated` is emitted for a renewal) | `customer_id`, `subscription_id`, `plan_id`, `current_period_start`, `current_period_end`, `credits_granted` |
|
|
490
|
+
| `subscription.canceled` | a subscription's status becomes canceled — fired **alongside** `subscription.updated`, once, on the call that actually cancels | `customer_id`, `subscription_id`, `plan_id`, `canceled_at` |
|
|
443
491
|
|
|
444
492
|
For example, a `credit.granted` delivery:
|
|
445
493
|
|
|
@@ -509,6 +557,6 @@ await client.usage.record({
|
|
|
509
557
|
|
|
510
558
|
// Support asks: "what's Ana's situation?"
|
|
511
559
|
const balance = await client.credits.balance("ana");
|
|
512
|
-
const history = await client.credits.transactions("ana"
|
|
560
|
+
const history = await client.credits.transactions("ana");
|
|
513
561
|
const usage = await client.usage.summary("ana");
|
|
514
562
|
```
|
package/dist/index.cjs
CHANGED
|
@@ -35,7 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
36
|
|
|
37
37
|
// package.json
|
|
38
|
-
var version = "0.
|
|
38
|
+
var version = "0.6.0";
|
|
39
39
|
|
|
40
40
|
// src/errors.ts
|
|
41
41
|
var MeterFlowError = class extends Error {
|
|
@@ -245,10 +245,8 @@ var CreditsResource = class {
|
|
|
245
245
|
balance(customerId) {
|
|
246
246
|
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/balance`);
|
|
247
247
|
}
|
|
248
|
-
transactions(customerId
|
|
249
|
-
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/transactions
|
|
250
|
-
...query !== void 0 && { query }
|
|
251
|
-
});
|
|
248
|
+
transactions(customerId) {
|
|
249
|
+
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/transactions`);
|
|
252
250
|
}
|
|
253
251
|
};
|
|
254
252
|
|
package/dist/index.js
CHANGED
|
@@ -35,7 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
36
|
|
|
37
37
|
// package.json
|
|
38
|
-
var version = "0.
|
|
38
|
+
var version = "0.6.0";
|
|
39
39
|
|
|
40
40
|
// src/errors.ts
|
|
41
41
|
var MeterFlowError = class extends Error {
|
|
@@ -245,10 +245,8 @@ var CreditsResource = class {
|
|
|
245
245
|
balance(customerId) {
|
|
246
246
|
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/balance`);
|
|
247
247
|
}
|
|
248
|
-
transactions(customerId
|
|
249
|
-
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/transactions
|
|
250
|
-
...query !== void 0 && { query }
|
|
251
|
-
});
|
|
248
|
+
transactions(customerId) {
|
|
249
|
+
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/transactions`);
|
|
252
250
|
}
|
|
253
251
|
};
|
|
254
252
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// package.json
|
|
2
|
-
var version = "0.
|
|
2
|
+
var version = "0.6.0";
|
|
3
3
|
|
|
4
4
|
// src/errors.ts
|
|
5
5
|
var MeterFlowError = class extends Error {
|
|
@@ -209,10 +209,8 @@ var CreditsResource = class {
|
|
|
209
209
|
balance(customerId) {
|
|
210
210
|
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/balance`);
|
|
211
211
|
}
|
|
212
|
-
transactions(customerId
|
|
213
|
-
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/transactions
|
|
214
|
-
...query !== void 0 && { query }
|
|
215
|
-
});
|
|
212
|
+
transactions(customerId) {
|
|
213
|
+
return this.client.request("GET", `credits/${encodeURIComponent(customerId)}/transactions`);
|
|
216
214
|
}
|
|
217
215
|
};
|
|
218
216
|
|
|
@@ -10,17 +10,13 @@ export interface GrantOptions {
|
|
|
10
10
|
export interface DeductOptions {
|
|
11
11
|
idempotencyKey?: string;
|
|
12
12
|
}
|
|
13
|
-
export interface TransactionsQuery {
|
|
14
|
-
page?: number;
|
|
15
|
-
limit?: number;
|
|
16
|
-
}
|
|
17
13
|
export declare class CreditsResource {
|
|
18
14
|
private readonly client;
|
|
19
15
|
constructor(client: MeterFlow);
|
|
20
16
|
grant(body: CreditGrantRequest, opts?: GrantOptions): Promise<CreditTransactionResponse>;
|
|
21
17
|
deduct(body: CreditDeductRequest, opts?: DeductOptions): Promise<CreditTransactionResponse>;
|
|
22
18
|
balance(customerId: string): Promise<CreditBalanceResponse>;
|
|
23
|
-
transactions(customerId: string
|
|
19
|
+
transactions(customerId: string): Promise<CreditTransactionResponse[]>;
|
|
24
20
|
}
|
|
25
21
|
export {};
|
|
26
22
|
//# sourceMappingURL=credits.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"credits.d.ts","sourceRoot":"","sources":["../../../src/resources/credits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,KAAK,kBAAkB,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC,oBAAoB,CAAC,CAAC;AACtE,KAAK,mBAAmB,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC,qBAAqB,CAAC,CAAC;AACxE,KAAK,yBAAyB,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC,2BAA2B,CAAC,CAAC;AACpF,KAAK,qBAAqB,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC,uBAAuB,CAAC,CAAC;AAE5E,MAAM,WAAW,YAAY;IAC3B,cAAc,CAAC,EAAE,MAAM,CAAC;CACz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