-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete.ts
More file actions
486 lines (410 loc) · 16.1 KB
/
Copy pathcomplete.ts
File metadata and controls
486 lines (410 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* FlexPrice TypeScript SDK – Complete example
*
* Demonstrates: client init, customer CRUD, pagination, plan creation,
* subscription lifecycle, event ingestion (single + bulk), invoice listing,
* idempotency headers, per-request timeouts, and webhook handling.
*
* Prerequisites:
* FLEXPRICE_API_KEY – your API key
* FLEXPRICE_API_HOST – optional, defaults to us.api.flexprice.io
*/
import { FlexPrice } from "@flexprice/sdk";
import type {
Customer,
Subscription,
Invoice,
Plan,
CreateCustomerRequest,
UpdateCustomerRequest,
CreatePlanRequest,
CreateSubscriptionRequest,
CancelSubscriptionRequest,
IngestEventRequest,
BulkIngestEventRequest,
InvoiceFilter,
CustomerFilter,
ListCustomersResponse,
ListInvoicesResponse,
} from "@flexprice/sdk";
import { SDKError } from "@flexprice/sdk";
// ── Client initialisation ─────────────────────────────────────────────────────
const apiKey = process.env.FLEXPRICE_API_KEY;
const apiHost = process.env.FLEXPRICE_API_HOST ?? "us.api.flexprice.io";
if (!apiKey) {
console.error("FLEXPRICE_API_KEY is not set. Exiting.");
process.exit(1);
}
const serverURL = apiHost.startsWith("http") ? apiHost : `https://${apiHost}`;
const flexprice = new FlexPrice({
serverURL,
apiKeyAuth: apiKey,
});
// ── Helpers ───────────────────────────────────────────────────────────────────
/** Generate a unique external ID so the example is re-runnable. */
const externalId = (prefix: string): string => `${prefix}-${Date.now()}`;
// ── createOrGetCustomer ───────────────────────────────────────────────────────
/**
* Creates a new customer, handling a 409 Conflict gracefully by fetching the
* existing record instead. Demonstrates idempotency key and optional chaining.
*/
async function createOrGetCustomer(): Promise<Customer> {
console.log("\n// ── Create customer ──────────────────────────────────────");
const customerExternalId = externalId("acme-corp");
const request: CreateCustomerRequest = {
externalId: customerExternalId,
name: "Acme Corp",
email: "billing@acme-corp.example.com",
addressLine1: "123 Commerce Street",
addressCity: "San Francisco",
addressState: "CA",
addressPostalCode: "94105",
addressCountry: "US",
metadata: {
salesforce_id: "0010X000abcdef",
tier: "enterprise",
},
};
try {
// Pass an idempotency key via a custom request header so that retrying the
// same network call never creates a duplicate customer.
const customer = await flexprice.customers.createCustomer(request, {
headers: { "Idempotency-Key": `create-customer-${customerExternalId}` },
});
console.log(
`Created customer id=${customer.id} email=${customer.email ?? "N/A"}`
);
return customer;
} catch (err) {
// A 409 means the external_id already exists – retrieve the existing record.
if (err instanceof SDKError && err.statusCode === 409) {
console.log("Customer already exists; fetching by external_id …");
const result: ListCustomersResponse =
await flexprice.customers.queryCustomer({
externalId: customerExternalId,
limit: 1,
});
const existing = result.items?.[0];
if (!existing) {
throw new Error(
`409 returned but customer with externalId=${customerExternalId} not found`
);
}
console.log(
`Fetched existing customer id=${existing.id} name=${existing.name ?? "N/A"}`
);
return existing;
}
throw err;
}
}
// ── updateCustomer ────────────────────────────────────────────────────────────
async function updateCustomer(customerId: string): Promise<Customer> {
console.log("\n// ── Update customer ──────────────────────────────────────");
const update: UpdateCustomerRequest = {
metadata: {
salesforce_id: "0010X000abcdef",
tier: "enterprise",
onboarded: "true",
},
};
const updated = await flexprice.customers.updateCustomer(update, customerId);
console.log(
`Updated customer id=${updated.id} name=${updated.name ?? "N/A"}`
);
return updated;
}
// ── paginateCustomers ─────────────────────────────────────────────────────────
/**
* Iterates through every customer page until all records have been visited.
* Uses the `pagination.total` field to decide when to stop.
*/
async function paginateCustomers(): Promise<void> {
console.log(
"\n// ── Paginate all customers ────────────────────────────────"
);
const PAGE_SIZE = 50;
let offset = 0;
let total: number | undefined;
let page = 1;
do {
const filter: CustomerFilter = {
limit: PAGE_SIZE,
offset,
order: "desc",
};
const response: ListCustomersResponse =
await flexprice.customers.queryCustomer(filter);
total = response.pagination?.total;
const items = response.items ?? [];
console.log(
` Page ${page}: fetched ${items.length} customers` +
(total !== undefined ? ` (total=${total})` : "")
);
for (const c of items) {
console.log(` • ${c.id} ${c.email ?? "no-email"}`);
}
offset += items.length;
page++;
// Stop if we received fewer items than the page size (last page) or once
// we have consumed everything reported by the server.
} while (
(total === undefined || offset < total) &&
(response.items?.length ?? 0) === PAGE_SIZE
);
}
// ── createPlan ────────────────────────────────────────────────────────────────
async function createPlan(): Promise<Plan> {
console.log("\n// ── Create plan ──────────────────────────────────────────");
const request: CreatePlanRequest = {
name: "Pro Plan",
description: "Usage-based plan for professional teams",
lookupKey: `pro-plan-${Date.now()}`,
metadata: {
internal_code: "PLAN_PRO_V1",
},
};
const plan = await flexprice.plans.createPlan(request);
console.log(`Created plan id=${plan.id} name=${plan.name}`);
return plan;
}
// ── createSubscription ────────────────────────────────────────────────────────
async function createSubscription(
customerId: string,
planId: string
): Promise<Subscription> {
console.log(
"\n// ── Create subscription ──────────────────────────────────"
);
const request: CreateSubscriptionRequest = {
customerId,
planId,
billingCadence: "RECURRING",
billingPeriod: "MONTHLY",
currency: "USD",
collectionMethod: "charge_automatically",
startDate: new Date(),
metadata: {
source: "ts_sdk_example",
},
};
// Per-request timeout – useful for operations that trigger async work on the
// server (e.g. proration calculations) and may take longer than the default.
const subscription = await flexprice.subscriptions.createSubscription(
request,
{ timeoutMs: 15000 }
);
console.log(
`Created subscription id=${subscription.id}` +
` status=${subscription.subscriptionStatus ?? "unknown"}`
);
return subscription;
}
// ── ingestEvents ──────────────────────────────────────────────────────────────
async function ingestEvents(externalCustomerId: string): Promise<void> {
console.log(
"\n// ── Ingest single event ──────────────────────────────────"
);
// Single event ingest (returns void on HTTP 202)
const singleEvent: IngestEventRequest = {
eventName: "api_request",
externalCustomerId,
eventId: `evt-single-${Date.now()}`,
timestamp: new Date().toISOString(),
source: "ts_sdk_example",
properties: {
endpoint: "/v1/completions",
model: "gpt-4o",
tokens_used: "1500",
},
};
await flexprice.events.ingestEvent(singleEvent);
console.log("Single event accepted (202)");
// ── Bulk event ingest ───────────────────────────────────────────────────────
console.log(
"\n// ── Ingest events bulk ───────────────────────────────────"
);
const bulkPayload: BulkIngestEventRequest = {
events: [
{
eventName: "api_request",
externalCustomerId,
eventId: `evt-bulk-1-${Date.now()}`,
timestamp: new Date().toISOString(),
source: "ts_sdk_example",
properties: {
endpoint: "/v1/embeddings",
model: "text-embedding-3-small",
tokens_used: "800",
},
},
{
eventName: "storage_write",
externalCustomerId,
eventId: `evt-bulk-2-${Date.now()}`,
timestamp: new Date().toISOString(),
source: "ts_sdk_example",
properties: {
bucket: "user-uploads",
bytes_written: "204800",
},
},
{
eventName: "storage_read",
externalCustomerId,
eventId: `evt-bulk-3-${Date.now()}`,
timestamp: new Date().toISOString(),
source: "ts_sdk_example",
properties: {
bucket: "user-uploads",
bytes_read: "102400",
},
},
],
};
await flexprice.events.ingestEventsBulk(bulkPayload);
console.log(`Bulk ingest accepted (202) count=${bulkPayload.events.length}`);
}
// ── cancelSubscription ────────────────────────────────────────────────────────
async function cancelSubscription(subscriptionId: string): Promise<void> {
console.log(
"\n// ── Cancel subscription ──────────────────────────────────"
);
const request: CancelSubscriptionRequest = {
cancellationType: "end_of_period",
reason: "Customer requested cancellation via account settings",
};
const cancelled = await flexprice.subscriptions.cancelSubscription(
subscriptionId,
request
);
console.log(
`Subscription cancellation scheduled id=${cancelled.id}` +
` status=${cancelled.subscriptionStatus ?? "unknown"}`
);
}
// ── listInvoices ──────────────────────────────────────────────────────────────
async function listInvoices(customerId: string): Promise<void> {
console.log("\n// ── List invoices ────────────────────────────────────────");
const filter: InvoiceFilter = {
customerId,
invoiceStatus: ["DRAFT", "FINALIZED"],
limit: 20,
offset: 0,
order: "desc",
};
const response: ListInvoicesResponse =
await flexprice.invoices.queryInvoice(filter);
const invoices: Invoice[] = response.items ?? [];
console.log(
`Found ${invoices.length} invoice(s)` +
(response.pagination?.total !== undefined
? ` (total=${response.pagination.total})`
: "")
);
for (const inv of invoices) {
console.log(
` • ${inv.id}` +
` status=${inv.invoiceStatus ?? "unknown"}` +
` amount_due=${inv.amountDue ?? "0"} ${inv.currency ?? ""}`
);
}
}
// ── handleWebhook ─────────────────────────────────────────────────────────────
/**
* Express-style webhook handler that reads the FlexPrice event type and
* type-narrows the payload. Mount with:
*
* app.post("/webhooks/flexprice", express.raw({ type: "*\/*" }), handleWebhook);
*/
async function handleWebhook(
req: { body: Buffer | string; headers: Record<string, string | string[] | undefined> },
res: { status: (code: number) => { send: (body: string) => void } }
): Promise<void> {
const rawBody =
req.body instanceof Buffer ? req.body.toString("utf8") : req.body;
let event: Record<string, unknown>;
try {
event = JSON.parse(rawBody) as Record<string, unknown>;
} catch {
res.status(400).send("Invalid JSON");
return;
}
const eventType = event["event_type"] as string | undefined;
switch (eventType) {
case "invoice.created": {
const invoice = event["data"] as Invoice;
console.log(
`Webhook: invoice created id=${invoice?.id}` +
` amount_due=${invoice?.amountDue ?? "0"}`
);
break;
}
case "invoice.finalized": {
const invoice = event["data"] as Invoice;
console.log(
`Webhook: invoice finalised id=${invoice?.id}` +
` status=${invoice?.invoiceStatus ?? "unknown"}`
);
break;
}
case "subscription.created":
case "subscription.updated":
case "subscription.cancelled": {
const sub = event["data"] as Subscription;
console.log(
`Webhook: ${eventType} id=${sub?.id}` +
` status=${sub?.subscriptionStatus ?? "unknown"}`
);
break;
}
default:
console.log(`Webhook: unhandled event_type="${eventType ?? ""}"`);
}
res.status(200).send("OK");
}
// ── main ──────────────────────────────────────────────────────────────────────
async function main(): Promise<void> {
console.log("FlexPrice TypeScript SDK – complete example");
console.log(`Server: ${serverURL}\n`);
// 1. Customer lifecycle
const customer = await createOrGetCustomer();
await updateCustomer(customer.id);
// 2. Pagination
await paginateCustomers();
// 3. Plan
const plan = await createPlan();
// 4. Subscription (requires a plan with at least one price; this example
// assumes the plan already has prices attached via the dashboard / API)
const subscription = await createSubscription(customer.id, plan.id);
// 5. Usage events
const extId = customer.externalId ?? customer.id;
await ingestEvents(extId);
// 6. Invoices
await listInvoices(customer.id);
// 7. Cancel at end of current billing period
await cancelSubscription(subscription.id);
// 8. Webhook handler – simulated invocation (would normally be wired into
// an HTTP server; shown here to demonstrate the typing pattern)
console.log("\n// ── Webhook handler (simulated) ──────────────────────────");
await handleWebhook(
{
body: JSON.stringify({
event_type: "invoice.created",
data: { id: "inv_demo", amountDue: "49.00", currency: "USD" },
}),
headers: { "content-type": "application/json" },
},
{
status: (code: number) => ({
send: (body: string) =>
console.log(` Webhook response: ${code} ${body}`),
}),
}
);
console.log("\nAll examples completed successfully.");
}
main().catch((err: unknown) => {
console.error("Fatal error:", err);
process.exit(1);
});