@b3dotfun/sdk 0.1.68-alpha.2 → 0.1.68-alpha.4

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.
Files changed (127) hide show
  1. package/dist/cjs/anyspend/platform/client.d.ts +35 -0
  2. package/dist/cjs/anyspend/platform/client.js +158 -0
  3. package/dist/cjs/anyspend/platform/errors.d.ts +38 -0
  4. package/dist/cjs/anyspend/platform/errors.js +77 -0
  5. package/dist/cjs/anyspend/platform/index.d.ts +87 -0
  6. package/dist/cjs/anyspend/platform/index.js +85 -0
  7. package/dist/cjs/anyspend/platform/resources/analytics.d.ts +7 -0
  8. package/dist/cjs/anyspend/platform/resources/analytics.js +12 -0
  9. package/dist/cjs/anyspend/platform/resources/checkout-sessions.d.ts +17 -0
  10. package/dist/cjs/anyspend/platform/resources/checkout-sessions.js +27 -0
  11. package/dist/cjs/anyspend/platform/resources/customers.d.ts +19 -0
  12. package/dist/cjs/anyspend/platform/resources/customers.js +34 -0
  13. package/dist/cjs/anyspend/platform/resources/discount-codes.d.ts +29 -0
  14. package/dist/cjs/anyspend/platform/resources/discount-codes.js +31 -0
  15. package/dist/cjs/anyspend/platform/resources/events.d.ts +14 -0
  16. package/dist/cjs/anyspend/platform/resources/events.js +16 -0
  17. package/dist/cjs/anyspend/platform/resources/notifications.d.ts +18 -0
  18. package/dist/cjs/anyspend/platform/resources/notifications.js +27 -0
  19. package/dist/cjs/anyspend/platform/resources/organization.d.ts +17 -0
  20. package/dist/cjs/anyspend/platform/resources/organization.js +15 -0
  21. package/dist/cjs/anyspend/platform/resources/payment-links.d.ts +21 -0
  22. package/dist/cjs/anyspend/platform/resources/payment-links.js +49 -0
  23. package/dist/cjs/anyspend/platform/resources/products.d.ts +27 -0
  24. package/dist/cjs/anyspend/platform/resources/products.js +31 -0
  25. package/dist/cjs/anyspend/platform/resources/transactions.d.ts +11 -0
  26. package/dist/cjs/anyspend/platform/resources/transactions.js +25 -0
  27. package/dist/cjs/anyspend/platform/resources/webhooks.d.ts +14 -0
  28. package/dist/cjs/anyspend/platform/resources/webhooks.js +33 -0
  29. package/dist/cjs/anyspend/platform/resources/widgets.d.ts +38 -0
  30. package/dist/cjs/anyspend/platform/resources/widgets.js +31 -0
  31. package/dist/cjs/anyspend/platform/types.d.ts +478 -0
  32. package/dist/cjs/anyspend/platform/types.js +5 -0
  33. package/dist/cjs/anyspend/platform/utils/idempotency.d.ts +4 -0
  34. package/dist/cjs/anyspend/platform/utils/idempotency.js +17 -0
  35. package/dist/cjs/anyspend/platform/utils/pagination.d.ts +12 -0
  36. package/dist/cjs/anyspend/platform/utils/pagination.js +22 -0
  37. package/dist/cjs/anyspend/react/components/checkout/AnySpendCheckout.d.ts +10 -6
  38. package/dist/cjs/anyspend/react/components/checkout/AnySpendCheckout.js +55 -8
  39. package/dist/cjs/anyspend/react/components/checkout/VariablePricingInput.d.ts +17 -0
  40. package/dist/cjs/anyspend/react/components/checkout/VariablePricingInput.js +145 -0
  41. package/dist/cjs/anyspend/react/components/index.d.ts +1 -1
  42. package/dist/esm/anyspend/platform/client.d.ts +35 -0
  43. package/dist/esm/anyspend/platform/client.js +153 -0
  44. package/dist/esm/anyspend/platform/errors.d.ts +38 -0
  45. package/dist/esm/anyspend/platform/errors.js +67 -0
  46. package/dist/esm/anyspend/platform/index.d.ts +87 -0
  47. package/dist/esm/anyspend/platform/index.js +75 -0
  48. package/dist/esm/anyspend/platform/resources/analytics.d.ts +7 -0
  49. package/dist/esm/anyspend/platform/resources/analytics.js +8 -0
  50. package/dist/esm/anyspend/platform/resources/checkout-sessions.d.ts +17 -0
  51. package/dist/esm/anyspend/platform/resources/checkout-sessions.js +23 -0
  52. package/dist/esm/anyspend/platform/resources/customers.d.ts +19 -0
  53. package/dist/esm/anyspend/platform/resources/customers.js +30 -0
  54. package/dist/esm/anyspend/platform/resources/discount-codes.d.ts +29 -0
  55. package/dist/esm/anyspend/platform/resources/discount-codes.js +27 -0
  56. package/dist/esm/anyspend/platform/resources/events.d.ts +14 -0
  57. package/dist/esm/anyspend/platform/resources/events.js +12 -0
  58. package/dist/esm/anyspend/platform/resources/notifications.d.ts +18 -0
  59. package/dist/esm/anyspend/platform/resources/notifications.js +23 -0
  60. package/dist/esm/anyspend/platform/resources/organization.d.ts +17 -0
  61. package/dist/esm/anyspend/platform/resources/organization.js +11 -0
  62. package/dist/esm/anyspend/platform/resources/payment-links.d.ts +21 -0
  63. package/dist/esm/anyspend/platform/resources/payment-links.js +45 -0
  64. package/dist/esm/anyspend/platform/resources/products.d.ts +27 -0
  65. package/dist/esm/anyspend/platform/resources/products.js +27 -0
  66. package/dist/esm/anyspend/platform/resources/transactions.d.ts +11 -0
  67. package/dist/esm/anyspend/platform/resources/transactions.js +21 -0
  68. package/dist/esm/anyspend/platform/resources/webhooks.d.ts +14 -0
  69. package/dist/esm/anyspend/platform/resources/webhooks.js +29 -0
  70. package/dist/esm/anyspend/platform/resources/widgets.d.ts +38 -0
  71. package/dist/esm/anyspend/platform/resources/widgets.js +27 -0
  72. package/dist/esm/anyspend/platform/types.d.ts +478 -0
  73. package/dist/esm/anyspend/platform/types.js +4 -0
  74. package/dist/esm/anyspend/platform/utils/idempotency.d.ts +4 -0
  75. package/dist/esm/anyspend/platform/utils/idempotency.js +14 -0
  76. package/dist/esm/anyspend/platform/utils/pagination.d.ts +12 -0
  77. package/dist/esm/anyspend/platform/utils/pagination.js +19 -0
  78. package/dist/esm/anyspend/react/components/checkout/AnySpendCheckout.d.ts +10 -6
  79. package/dist/esm/anyspend/react/components/checkout/AnySpendCheckout.js +55 -8
  80. package/dist/esm/anyspend/react/components/checkout/VariablePricingInput.d.ts +17 -0
  81. package/dist/esm/anyspend/react/components/checkout/VariablePricingInput.js +142 -0
  82. package/dist/esm/anyspend/react/components/index.d.ts +1 -1
  83. package/dist/styles/index.css +1 -1
  84. package/dist/types/anyspend/platform/client.d.ts +35 -0
  85. package/dist/types/anyspend/platform/errors.d.ts +38 -0
  86. package/dist/types/anyspend/platform/index.d.ts +87 -0
  87. package/dist/types/anyspend/platform/resources/analytics.d.ts +7 -0
  88. package/dist/types/anyspend/platform/resources/checkout-sessions.d.ts +17 -0
  89. package/dist/types/anyspend/platform/resources/customers.d.ts +19 -0
  90. package/dist/types/anyspend/platform/resources/discount-codes.d.ts +29 -0
  91. package/dist/types/anyspend/platform/resources/events.d.ts +14 -0
  92. package/dist/types/anyspend/platform/resources/notifications.d.ts +18 -0
  93. package/dist/types/anyspend/platform/resources/organization.d.ts +17 -0
  94. package/dist/types/anyspend/platform/resources/payment-links.d.ts +21 -0
  95. package/dist/types/anyspend/platform/resources/products.d.ts +27 -0
  96. package/dist/types/anyspend/platform/resources/transactions.d.ts +11 -0
  97. package/dist/types/anyspend/platform/resources/webhooks.d.ts +14 -0
  98. package/dist/types/anyspend/platform/resources/widgets.d.ts +38 -0
  99. package/dist/types/anyspend/platform/types.d.ts +478 -0
  100. package/dist/types/anyspend/platform/utils/idempotency.d.ts +4 -0
  101. package/dist/types/anyspend/platform/utils/pagination.d.ts +12 -0
  102. package/dist/types/anyspend/react/components/checkout/AnySpendCheckout.d.ts +10 -6
  103. package/dist/types/anyspend/react/components/checkout/VariablePricingInput.d.ts +17 -0
  104. package/dist/types/anyspend/react/components/index.d.ts +1 -1
  105. package/package.json +6 -1
  106. package/src/anyspend/docs/checkout-sessions.md +20 -3
  107. package/src/anyspend/platform/client.ts +198 -0
  108. package/src/anyspend/platform/errors.ts +92 -0
  109. package/src/anyspend/platform/index.ts +129 -0
  110. package/src/anyspend/platform/resources/analytics.ts +10 -0
  111. package/src/anyspend/platform/resources/checkout-sessions.ts +36 -0
  112. package/src/anyspend/platform/resources/customers.ts +54 -0
  113. package/src/anyspend/platform/resources/discount-codes.ts +63 -0
  114. package/src/anyspend/platform/resources/events.ts +22 -0
  115. package/src/anyspend/platform/resources/notifications.ts +37 -0
  116. package/src/anyspend/platform/resources/organization.ts +24 -0
  117. package/src/anyspend/platform/resources/payment-links.ts +74 -0
  118. package/src/anyspend/platform/resources/products.ts +59 -0
  119. package/src/anyspend/platform/resources/transactions.ts +33 -0
  120. package/src/anyspend/platform/resources/webhooks.ts +47 -0
  121. package/src/anyspend/platform/resources/widgets.ts +63 -0
  122. package/src/anyspend/platform/types.ts +532 -0
  123. package/src/anyspend/platform/utils/idempotency.ts +15 -0
  124. package/src/anyspend/platform/utils/pagination.ts +32 -0
  125. package/src/anyspend/react/components/checkout/AnySpendCheckout.tsx +73 -18
  126. package/src/anyspend/react/components/checkout/VariablePricingInput.tsx +247 -0
  127. package/src/anyspend/react/components/index.ts +1 -0
