@zauthx402/sdk 0.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 ADDED
@@ -0,0 +1,199 @@
1
+ <p align="center">
2
+ <img src="assets/z-small.png" alt="" width="80" height="80" />
3
+ <br />
4
+ <strong style="font-size: 24px;">@zauthx402/sdk</strong>
5
+ </p>
6
+
7
+ <p align="center">
8
+ <img src="https://img.shields.io/npm/v/@zauthx402/sdk.svg" alt="npm" />
9
+ <img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT" />
10
+ <img src="https://img.shields.io/badge/TypeScript-5.0-blue.svg" alt="TypeScript" />
11
+ <img src="https://img.shields.io/badge/x402-compatible-green.svg" alt="x402" />
12
+ </p>
13
+
14
+ <p align="center">
15
+ Monitoring, verification, and refund SDK for x402 payment endpoints.
16
+ <br />
17
+ <strong>Works with any x402 implementation</strong> - Coinbase @x402/*, custom implementations, whatever.
18
+ </p>
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install @zauthx402/sdk
24
+ ```
25
+
26
+ ## Quick Start
27
+
28
+ ```typescript
29
+ import express from 'express';
30
+ import { zauthProvider } from '@zauthx402/sdk/middleware';
31
+
32
+ const app = express();
33
+
34
+ // Add BEFORE your x402 middleware
35
+ app.use(zauthProvider('your-api-key'));
36
+
37
+ // Your existing x402 setup continues unchanged
38
+ app.use(x402Middleware(...));
39
+ app.get('/api/paid', ...);
40
+ ```
41
+
42
+ ## Features
43
+
44
+ - **Non-invasive** - Observes requests/responses without interfering with payments
45
+ - **Implementation agnostic** - Works with any x402 implementation (V1/V2)
46
+ - **Full telemetry** - Request params, response bodies, timing, payment details
47
+ - **Response validation** - Detect empty, invalid, or error responses
48
+ - **Auto-refunds** (optional) - Trigger refunds when responses are bad
49
+
50
+ ## How It Works
51
+
52
+ ```mermaid
53
+ %%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1a1a1a', 'primaryTextColor': '#fff', 'primaryBorderColor': '#333', 'lineColor': '#666', 'secondaryColor': '#2d2d2d', 'tertiaryColor': '#1a1a1a' }}}%%
54
+ flowchart LR
55
+ subgraph stack [Your Stack]
56
+ A[Request] --> B[zauthSDK]
57
+ B --> C[x402 Middleware]
58
+ C --> D[Your Routes]
59
+ D --> E[Response]
60
+ end
61
+
62
+ B -.->|telemetry| F[(zauthx402.com)]
63
+ E -.->|validation| F
64
+ F -.->|refund| B
65
+
66
+ style B fill:#0d4d2b,stroke:#10b981,color:#fff
67
+ style F fill:#0d4d2b,stroke:#10b981,color:#fff
68
+ ```
69
+
70
+ The SDK:
71
+ 1. Observes HTTP traffic (headers, bodies, status codes)
72
+ 2. Parses x402 payment headers (both V1 and V2)
73
+ 3. Validates responses for "meaningfulness"
74
+ 4. Reports telemetry to your zauthx402 dashboard
75
+ 5. Optionally triggers refunds for bad responses
76
+
77
+ ## Configuration
78
+
79
+ ```typescript
80
+ import { createZauthMiddleware } from '@zauthx402/sdk/middleware';
81
+
82
+ app.use(createZauthMiddleware({
83
+ apiKey: 'your-api-key',
84
+ mode: 'provider',
85
+
86
+ // Validation rules
87
+ validation: {
88
+ requiredFields: ['data'], // Must have these fields
89
+ errorFields: ['error', 'errors'], // These indicate errors
90
+ minResponseSize: 10, // Minimum response size
91
+ rejectEmptyCollections: true, // Reject empty arrays/objects
92
+ },
93
+
94
+ // Route filtering
95
+ includeRoutes: ['/api/.*'], // Only monitor these
96
+ excludeRoutes: ['/health'], // Skip these
97
+
98
+ // Telemetry options
99
+ telemetry: {
100
+ includeRequestBody: true,
101
+ includeResponseBody: true,
102
+ maxBodySize: 10000,
103
+ redactHeaders: ['authorization'],
104
+ redactFields: ['password', 'secret'],
105
+ sampleRate: 1.0, // 1.0 = all, 0.1 = 10%
106
+ },
107
+
108
+ // Auto-refunds (optional)
109
+ refund: {
110
+ enabled: true,
111
+ privateKey: process.env.ZAUTH_REFUND_PRIVATE_KEY,
112
+ network: 'base',
113
+ maxRefundUsdc: '1.00',
114
+ },
115
+
116
+ debug: true,
117
+ }));
118
+ ```
119
+
120
+ ## Auto-Refunds
121
+
122
+ When enabled, the SDK can automatically refund users who receive bad responses:
123
+
124
+ ```mermaid
125
+ %%{init: {'theme': 'dark', 'themeVariables': { 'actorBkg': '#1a1a1a', 'actorTextColor': '#fff', 'actorLineColor': '#666', 'signalColor': '#666', 'signalTextColor': '#fff', 'labelBoxBkgColor': '#1a1a1a', 'labelTextColor': '#fff', 'altSectionBkgColor': '#2d2d2d' }}}%%
126
+ sequenceDiagram
127
+ participant User
128
+ participant Provider
129
+ participant SDK as zauthSDK
130
+ participant Backend as zauthx402.com
131
+
132
+ User->>Provider: Request + Payment
133
+ Provider->>SDK: Capture response
134
+ SDK->>SDK: Validate response
135
+
136
+ alt Bad Response (empty/error/garbage)
137
+ SDK->>Backend: Report failure
138
+ Backend-->>SDK: Approve refund (WebSocket)
139
+ SDK->>User: Execute refund (on-chain)
140
+ SDK-->>Backend: Confirm refund
141
+ else Good Response
142
+ SDK-->>Backend: Report success
143
+ end
144
+ ```
145
+
146
+ **Requirements:**
147
+ - `viem` package installed
148
+ - Hot wallet private key configured
149
+ - Network matches endpoint network
150
+
151
+ ```bash
152
+ npm install viem
153
+ export ZAUTH_REFUND_PRIVATE_KEY=your-hot-wallet-key
154
+ ```
155
+
156
+ ## Examples
157
+
158
+ See the `/examples` directory:
159
+
160
+ - `provider-express/` - Express server with monitoring
161
+
162
+ ## Local Development
163
+
164
+ A test server is available for E2E testing (git-ignored):
165
+
166
+ ```bash
167
+ cd test-server
168
+ npm install
169
+ npm start
170
+ ```
171
+
172
+ ## API Reference
173
+
174
+ ### Exports
175
+
176
+ ```typescript
177
+ // Main
178
+ export { ZauthClient, createClient } from '@zauthx402/sdk';
179
+
180
+ // Middleware
181
+ export { createZauthMiddleware, zauthProvider } from '@zauthx402/sdk/middleware';
182
+
183
+ // Validation
184
+ export { validateResponse, createSchemaValidator } from '@zauthx402/sdk';
185
+
186
+ // Refunds
187
+ export { RefundHandler, createRefundHandler } from '@zauthx402/sdk';
188
+
189
+ // Types
190
+ export * from '@zauthx402/sdk'; // All types
191
+ ```
192
+
193
+ ## Get Your API Key
194
+
195
+ Sign up at [zauthx402.com](https://zauthx402.com) to get your API key and access the monitoring dashboard.
196
+
197
+ ## License
198
+
199
+ MIT