apinow-sdk 0.19.0 → 0.20.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/dist/cli.js +67 -1
- package/dist/index.d.ts +36 -0
- package/dist/index.js +42 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -7,7 +7,7 @@ const program = new Command();
|
|
|
7
7
|
program
|
|
8
8
|
.name('apinow')
|
|
9
9
|
.description('CLI for APINow.fun — search, inspect, and call pay-per-request APIs')
|
|
10
|
-
.version('0.
|
|
10
|
+
.version('0.20.0');
|
|
11
11
|
// ─── Helpers ───
|
|
12
12
|
function getPrivateKey(opts) {
|
|
13
13
|
const raw = opts.key || process.env.APINOW_WALLET_PKEY || process.env.PRIVATE_KEY;
|
|
@@ -311,6 +311,72 @@ program
|
|
|
311
311
|
process.exit(1);
|
|
312
312
|
}
|
|
313
313
|
});
|
|
314
|
+
// ─── discover (external x402) ───
|
|
315
|
+
program
|
|
316
|
+
.command('discover <url>')
|
|
317
|
+
.description('Discover the x402 price of any external endpoint (free)')
|
|
318
|
+
.option('-m, --method <method>', 'HTTP method the target expects', 'POST')
|
|
319
|
+
.action(async (url, opts) => {
|
|
320
|
+
try {
|
|
321
|
+
const params = new URLSearchParams({ url, method: opts.method.toUpperCase() });
|
|
322
|
+
const data = await fetchJson(`${API_BASE}/api/x402-proxy?${params}`);
|
|
323
|
+
console.log(`\n ${data.url}`);
|
|
324
|
+
console.log(` ${'─'.repeat(50)}`);
|
|
325
|
+
console.log(` Method: ${data.method}`);
|
|
326
|
+
console.log(` x402 endpoint: ${data.isX402 ? 'YES' : 'NO'}`);
|
|
327
|
+
console.log(` Upstream price: ${data.upstreamPrice}`);
|
|
328
|
+
console.log(` Proxy fee: ${data.proxyFee}`);
|
|
329
|
+
console.log(` Total price: ${data.totalPrice}`);
|
|
330
|
+
console.log(` Network: ${data.network}`);
|
|
331
|
+
if (data.upstreamAccepts?.length) {
|
|
332
|
+
console.log(` Accepts:`);
|
|
333
|
+
for (const a of data.upstreamAccepts) {
|
|
334
|
+
console.log(` scheme=${a.scheme} payTo=${a.payTo?.substring(0, 16)}…`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
console.log('');
|
|
338
|
+
}
|
|
339
|
+
catch (err) {
|
|
340
|
+
console.error(`Error: ${err.message}`);
|
|
341
|
+
process.exit(1);
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
// ─── call-external (external x402 proxy) ───
|
|
345
|
+
program
|
|
346
|
+
.command('call-external <url>')
|
|
347
|
+
.description('Call any external x402 endpoint through the APINow proxy (paid)')
|
|
348
|
+
.option('-d, --data <json>', 'JSON body to send to the target')
|
|
349
|
+
.option('-m, --method <method>', 'HTTP method', 'POST')
|
|
350
|
+
.option('-H, --header <kv...>', 'Extra headers as key:value pairs')
|
|
351
|
+
.option('-k, --key <privateKey>', 'Wallet private key')
|
|
352
|
+
.action(async (url, opts) => {
|
|
353
|
+
try {
|
|
354
|
+
const privateKey = getPrivateKey(opts);
|
|
355
|
+
const body = opts.data ? JSON.parse(opts.data) : undefined;
|
|
356
|
+
const headers = {};
|
|
357
|
+
if (opts.header) {
|
|
358
|
+
for (const h of opts.header) {
|
|
359
|
+
const idx = h.indexOf(':');
|
|
360
|
+
if (idx > 0)
|
|
361
|
+
headers[h.slice(0, idx).trim()] = h.slice(idx + 1).trim();
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const params = new URLSearchParams({ url, method: opts.method.toUpperCase() });
|
|
365
|
+
const priceData = await fetchJson(`${API_BASE}/api/x402-proxy?${params}`);
|
|
366
|
+
console.error(`Calling ${url} [${opts.method.toUpperCase()}] — total cost: ${priceData.totalPrice} (upstream: ${priceData.upstreamPrice} + fee: ${priceData.proxyFee})`);
|
|
367
|
+
const apinow = createClient({ privateKey });
|
|
368
|
+
const result = await apinow.callExternal(url, {
|
|
369
|
+
method: opts.method.toUpperCase(),
|
|
370
|
+
...(body ? { body } : {}),
|
|
371
|
+
...(Object.keys(headers).length ? { headers } : {}),
|
|
372
|
+
});
|
|
373
|
+
console.log(JSON.stringify(result, null, 2));
|
|
374
|
+
}
|
|
375
|
+
catch (err) {
|
|
376
|
+
console.error(`Error: ${err.message}`);
|
|
377
|
+
process.exit(1);
|
|
378
|
+
}
|
|
379
|
+
});
|
|
314
380
|
// ─── workflows ───
|
|
315
381
|
program
|
|
316
382
|
.command('workflows')
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,21 @@ export interface CallOptions {
|
|
|
3
3
|
body?: Record<string, any>;
|
|
4
4
|
headers?: Record<string, string>;
|
|
5
5
|
}
|
|
6
|
+
export interface ExternalCallOptions {
|
|
7
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
8
|
+
body?: Record<string, any>;
|
|
9
|
+
headers?: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
export interface PriceDiscovery {
|
|
12
|
+
url: string;
|
|
13
|
+
method: string;
|
|
14
|
+
isX402: boolean;
|
|
15
|
+
upstreamPrice: string;
|
|
16
|
+
proxyFee: string;
|
|
17
|
+
totalPrice: string;
|
|
18
|
+
network: string;
|
|
19
|
+
upstreamAccepts: any[];
|
|
20
|
+
}
|
|
6
21
|
export interface ApinowConfig {
|
|
7
22
|
privateKey: `0x${string}`;
|
|
8
23
|
baseUrl?: string;
|
|
@@ -155,6 +170,27 @@ export declare function createClient(config: ApinowConfig): {
|
|
|
155
170
|
input?: any;
|
|
156
171
|
saveExample?: boolean;
|
|
157
172
|
}): Promise<any>;
|
|
173
|
+
/**
|
|
174
|
+
* Discover the x402 price for any external URL without executing it.
|
|
175
|
+
* Free — no payment required.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* const price = await apinow.discoverPrice('https://stablesocial.dev/api/tiktok/profile');
|
|
179
|
+
* console.log(price.totalPrice); // "$0.070000"
|
|
180
|
+
*/
|
|
181
|
+
discoverPrice(url: string, method?: string): Promise<PriceDiscovery>;
|
|
182
|
+
/**
|
|
183
|
+
* Call any external x402 endpoint through the APINow proxy.
|
|
184
|
+
* Handles payment automatically: you pay APINow (upstream price + proxy fee),
|
|
185
|
+
* and APINow pays the upstream service with its server wallet.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* const data = await apinow.callExternal('https://stablesocial.dev/api/tiktok/profile', {
|
|
189
|
+
* method: 'POST',
|
|
190
|
+
* body: { handle: 'someuser' },
|
|
191
|
+
* });
|
|
192
|
+
*/
|
|
193
|
+
callExternal(url: string, opts?: ExternalCallOptions): Promise<any>;
|
|
158
194
|
/**
|
|
159
195
|
* The underlying x402-wrapped fetch, for advanced use.
|
|
160
196
|
*/
|
package/dist/index.js
CHANGED
|
@@ -270,6 +270,48 @@ export function createClient(config) {
|
|
|
270
270
|
}
|
|
271
271
|
return res.json();
|
|
272
272
|
},
|
|
273
|
+
// ─── External x402 Proxy ───
|
|
274
|
+
/**
|
|
275
|
+
* Discover the x402 price for any external URL without executing it.
|
|
276
|
+
* Free — no payment required.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* const price = await apinow.discoverPrice('https://stablesocial.dev/api/tiktok/profile');
|
|
280
|
+
* console.log(price.totalPrice); // "$0.070000"
|
|
281
|
+
*/
|
|
282
|
+
async discoverPrice(url, method) {
|
|
283
|
+
const params = new URLSearchParams({ url });
|
|
284
|
+
if (method)
|
|
285
|
+
params.set('method', method);
|
|
286
|
+
const res = await fetch(`${baseUrl}/api/x402-proxy?${params}`);
|
|
287
|
+
if (!res.ok) {
|
|
288
|
+
const text = await res.text();
|
|
289
|
+
throw new Error(`Discovery failed: ${res.status} ${text}`);
|
|
290
|
+
}
|
|
291
|
+
return res.json();
|
|
292
|
+
},
|
|
293
|
+
/**
|
|
294
|
+
* Call any external x402 endpoint through the APINow proxy.
|
|
295
|
+
* Handles payment automatically: you pay APINow (upstream price + proxy fee),
|
|
296
|
+
* and APINow pays the upstream service with its server wallet.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* const data = await apinow.callExternal('https://stablesocial.dev/api/tiktok/profile', {
|
|
300
|
+
* method: 'POST',
|
|
301
|
+
* body: { handle: 'someuser' },
|
|
302
|
+
* });
|
|
303
|
+
*/
|
|
304
|
+
async callExternal(url, opts = {}) {
|
|
305
|
+
return this.call('/api/x402-proxy', {
|
|
306
|
+
method: 'POST',
|
|
307
|
+
body: {
|
|
308
|
+
url,
|
|
309
|
+
method: opts.method || 'POST',
|
|
310
|
+
body: opts.body,
|
|
311
|
+
headers: opts.headers,
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
},
|
|
273
315
|
/**
|
|
274
316
|
* The underlying x402-wrapped fetch, for advanced use.
|
|
275
317
|
*/
|
package/package.json
CHANGED