@valiron/sdk 1.0.2 → 1.1.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.
@@ -0,0 +1,417 @@
1
+ # API Scaffolding
2
+
3
+ API Scaffolding is a separate Valiron product for exposing existing HTTPS APIs through hosted, agent-facing URLs. It can add x402 or MPP payment handling, optional Valiron trust checks, rate limits, upstream forwarding, and usage logs without requiring immediate SDK middleware integration.
4
+
5
+ Use this flow when Valiron should host the payment and optional trust layer for an existing HTTPS API:
6
+
7
+ ```text
8
+ Agent or client
9
+ -> Valiron-hosted wrapper URL
10
+ -> x402 or MPP payment check
11
+ -> optional Valiron trust check
12
+ -> provider's existing API
13
+ -> response returned through Valiron
14
+ ```
15
+
16
+ The wrapper is intentionally narrow: Valiron forwards valid HTTP requests to an existing upstream API. It does not need direct database access, internal service access, or provider infrastructure credentials beyond the optional upstream auth secret needed to call the API.
17
+
18
+ ## When To Use This
19
+
20
+ Use API Scaffolding when:
21
+
22
+ - You already have an HTTPS API and want to sell per-call access to agents.
23
+ - You want x402 or MPP payment challenges without implementing those protocols yourself.
24
+ - You want Valiron trust checks before paid traffic reaches your API.
25
+ - You need a no-code or low-code path from an existing API to an agent-compatible endpoint.
26
+
27
+ Use the SDK middleware instead when:
28
+
29
+ - You want payments to be handled entirely by your own server.
30
+ - You need custom payment terms, subscriptions, account billing, or business-specific authorization.
31
+ - Valiron should run directly in the API backend as the primary trust and policy enforcement layer.
32
+
33
+ ## Provider Setup
34
+
35
+ Providers configure one wrapper per public endpoint.
36
+
37
+ | Field | Required | Description |
38
+ |-------|----------|-------------|
39
+ | Public path | Yes | Path callers use on Valiron, for example `/weather`. |
40
+ | HTTP method | Yes | `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, or `HEAD`. |
41
+ | Upstream base URL | Yes | Existing HTTPS API base URL, for example `https://api.example.com`. |
42
+ | Price per call | Yes | USD amount charged for paid requests. Use `0` for a free wrapper. |
43
+ | Payment protocol | Yes | `x402` or `MPP`. |
44
+ | Payment network | Yes | x402: `eip155:8453` or `eip155:84532`; MPP: `tempo:4217` or `tempo:42431`. |
45
+ | Payout wallet | Paid only | EVM address receiving payment. |
46
+ | Upstream auth | No | Secret Valiron injects when forwarding to your upstream. |
47
+ | Rate limit | No | Max requests per minute per client. |
48
+ | Trust check | No | Whether callers must pass Valiron agent trust evaluation before forwarding. |
49
+
50
+ Example existing API:
51
+
52
+ ```text
53
+ GET https://api.example.com/weather
54
+ ```
55
+
56
+ Example Valiron URL:
57
+
58
+ ```text
59
+ GET https://valiron-edge-proxy.onrender.com/wrap/{operatorId}/weather
60
+ ```
61
+
62
+ Valiron preserves query strings and appends the public wrapper path to the upstream base URL. If your upstream base URL is `https://api.example.com/v1` and your public path is `/weather`, Valiron forwards to:
63
+
64
+ ```text
65
+ https://api.example.com/v1/weather
66
+ ```
67
+
68
+ ## Dashboard Flow
69
+
70
+ 1. Open [valiron.co/dashboard](https://www.valiron.co/dashboard).
71
+ 2. Go to **Make API Agent-Ready**.
72
+ 3. Click **Make API Agent-Ready**.
73
+ 4. Enter the upstream URL, public path, method, price, protocol, network, and payout wallet.
74
+ 5. Choose whether Valiron trust checks are required.
75
+ 6. Configure upstream auth only if your API needs it.
76
+ 7. Create the wrapper.
77
+ 8. Distribute the generated Valiron URL to agent clients or API customers.
78
+
79
+ ## HTTP API Flow
80
+
81
+ The dashboard calls the same operator API you can call directly.
82
+
83
+ ### Register
84
+
85
+ ```bash
86
+ curl -X POST https://valiron-edge-proxy.onrender.com/operator/register \
87
+ -H "content-type: application/json" \
88
+ -d '{
89
+ "email": "you@example.com",
90
+ "password": "a-long-password",
91
+ "name": "Example Operator"
92
+ }'
93
+ ```
94
+
95
+ ### Login
96
+
97
+ ```bash
98
+ curl -X POST https://valiron-edge-proxy.onrender.com/operator/login \
99
+ -H "content-type: application/json" \
100
+ -d '{
101
+ "email": "you@example.com",
102
+ "password": "a-long-password"
103
+ }'
104
+ ```
105
+
106
+ ### Create A Free Wrapper
107
+
108
+ ```bash
109
+ curl -X POST https://valiron-edge-proxy.onrender.com/operator/endpoints \
110
+ -H "authorization: Bearer $VALIRON_JWT" \
111
+ -H "content-type: application/json" \
112
+ -d '{
113
+ "path": "/weather",
114
+ "method": "GET",
115
+ "pricePerCall": 0,
116
+ "targetUrl": "https://api.example.com",
117
+ "paymentProtocol": "x402",
118
+ "paymentNetwork": "eip155:8453",
119
+ "trustEnabled": false,
120
+ "upstreamAuthType": "none"
121
+ }'
122
+ ```
123
+
124
+ ### Create A Paid x402 Wrapper
125
+
126
+ ```bash
127
+ curl -X POST https://valiron-edge-proxy.onrender.com/operator/endpoints \
128
+ -H "authorization: Bearer $VALIRON_JWT" \
129
+ -H "content-type: application/json" \
130
+ -d '{
131
+ "path": "/weather",
132
+ "method": "GET",
133
+ "pricePerCall": 0.001,
134
+ "targetUrl": "https://api.example.com",
135
+ "paymentProtocol": "x402",
136
+ "paymentNetwork": "eip155:8453",
137
+ "payoutAddress": "0xYourPayoutWallet",
138
+ "trustEnabled": false,
139
+ "upstreamAuthType": "none"
140
+ }'
141
+ ```
142
+
143
+ ### Create A Paid MPP Wrapper
144
+
145
+ ```bash
146
+ curl -X POST https://valiron-edge-proxy.onrender.com/operator/endpoints \
147
+ -H "authorization: Bearer $VALIRON_JWT" \
148
+ -H "content-type: application/json" \
149
+ -d '{
150
+ "path": "/research",
151
+ "method": "GET",
152
+ "pricePerCall": 0.001,
153
+ "targetUrl": "https://api.example.com",
154
+ "paymentProtocol": "mpp",
155
+ "paymentNetwork": "tempo:4217",
156
+ "payoutAddress": "0xYourPayoutWallet",
157
+ "trustEnabled": false,
158
+ "upstreamAuthType": "none"
159
+ }'
160
+ ```
161
+
162
+ For Tempo testnet, use:
163
+
164
+ ```json
165
+ { "paymentNetwork": "tempo:42431" }
166
+ ```
167
+
168
+ ### List Wrappers
169
+
170
+ ```bash
171
+ curl https://valiron-edge-proxy.onrender.com/operator/endpoints \
172
+ -H "authorization: Bearer $VALIRON_JWT"
173
+ ```
174
+
175
+ ### Deactivate A Wrapper
176
+
177
+ ```bash
178
+ curl -X DELETE https://valiron-edge-proxy.onrender.com/operator/endpoints/{endpointId} \
179
+ -H "authorization: Bearer $VALIRON_JWT"
180
+ ```
181
+
182
+ Deactivation is a soft delete. Existing logs remain available.
183
+
184
+ ## Calling A Wrapper
185
+
186
+ Call the generated URL directly:
187
+
188
+ ```bash
189
+ curl -i https://valiron-edge-proxy.onrender.com/wrap/{operatorId}/weather
190
+ ```
191
+
192
+ For a free wrapper, Valiron forwards immediately and returns the upstream response with Valiron wrapper headers:
193
+
194
+ ```text
195
+ x-valiron-wrapper-id: ...
196
+ x-valiron-payment-protocol: free
197
+ x-valiron-trust-enabled: false
198
+ ```
199
+
200
+ For a paid x402 wrapper without payment, Valiron returns `402` and an x402 payment challenge:
201
+
202
+ ```text
203
+ HTTP/1.1 402 Payment Required
204
+ x-payment-required: ...
205
+ ```
206
+
207
+ For a paid MPP wrapper without payment, Valiron returns `402` and an MPP challenge:
208
+
209
+ ```text
210
+ HTTP/1.1 402 Payment Required
211
+ WWW-Authenticate: Payment ...
212
+ ```
213
+
214
+ ## x402 Paid Client
215
+
216
+ Use an x402-capable client to retry with payment. The repo includes a local buyer test client in `services/x402-test-server/src/buyer-test.ts`, and the production smoke path uses the same libraries:
217
+
218
+ - `@x402/fetch`
219
+ - `@x402/core/client`
220
+ - `@x402/evm/exact/client`
221
+ - `viem/accounts`
222
+
223
+ Minimal client shape:
224
+
225
+ ```typescript
226
+ import { wrapFetchWithPayment } from "@x402/fetch";
227
+ import { x402Client } from "@x402/core/client";
228
+ import { ExactEvmScheme } from "@x402/evm/exact/client";
229
+ import { privateKeyToAccount } from "viem/accounts";
230
+
231
+ const account = privateKeyToAccount(process.env.BUYER_PRIVATE_KEY as `0x${string}`);
232
+
233
+ const client = new x402Client();
234
+ client.register("eip155:*", new ExactEvmScheme(account));
235
+
236
+ const fetchWithPayment = wrapFetchWithPayment(fetch, client);
237
+
238
+ const response = await fetchWithPayment(
239
+ "https://valiron-edge-proxy.onrender.com/wrap/{operatorId}/weather",
240
+ { method: "GET", headers: { "x-agent-id": "25459" } }
241
+ );
242
+
243
+ console.log(response.status);
244
+ console.log(await response.text());
245
+ ```
246
+
247
+ The buyer wallet must hold the required token on the selected x402 network. For Base mainnet, the default production path expects USDC on Base.
248
+
249
+ ## MPP Paid Client
250
+
251
+ Valiron MPP support uses the official `mppx` SDK with the Tempo `charge` intent.
252
+
253
+ Server behavior:
254
+
255
+ - Unpaid request returns `402`.
256
+ - Response includes `WWW-Authenticate: Payment ...`.
257
+ - Paid request includes `Authorization: Payment ...`.
258
+ - Successful paid response includes `Payment-Receipt`.
259
+
260
+ Recommended testnet client flow:
261
+
262
+ ```bash
263
+ pnpm exec mppx account create
264
+ pnpm exec mppx https://valiron-edge-proxy.onrender.com/wrap/{operatorId}/research
265
+ ```
266
+
267
+ Use `tempo:42431` for Tempo testnet and `tempo:4217` for Tempo mainnet.
268
+
269
+ Default Tempo currencies from `mppx`:
270
+
271
+ | Network | Chain ID | Default currency |
272
+ |---------|----------|------------------|
273
+ | Tempo mainnet | `4217` | USDC (`0x20C000000000000000000000b9537d11c60E8b50`) |
274
+ | Tempo testnet | `42431` | pathUSD (`0x20c0000000000000000000000000000000000000`) |
275
+
276
+ ## Optional Trust Checks
277
+
278
+ When `trustEnabled` is `false`, Valiron only handles payment, rate limiting, upstream auth, and forwarding.
279
+
280
+ When `trustEnabled` is `true`, callers must identify the agent:
281
+
282
+ ```text
283
+ x-agent-id: 25459
284
+ ```
285
+
286
+ Valiron evaluates the agent before forwarding. If the agent is denied, the upstream API is not called.
287
+
288
+ Use trust checks when:
289
+
290
+ - You want to block risky agents even if they can pay.
291
+ - You want route decisions and call logs tied to agent IDs.
292
+ - You plan to vary pricing, limits, or access based on trust tiers.
293
+
294
+ Use payment-only wrappers when:
295
+
296
+ - You want the lowest-friction paid endpoint.
297
+ - You do not need agent identity yet.
298
+ - You are testing x402 or MPP integration.
299
+
300
+ ## Upstream Auth
301
+
302
+ If your existing API requires auth, Valiron stores the secret encrypted and injects it only when forwarding a valid request upstream.
303
+
304
+ Supported auth modes:
305
+
306
+ | Mode | Behavior |
307
+ |------|----------|
308
+ | `none` | No upstream auth is added. |
309
+ | `bearer` | Adds `Authorization: Bearer <secret>`. |
310
+ | `api_key` | Adds the secret to the configured header name. |
311
+ | `hmac` | Signs the request using the secret and adds signature headers. |
312
+
313
+ The calling agent never sees your upstream secret.
314
+
315
+ Production requires `VALIRON_SECRET_ENCRYPTION_KEY` to store upstream auth secrets. Use a 32-byte hex key:
316
+
317
+ ```bash
318
+ openssl rand -hex 32
319
+ ```
320
+
321
+ ## Security Defaults
322
+
323
+ In production, Valiron wrappers:
324
+
325
+ - Require HTTPS upstream URLs.
326
+ - Block localhost, private IP ranges, link-local IPs, metadata hosts, and internal hostnames.
327
+ - Resolve upstream DNS and block private resolved IPs.
328
+ - Strip hop-by-hop and sensitive inbound headers before forwarding.
329
+ - Do not forward inbound `Authorization`, `Cookie`, `x-payment`, or `payment-signature` headers to the upstream.
330
+ - Add Valiron wrapper metadata headers to forwarded requests.
331
+ - Apply per-endpoint rate limits.
332
+ - Enforce request body size limits.
333
+ - Time out slow upstream calls.
334
+
335
+ Local development can set:
336
+
337
+ ```bash
338
+ VALIRON_ALLOW_INSECURE_UPSTREAMS=true
339
+ ```
340
+
341
+ Only use this for local testing. Do not enable it in production.
342
+
343
+ ## Production Environment Variables
344
+
345
+ Render / edge proxy:
346
+
347
+ | Variable | Required | Purpose |
348
+ |----------|----------|---------|
349
+ | `VALIRON_SECRET_ENCRYPTION_KEY` | Required for upstream auth | AES-256-GCM key for storing upstream secrets. |
350
+ | `X402_RECEIVING_ADDRESS` | Required for paid x402 without per-endpoint payout | Default payout wallet. Per-endpoint `payoutAddress` can also be used. |
351
+ | `X402_FACILITATOR_URL` | Optional | Defaults to `https://facilitator.payai.network`. |
352
+ | `MPP_SECRET_KEY` | Required for MPP | Secret used by `mppx` to sign/verify payment challenges. |
353
+ | `MPP_TEMPO_CURRENCY` | Optional | Override Tempo currency. Defaults come from `mppx`. |
354
+ | `MPP_TEMPO_CHAIN_ID` | Optional | Fallback Tempo chain ID when not set by endpoint. |
355
+ | `MPP_TEMPO_TESTNET` | Optional | Fallback testnet flag when not set by endpoint. |
356
+ | `MPP_TEMPO_WAIT_FOR_CONFIRMATION` | Optional | Defaults to `true`; set `false` only for specific test flows. |
357
+ | `MPP_REALM` | Optional | Defaults to `Valiron`. |
358
+ | `VALIRON_WRAPPER_MAX_BODY_BYTES` | Optional | Request body size limit. |
359
+ | `VALIRON_WRAPPER_UPSTREAM_TIMEOUT_MS` | Optional | Upstream request timeout. |
360
+
361
+ Vercel / website:
362
+
363
+ | Variable | Required | Purpose |
364
+ |----------|----------|---------|
365
+ | `NEXT_PUBLIC_API_URL` | Recommended | Edge proxy base URL used by the dashboard. |
366
+
367
+ ## Local Test Example
368
+
369
+ Run the local x402 test server:
370
+
371
+ ```bash
372
+ pnpm --filter x402-test-server dev
373
+ ```
374
+
375
+ Run the edge proxy with local upstreams allowed:
376
+
377
+ ```bash
378
+ VALIRON_ALLOW_INSECURE_UPSTREAMS=true pnpm --filter edge-proxy dev
379
+ ```
380
+
381
+ Create a free test wrapper pointing at:
382
+
383
+ ```text
384
+ http://localhost:4021
385
+ ```
386
+
387
+ with public path:
388
+
389
+ ```text
390
+ /health
391
+ ```
392
+
393
+ Then call:
394
+
395
+ ```bash
396
+ curl -i http://localhost:3005/wrap/{operatorId}/health
397
+ ```
398
+
399
+ For paid x402 tests, use the buyer test client with a funded buyer key and the configured facilitator/network.
400
+
401
+ ## Current Limits
402
+
403
+ The first wrapper release supports:
404
+
405
+ - Per-call x402 payments.
406
+ - Per-call MPP Tempo charge payments.
407
+ - Free wrappers.
408
+ - Optional trust checks.
409
+ - Optional upstream auth injection.
410
+ - Per-endpoint rate limits.
411
+
412
+ It does not yet support:
413
+
414
+ - MPP sessions or subscriptions.
415
+ - Multi-upstream routing.
416
+ - Cross-protocol fallback on a single endpoint.
417
+ - Non-HTTPS production upstreams.