@@ -0,0 +1,198 @@
1
+ /**
2
+ * Core HTTP client for the AnySpend Platform API.
3
+ * Handles authentication, retries, error parsing, and idempotency.
4
+ */
5
+
6
+ import { parseApiError, type ApiErrorResponse } from "./errors";
7
+ import { generateIdempotencyKey } from "./utils/idempotency";
8
+
9
+ export interface ClientConfig {
10
+ baseUrl?: string;
11
+ timeout?: number;
12
+ maxRetries?: number;
13
+ idempotencyKeyGenerator?: () => string;
14
+ }
15
+
16
+ export const DEFAULT_BASE_URL = "https://platform-api.anyspend.com/api/v1";
17
+ const DEFAULT_TIMEOUT = 30000;
18
+ const DEFAULT_MAX_RETRIES = 3;
19
+
20
+ export class HttpClient {
21
+ private apiKey: string;
22
+ private baseUrl: string;
23
+ private timeout: number;
24
+ private maxRetries: number;
25
+ private generateIdempotencyKey: () => string;
26
+
27
+ constructor(apiKey: string, config: ClientConfig = {}) {
28
+ this.apiKey = apiKey;
29
+ this.baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
30
+ this.timeout = config.timeout || DEFAULT_TIMEOUT;
31
+ this.maxRetries = config.maxRetries || DEFAULT_MAX_RETRIES;
32
+ this.generateIdempotencyKey = config.idempotencyKeyGenerator || generateIdempotencyKey;
33
+ }
34
+
35
+ async get<T>(path: string, params?: object): Promise<T> {
36
+ const url = this.buildUrl(path, params);
37
+ return this.request<T>("GET", url);
38
+ }
39
+
40
+ async post<T>(path: string, body?: object): Promise<T> {
41
+ const url = this.buildUrl(path);
42
+ return this.request<T>("POST", url, body);
43
+ }
44
+
45
+ async patch<T>(path: string, body: object): Promise<T> {
46
+ const url = this.buildUrl(path);
47
+ return this.request<T>("PATCH", url, body);
48
+ }
49
+
50
+ async delete<T>(path: string, body?: object): Promise<T> {
51
+ const url = this.buildUrl(path);
52
+ return this.request<T>("DELETE", url, body);
53
+ }
54
+
55
+ async postFormData<T>(path: string, formData: FormData): Promise<T> {
56
+ const url = this.buildUrl(path);
57
+ return this.requestRaw<T>("POST", url, formData);
58
+ }
59
+
60
+ private buildUrl(path: string, params?: object): string {
61
+ const url = new URL(`${this.baseUrl}${path}`);
62
+ if (params) {
63
+ for (const [key, value] of Object.entries(params as Record<string, unknown>)) {
64
+ if (value !== undefined && value !== null) url.searchParams.set(key, String(value));
65
+ }
66
+ }
67
+ return url.toString();
68
+ }
69
+
70
+ private async request<T>(method: string, url: string, body?: object): Promise<T> {
71
+ const headers: Record<string, string> = {
72
+ Authorization: `Bearer ${this.apiKey}`,
73
+ "Content-Type": "application/json",
74
+ };
75
+
76
+ // Auto-generate idempotency key for POST/PATCH
77
+ if (method === "POST" || method === "PATCH") {
78
+ headers["Idempotency-Key"] = this.generateIdempotencyKey();
79
+ }
80
+
81
+ return this.executeWithRetry<T>(method, url, headers, body ? JSON.stringify(body) : undefined);
82
+ }
83
+
84
+ private async requestRaw<T>(method: string, url: string, body: FormData): Promise<T> {
85
+ const headers: Record<string, string> = {
86
+ Authorization: `Bearer ${this.apiKey}`,
87
+ };
88
+
89
+ return this.executeWithRetry<T>(method, url, headers, body);
90
+ }
91
+
92
+ private async executeWithRetry<T>(
93
+ method: string,
94
+ url: string,
95
+ headers: Record<string, string>,
96
+ body?: string | FormData,
97
+ ): Promise<T> {
98
+ let lastError: Error | undefined;
99
+
100
+ for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
101
+ try {
102
+ const controller = new AbortController();
103
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
104
+
105
+ const response = await fetch(url, {
106
+ method,
107
+ headers,
108
+ body,
109
+ signal: controller.signal,
110
+ });
111
+
112
+ clearTimeout(timeoutId);
113
+
114
+ if (response.ok) {
115
+ // Handle CSV/text responses
116
+ const contentType = response.headers.get("Content-Type") || "";
117
+ if (contentType.includes("text/csv")) {
118
+ return (await response.text()) as unknown as T;
119
+ }
120
+ return (await response.json()) as T;
121
+ }
122
+
123
+ // Parse error response
124
+ const errorBody = (await response.json().catch(() => ({
125
+ error: { type: "api_error", code: "internal_error", message: "Unknown error" },
126
+ }))) as ApiErrorResponse;
127
+
128
+ const error = parseApiError(response.status, errorBody, response.headers);
129
+
130
+ // Don't retry 4xx errors (except 429 rate limits)
131
+ if (response.status < 500 && response.status !== 429) {
132
+ throw error;
133
+ }
134
+
135
+ // For 429, wait the specified retry-after time
136
+ if (response.status === 429) {
137
+ const retryAfter = parseInt(response.headers.get("Retry-After") || "1", 10);
138
+ await this.sleep(retryAfter * 1000);
139
+ lastError = error;
140
+ continue;
141
+ }
142
+
143
+ // For 5xx, retry with exponential backoff
144
+ lastError = error;
145
+ if (attempt < this.maxRetries) {
146
+ await this.sleep(Math.pow(2, attempt) * 1000);
147
+ }
148
+ } catch (err) {
149
+ if (err instanceof Error && err.name === "AbortError") {
150
+ lastError = new Error(`Request timed out after ${this.timeout}ms`);
151
+ } else if (err instanceof Error && "type" in err) {
152
+ // Already a parsed ApiError, re-throw
153
+ throw err;
154
+ } else {
155
+ lastError = err instanceof Error ? err : new Error(String(err));
156
+ }
157
+
158
+ if (attempt < this.maxRetries) {
159
+ await this.sleep(Math.pow(2, attempt) * 1000);
160
+ }
161
+ }
162
+ }
163
+
164
+ throw lastError || new Error("Request failed after retries");
165
+ }
166
+
167
+ private sleep(ms: number): Promise<void> {
168
+ return new Promise(resolve => setTimeout(resolve, ms));
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Static HTTP client for unauthenticated requests (quick-pay).
174
+ */
175
+ export class StaticHttpClient {
176
+ static async post<T>(baseUrl: string, path: string, body: Record<string, unknown>): Promise<T> {
177
+ const url = `${baseUrl.replace(/\/$/, "")}${path}`;
178
+ const controller = new AbortController();
179
+ const timeoutId = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT);
180
+ const response = await fetch(url, {
181
+ method: "POST",
182
+ headers: { "Content-Type": "application/json" },
183
+ body: JSON.stringify(body),
184
+ signal: controller.signal,
185
+ });
186
+ clearTimeout(timeoutId);
187
+
188
+ if (response.ok) {
189
+ return (await response.json()) as T;
190
+ }
191
+
192
+ const errorBody = (await response.json().catch(() => ({
193
+ error: { type: "api_error", code: "internal_error", message: "Unknown error" },
194
+ }))) as ApiErrorResponse;
195
+
196
+ throw parseApiError(response.status, errorBody, response.headers);
197
+ }
198
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Typed error classes for the AnySpend Platform API client.
3
+ */
4
+
5
+ export interface ApiErrorResponse {
6
+ error: {
7
+ type: string;
8
+ code: string;
9
+ message: string;
10
+ param?: string;
11
+ };
12
+ }
13
+
14
+ export class ApiError extends Error {
15
+ readonly type: string;
16
+ readonly code: string;
17
+ readonly status: number;
18
+ readonly param?: string;
19
+
20
+ constructor(status: number, body: ApiErrorResponse) {
21
+ super(body.error.message);
22
+ this.name = "ApiError";
23
+ this.type = body.error.type;
24
+ this.code = body.error.code;
25
+ this.status = status;
26
+ this.param = body.error.param;
27
+ }
28
+ }
29
+
30
+ export class AuthenticationError extends ApiError {
31
+ constructor(status: number, body: ApiErrorResponse) {
32
+ super(status, body);
33
+ this.name = "AuthenticationError";
34
+ }
35
+ }
36
+
37
+ export class PermissionError extends ApiError {
38
+ constructor(status: number, body: ApiErrorResponse) {
39
+ super(status, body);
40
+ this.name = "PermissionError";
41
+ }
42
+ }
43
+
44
+ export class RateLimitError extends ApiError {
45
+ readonly retryAfter: number;
46
+
47
+ constructor(status: number, body: ApiErrorResponse, retryAfter: number) {
48
+ super(status, body);
49
+ this.name = "RateLimitError";
50
+ this.retryAfter = retryAfter;
51
+ }
52
+ }
53
+
54
+ export class NotFoundError extends ApiError {
55
+ constructor(status: number, body: ApiErrorResponse) {
56
+ super(status, body);
57
+ this.name = "NotFoundError";
58
+ }
59
+ }
60
+
61
+ export class IdempotencyError extends ApiError {
62
+ constructor(status: number, body: ApiErrorResponse) {
63
+ super(status, body);
64
+ this.name = "IdempotencyError";
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Parse an API error response into the appropriate typed error class.
70
+ */
71
+ export function parseApiError(status: number, body: ApiErrorResponse, headers?: Headers): ApiError {
72
+ const type = body.error?.type;
73
+
74
+ if (status === 401 || type === "authentication_error") {
75
+ return new AuthenticationError(status, body);
76
+ }
77
+ if (status === 403 || type === "permission_error") {
78
+ return new PermissionError(status, body);
79
+ }
80
+ if (status === 429 || type === "rate_limit_error") {
81
+ const retryAfter = parseInt(headers?.get("Retry-After") || "60", 10);
82
+ return new RateLimitError(status, body, retryAfter);
83
+ }
84
+ if (status === 404 || type === "not_found_error") {
85
+ return new NotFoundError(status, body);
86
+ }
87
+ if (type === "idempotency_error") {
88
+ return new IdempotencyError(status, body);
89
+ }
90
+
91
+ return new ApiError(status, body);
92
+ }
@@ -0,0 +1,129 @@
1
+ /**
2
+ * AnySpend Platform API Client
3
+ *
4
+ * A headless TypeScript client for the AnySpend Platform REST API.
5
+ * Works in any runtime with `fetch` (Node.js, browsers, Cloudflare Workers, Deno, Bun).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { AnySpendPlatformClient } from '@b3dotfun/sdk/anyspend/platform';
10
+ *
11
+ * const platform = new AnySpendPlatformClient('asp_your_api_key_here');
12
+ *
13
+ * // Create a payment link
14
+ * const link = await platform.paymentLinks.create({
15
+ * name: 'Summer Sale',
16
+ * amount: '10000000',
17
+ * token_address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
18
+ * chain_id: 8453,
19
+ * recipient_address: '0x...',
20
+ * });
21
+ *
22
+ * // Quick pay (no API key needed)
23
+ * const quickLink = await AnySpendPlatformClient.quickPay({
24
+ * recipient_address: '0x...',
25
+ * amount: '5000000',
26
+ * });
27
+ * ```
28
+ */
29
+
30
+ import { HttpClient, StaticHttpClient, DEFAULT_BASE_URL, type ClientConfig } from "./client";
31
+ import { PaymentLinksResource } from "./resources/payment-links";
32
+ import { ProductsResource } from "./resources/products";
33
+ import { CustomersResource } from "./resources/customers";
34
+ import { TransactionsResource } from "./resources/transactions";
35
+ import { CheckoutSessionsResource } from "./resources/checkout-sessions";
36
+ import { WebhooksResource } from "./resources/webhooks";
37
+ import { DiscountCodesResource } from "./resources/discount-codes";
38
+ import { NotificationsResource } from "./resources/notifications";
39
+ import { WidgetsResource } from "./resources/widgets";
40
+ import { OrganizationResource } from "./resources/organization";
41
+ import { AnalyticsResource } from "./resources/analytics";
42
+ import { EventsResource } from "./resources/events";
43
+ import type { QuickPayParams, PaymentLink } from "./types";
44
+
45
+ export class AnySpendPlatformClient {
46
+ private client: HttpClient;
47
+
48
+ /** Payment Links - Create, manage, and track payment links */
49
+ readonly paymentLinks: PaymentLinksResource;
50
+
51
+ /** Products - Manage your product catalog */
52
+ readonly products: ProductsResource;
53
+
54
+ /** Customers - Manage customer records */
55
+ readonly customers: CustomersResource;
56
+
57
+ /** Transactions - View transaction history and stats */
58
+ readonly transactions: TransactionsResource;
59
+
60
+ /** Checkout Sessions - Server-side checkout management */
61
+ readonly checkoutSessions: CheckoutSessionsResource;
62
+
63
+ /** Webhooks - Configure webhook endpoints */
64
+ readonly webhooks: WebhooksResource;
65
+
66
+ /** Discount Codes - Create and validate discounts */
67
+ readonly discountCodes: DiscountCodesResource;
68
+
69
+ /** Notifications - Email and Telegram settings */
70
+ readonly notifications: NotificationsResource;
71
+
72
+ /** Widgets - Embeddable widget configurations */
73
+ readonly widgets: WidgetsResource;
74
+
75
+ /** Organization - Org settings and defaults */
76
+ readonly organization: OrganizationResource;
77
+
78
+ /** Analytics - Revenue and conversion analytics */
79
+ readonly analytics: AnalyticsResource;
80
+
81
+ /** Events - API audit trail */
82
+ readonly events: EventsResource;
83
+
84
+ /**
85
+ * Create a new AnySpend Platform API client.
86
+ *
87
+ * @param apiKey - Your API key (starts with `asp_`)
88
+ * @param config - Optional configuration
89
+ */
90
+ constructor(apiKey: string, config?: ClientConfig) {
91
+ this.client = new HttpClient(apiKey, config);
92
+
93
+ this.paymentLinks = new PaymentLinksResource(this.client);
94
+ this.products = new ProductsResource(this.client);
95
+ this.customers = new CustomersResource(this.client);
96
+ this.transactions = new TransactionsResource(this.client);
97
+ this.checkoutSessions = new CheckoutSessionsResource(this.client);
98
+ this.webhooks = new WebhooksResource(this.client);
99
+ this.discountCodes = new DiscountCodesResource(this.client);
100
+ this.notifications = new NotificationsResource(this.client);
101
+ this.widgets = new WidgetsResource(this.client);
102
+ this.organization = new OrganizationResource(this.client);
103
+ this.analytics = new AnalyticsResource(this.client);
104
+ this.events = new EventsResource(this.client);
105
+ }
106
+
107
+ /**
108
+ * Create a quick payment link without authentication.
109
+ * Rate limited to 5 requests per minute per IP.
110
+ *
111
+ * @param params - Quick pay parameters
112
+ * @param baseUrl - Optional API base URL (defaults to production)
113
+ */
114
+ static async quickPay(params: QuickPayParams, baseUrl: string = DEFAULT_BASE_URL): Promise<PaymentLink> {
115
+ return StaticHttpClient.post<PaymentLink>(baseUrl, "/quick-pay", params as unknown as Record<string, unknown>);
116
+ }
117
+ }
118
+
119
+ // Re-export types and errors
120
+ export type { ClientConfig } from "./client";
121
+ export {
122
+ ApiError,
123
+ AuthenticationError,
124
+ PermissionError,
125
+ RateLimitError,
126
+ NotFoundError,
127
+ IdempotencyError,
128
+ } from "./errors";
129
+ export type * from "./types";
@@ -0,0 +1,10 @@
1
+ import type { HttpClient } from "../client";
2
+ import type { AnalyticsOverview } from "../types";
3
+
4
+ export class AnalyticsResource {
5
+ constructor(private client: HttpClient) {}
6
+
7
+ async overview(period?: "7d" | "30d" | "90d" | "all"): Promise<AnalyticsOverview> {
8
+ return this.client.get<AnalyticsOverview>("/analytics/overview", period !== undefined ? { period } : undefined);
9
+ }
10
+ }
@@ -0,0 +1,36 @@
1
+ import type { HttpClient } from "../client";
2
+ import { autoPaginate } from "../utils/pagination";
3
+ import type { CheckoutSession, ListResponse, CreateCheckoutSessionParams, PaginationParams } from "../types";
4
+
5
+ export interface ListCheckoutSessionsParams extends PaginationParams {
6
+ status?: "open" | "processing" | "completed" | "expired" | "failed";
7
+ payment_link_id?: string;
8
+ from?: number;
9
+ to?: number;
10
+ }
11
+
12
+ export class CheckoutSessionsResource {
13
+ constructor(private client: HttpClient) {}
14
+
15
+ async list(params?: ListCheckoutSessionsParams): Promise<ListResponse<CheckoutSession>> {
16
+ return this.client.get<ListResponse<CheckoutSession>>("/checkout-sessions", params);
17
+ }
18
+
19
+ async *listAutoPaginate(params?: Omit<ListCheckoutSessionsParams, "page">): AsyncGenerator<CheckoutSession> {
20
+ yield* autoPaginate<CheckoutSession>((page, limit) => this.list({ ...params, page, limit }), {
21
+ limit: params?.limit,
22
+ });
23
+ }
24
+
25
+ async create(params: CreateCheckoutSessionParams): Promise<CheckoutSession> {
26
+ return this.client.post<CheckoutSession>("/checkout-sessions", params);
27
+ }
28
+
29
+ async get(id: string): Promise<CheckoutSession> {
30
+ return this.client.get<CheckoutSession>(`/checkout-sessions/${id}`);
31
+ }
32
+
33
+ async expire(id: string): Promise<CheckoutSession> {
34
+ return this.client.post<CheckoutSession>(`/checkout-sessions/${id}/expire`);
35
+ }
36
+ }
@@ -0,0 +1,54 @@
1
+ import type { HttpClient } from "../client";
2
+ import { autoPaginate } from "../utils/pagination";
3
+ import type {
4
+ Customer,
5
+ Transaction,
6
+ ListResponse,
7
+ DeletedResponse,
8
+ CreateCustomerParams,
9
+ UpdateCustomerParams,
10
+ ExportCustomersParams,
11
+ PaginationParams,
12
+ } from "../types";
13
+
14
+ export interface ListCustomersParams extends PaginationParams {
15
+ search?: string;
16
+ sort?: "created_at" | "updated_at" | "total_paid" | "transaction_count";
17
+ order?: "asc" | "desc";
18
+ }
19
+
20
+ export class CustomersResource {
21
+ constructor(private client: HttpClient) {}
22
+
23
+ async list(params?: ListCustomersParams): Promise<ListResponse<Customer>> {
24
+ return this.client.get<ListResponse<Customer>>("/customers", params);
25
+ }
26
+
27
+ async *listAutoPaginate(params?: Omit<ListCustomersParams, "page">): AsyncGenerator<Customer> {
28
+ yield* autoPaginate<Customer>((page, limit) => this.list({ ...params, page, limit }), { limit: params?.limit });
29
+ }
30
+
31
+ async create(params: CreateCustomerParams): Promise<Customer> {
32
+ return this.client.post<Customer>("/customers", params);
33
+ }
34
+
35
+ async get(id: string): Promise<Customer> {
36
+ return this.client.get<Customer>(`/customers/${id}`);
37
+ }
38
+
39
+ async update(id: string, params: UpdateCustomerParams): Promise<Customer> {
40
+ return this.client.patch<Customer>(`/customers/${id}`, params);
41
+ }
42
+
43
+ async delete(id: string): Promise<DeletedResponse> {
44
+ return this.client.delete<DeletedResponse>(`/customers/${id}`);
45
+ }
46
+
47
+ async transactions(id: string, params?: PaginationParams): Promise<ListResponse<Transaction>> {
48
+ return this.client.get<ListResponse<Transaction>>(`/customers/${id}/transactions`, params);
49
+ }
50
+
51
+ async export(params?: ExportCustomersParams): Promise<string> {
52
+ return this.client.get<string>("/customers/export", params);
53
+ }
54
+ }
@@ -0,0 +1,63 @@
1
+ import type { HttpClient } from "../client";
2
+ import { autoPaginate } from "../utils/pagination";
3
+ import type {
4
+ DiscountCode,
5
+ ListResponse,
6
+ DeletedResponse,
7
+ CreateDiscountCodeParams,
8
+ BatchCreateDiscountCodesParams,
9
+ ValidateDiscountResult,
10
+ BatchCreateResult,
11
+ PaginationParams,
12
+ } from "../types";
13
+
14
+ export interface ListDiscountCodesParams extends PaginationParams {
15
+ payment_link_id?: string;
16
+ active?: "true" | "false";
17
+ search?: string;
18
+ }
19
+
20
+ export interface UpdateDiscountCodeParams {
21
+ is_active?: boolean;
22
+ max_uses?: number;
23
+ min_order_amount?: string;
24
+ expires_at?: number;
25
+ }
26
+
27
+ export interface ValidateDiscountParams {
28
+ code: string;
29
+ payment_link_id?: string;
30
+ amount?: string;
31
+ }
32
+
33
+ export class DiscountCodesResource {
34
+ constructor(private client: HttpClient) {}
35
+
36
+ async list(params?: ListDiscountCodesParams): Promise<ListResponse<DiscountCode>> {
37
+ return this.client.get<ListResponse<DiscountCode>>("/discount-codes", params);
38
+ }
39
+
40
+ async *listAutoPaginate(params?: Omit<ListDiscountCodesParams, "page">): AsyncGenerator<DiscountCode> {
41
+ yield* autoPaginate<DiscountCode>((page, limit) => this.list({ ...params, page, limit }), { limit: params?.limit });
42
+ }
43
+
44
+ async create(params: CreateDiscountCodeParams): Promise<DiscountCode> {
45
+ return this.client.post<DiscountCode>("/discount-codes", params);
46
+ }
47
+
48
+ async update(id: string, params: UpdateDiscountCodeParams): Promise<DiscountCode> {
49
+ return this.client.patch<DiscountCode>(`/discount-codes/${id}`, params);
50
+ }
51
+
52
+ async delete(id: string): Promise<DeletedResponse> {
53
+ return this.client.delete<DeletedResponse>(`/discount-codes/${id}`);
54
+ }
55
+
56
+ async validate(params: ValidateDiscountParams): Promise<ValidateDiscountResult> {
57
+ return this.client.post<ValidateDiscountResult>("/discount-codes/validate", params);
58
+ }
59
+
60
+ async batchCreate(params: BatchCreateDiscountCodesParams): Promise<BatchCreateResult> {
61
+ return this.client.post<BatchCreateResult>("/discount-codes/batch", params);
62
+ }
63
+ }
@@ -0,0 +1,22 @@
1
+ import type { HttpClient } from "../client";
2
+ import { autoPaginate } from "../utils/pagination";
3
+ import type { ApiEvent, ListResponse, PaginationParams } from "../types";
4
+
5
+ export interface ListEventsParams extends PaginationParams {
6
+ event_type?: string;
7
+ resource_type?: string;
8
+ from?: number;
9
+ to?: number;
10
+ }
11
+
12
+ export class EventsResource {
13
+ constructor(private client: HttpClient) {}
14
+
15
+ async list(params?: ListEventsParams): Promise<ListResponse<ApiEvent>> {
16
+ return this.client.get<ListResponse<ApiEvent>>("/events", params);
17
+ }
18
+
19
+ async *listAutoPaginate(params?: Omit<ListEventsParams, "page">): AsyncGenerator<ApiEvent> {
20
+ yield* autoPaginate<ApiEvent>((page, limit) => this.list({ ...params, page, limit }), { limit: params?.limit });
21
+ }
22
+ }
@@ -0,0 +1,37 @@
1
+ import type { HttpClient } from "../client";
2
+ import type { NotificationSettings, UpdateNotificationSettingsParams, ActionResponse } from "../types";
3
+
4
+ export interface TelegramLinkResult {
5
+ object: "telegram_link";
6
+ link_url: string;
7
+ link_code: string;
8
+ expires_at: number;
9
+ }
10
+
11
+ export class NotificationsResource {
12
+ constructor(private client: HttpClient) {}
13
+
14
+ async get(): Promise<NotificationSettings> {
15
+ return this.client.get<NotificationSettings>("/notifications");
16
+ }
17
+
18
+ async update(params: UpdateNotificationSettingsParams): Promise<NotificationSettings> {
19
+ return this.client.patch<NotificationSettings>("/notifications", params);
20
+ }
21
+
22
+ async linkTelegram(): Promise<TelegramLinkResult> {
23
+ return this.client.post<TelegramLinkResult>("/notifications/telegram/link");
24
+ }
25
+
26
+ async unlinkTelegram(): Promise<ActionResponse> {
27
+ return this.client.post<ActionResponse>("/notifications/telegram/unlink");
28
+ }
29
+
30
+ async testEmail(): Promise<ActionResponse> {
31
+ return this.client.post<ActionResponse>("/notifications/test/email");
32
+ }
33
+
34
+ async testTelegram(): Promise<ActionResponse> {
35
+ return this.client.post<ActionResponse>("/notifications/test/telegram");
36
+ }
37
+ }
@@ -0,0 +1,24 @@
1
+ import type { HttpClient } from "../client";
2
+ import type { Organization } from "../types";
3
+
4
+ export interface UpdateOrganizationParams {
5
+ name?: string;
6
+ description?: string;
7
+ logo_url?: string;
8
+ website_url?: string;
9
+ default_recipient_address?: string;
10
+ default_chain_id?: number;
11
+ default_token_address?: string;
12
+ }
13
+
14
+ export class OrganizationResource {
15
+ constructor(private client: HttpClient) {}
16
+
17
+ async get(): Promise<Organization> {
18
+ return this.client.get<Organization>("/organization");
19
+ }
20
+
21
+ async update(params: UpdateOrganizationParams): Promise<Organization> {
22
+ return this.client.patch<Organization>("/organization", params);
23
+ }
24
+ }