apinow-sdk 0.14.0 → 0.15.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.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,219 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from 'commander';
3
+ import { createClient } from './index.js';
4
+ const API_BASE = 'https://apinow.fun';
5
+ const program = new Command();
6
+ program
7
+ .name('apinow')
8
+ .description('CLI for APINow.fun — search, inspect, and call pay-per-request APIs')
9
+ .version('0.15.0');
10
+ // ─── Helpers ───
11
+ function getPrivateKey(opts) {
12
+ const raw = opts.key || process.env.APINOW_WALLET_PKEY || process.env.PRIVATE_KEY;
13
+ if (!raw) {
14
+ console.error('Error: Private key required. Set APINOW_WALLET_PKEY or pass --key');
15
+ process.exit(1);
16
+ }
17
+ return (raw.startsWith('0x') ? raw : `0x${raw}`);
18
+ }
19
+ async function fetchJson(url, init) {
20
+ const res = await fetch(url, init);
21
+ if (!res.ok) {
22
+ const body = await res.text();
23
+ throw new Error(`${res.status} ${res.statusText}: ${body}`);
24
+ }
25
+ return res.json();
26
+ }
27
+ function formatUsd(paymentOptions) {
28
+ if (!paymentOptions?.length)
29
+ return 'free';
30
+ const p = paymentOptions[0];
31
+ const usd = p.usdAmount ?? p.amount ?? 0;
32
+ return `$${Number(usd).toFixed(4)}`;
33
+ }
34
+ function printTable(rows) {
35
+ if (!rows.length)
36
+ return;
37
+ const widths = rows[0].map((_, col) => Math.max(...rows.map((r) => (r[col] || '').length)));
38
+ for (const row of rows) {
39
+ console.log(row.map((cell, i) => (cell || '').padEnd(widths[i])).join(' '));
40
+ }
41
+ }
42
+ function truncate(s, max) {
43
+ return s.length > max ? s.slice(0, max - 1) + '…' : s;
44
+ }
45
+ // ─── search ───
46
+ program
47
+ .command('search <query>')
48
+ .description('Semantic search across all endpoints')
49
+ .option('-l, --limit <n>', 'Max results', '10')
50
+ .action(async (query, opts) => {
51
+ try {
52
+ const data = await fetchJson(`${API_BASE}/api/endpoints/semantic-search`, {
53
+ method: 'POST',
54
+ headers: { 'Content-Type': 'application/json' },
55
+ body: JSON.stringify({ query, limit: Number(opts.limit) }),
56
+ });
57
+ const endpoints = data.endpoints || data.results || data;
58
+ if (!Array.isArray(endpoints) || !endpoints.length) {
59
+ console.log('No results found.');
60
+ return;
61
+ }
62
+ const rows = [['ENDPOINT', 'METHOD', 'COST', 'DESCRIPTION']];
63
+ for (const ep of endpoints) {
64
+ const key = `${ep.namespace}/${ep.endpointName}`;
65
+ rows.push([
66
+ key,
67
+ ep.httpMethod || 'POST',
68
+ formatUsd(ep.paymentOptions),
69
+ truncate(ep.description || '', 60),
70
+ ]);
71
+ }
72
+ printTable(rows);
73
+ }
74
+ catch (err) {
75
+ console.error(`Error: ${err.message}`);
76
+ process.exit(1);
77
+ }
78
+ });
79
+ // ─── list ───
80
+ program
81
+ .command('list')
82
+ .description('List available endpoints')
83
+ .option('-l, --limit <n>', 'Max results', '20')
84
+ .option('-s, --sort <sortBy>', 'Sort by: popular | newest', 'popular')
85
+ .option('-n, --namespace <ns>', 'Filter by namespace')
86
+ .option('-q, --search <term>', 'Text search filter')
87
+ .action(async (opts) => {
88
+ try {
89
+ const params = new URLSearchParams({
90
+ limit: opts.limit,
91
+ sortBy: opts.sort,
92
+ });
93
+ if (opts.namespace)
94
+ params.set('namespace', opts.namespace);
95
+ if (opts.search)
96
+ params.set('search', opts.search);
97
+ const data = await fetchJson(`${API_BASE}/api/endpoints?${params}`);
98
+ const endpoints = data.endpoints || [];
99
+ if (!endpoints.length) {
100
+ console.log('No endpoints found.');
101
+ return;
102
+ }
103
+ const rows = [['ENDPOINT', 'METHOD', 'COST', 'CALLS', 'DESCRIPTION']];
104
+ for (const ep of endpoints) {
105
+ rows.push([
106
+ `${ep.namespace}/${ep.endpointName}`,
107
+ ep.httpMethod || 'POST',
108
+ formatUsd(ep.paymentOptions),
109
+ String(ep.callCount || 0),
110
+ truncate(ep.description || '', 50),
111
+ ]);
112
+ }
113
+ printTable(rows);
114
+ if (data.hasMore) {
115
+ console.log(`\n… more results available (showing ${endpoints.length})`);
116
+ }
117
+ }
118
+ catch (err) {
119
+ console.error(`Error: ${err.message}`);
120
+ process.exit(1);
121
+ }
122
+ });
123
+ // ─── info ───
124
+ program
125
+ .command('info <endpoint>')
126
+ .description('Get endpoint details (cost, wallet, schemas, examples)')
127
+ .action(async (endpoint) => {
128
+ try {
129
+ if (!endpoint.includes('/')) {
130
+ throw new Error('Format: namespace/endpoint-name');
131
+ }
132
+ const [ns, ep] = endpoint.split('/');
133
+ const data = await fetchJson(`${API_BASE}/api/endpoints/${ns}/${ep}/details`);
134
+ const cost = formatUsd(data.paymentOptions);
135
+ const payment = data.paymentOptions?.[0];
136
+ console.log(`\n ${data.namespace}/${data.endpointName}`);
137
+ console.log(` ${'─'.repeat(40)}`);
138
+ console.log(` ${data.description || '(no description)'}\n`);
139
+ console.log(` Method: ${data.httpMethod || 'POST'}`);
140
+ console.log(` Cost: ${cost}`);
141
+ console.log(` Chain: ${data.chain || 'base'}`);
142
+ console.log(` Wallet: ${data.walletAddress || '—'}`);
143
+ if (data.model)
144
+ console.log(` Model: ${data.model}`);
145
+ if (data.tags?.length)
146
+ console.log(` Tags: ${data.tags.join(', ')}`);
147
+ if (data.docsUrl)
148
+ console.log(` Docs: ${data.docsUrl}`);
149
+ if (payment) {
150
+ console.log(`\n Payment:`);
151
+ console.log(` Network: ${payment.network || 'base-sepolia'}`);
152
+ console.log(` Token: ${payment.resource || payment.token || 'USDC'}`);
153
+ console.log(` Amount: ${payment.amount ?? '—'}`);
154
+ if (payment.usdAmount != null)
155
+ console.log(` USD: $${payment.usdAmount}`);
156
+ }
157
+ if (data.querySchema) {
158
+ console.log(`\n Input Schema:`);
159
+ const schema = data.querySchema;
160
+ if (schema.properties) {
161
+ const required = new Set(schema.required || []);
162
+ for (const [key, val] of Object.entries(schema.properties)) {
163
+ const req = required.has(key) ? ' (required)' : '';
164
+ const desc = val.description ? ` — ${val.description}` : '';
165
+ const ex = val.example ? ` e.g. ${JSON.stringify(val.example)}` : '';
166
+ console.log(` ${key}: ${val.type || 'any'}${req}${desc}${ex}`);
167
+ }
168
+ }
169
+ else {
170
+ console.log(` ${JSON.stringify(schema, null, 4).replace(/\n/g, '\n ')}`);
171
+ }
172
+ }
173
+ if (data.responseSchema) {
174
+ console.log(`\n Output Schema:`);
175
+ console.log(` ${JSON.stringify(data.responseSchema, null, 4).replace(/\n/g, '\n ')}`);
176
+ }
177
+ if (data.exampleQuery) {
178
+ console.log(`\n Example Input:`);
179
+ console.log(` ${JSON.stringify(data.exampleQuery, null, 2).replace(/\n/g, '\n ')}`);
180
+ }
181
+ console.log('');
182
+ }
183
+ catch (err) {
184
+ console.error(`Error: ${err.message}`);
185
+ process.exit(1);
186
+ }
187
+ });
188
+ // ─── call ───
189
+ program
190
+ .command('call <endpoint>')
191
+ .description('Call an endpoint (x402 paid)')
192
+ .option('-d, --data <json>', 'JSON body')
193
+ .option('-m, --method <method>', 'HTTP method')
194
+ .option('-k, --key <privateKey>', 'Wallet private key')
195
+ .action(async (endpoint, opts) => {
196
+ try {
197
+ if (!endpoint.includes('/')) {
198
+ throw new Error('Format: namespace/endpoint-name');
199
+ }
200
+ const privateKey = getPrivateKey(opts);
201
+ const [ns, ep] = endpoint.split('/');
202
+ const details = await fetchJson(`${API_BASE}/api/endpoints/${ns}/${ep}/details`);
203
+ const method = (opts.method || details?.httpMethod || 'POST').toUpperCase();
204
+ const body = opts.data ? JSON.parse(opts.data) : undefined;
205
+ const cost = formatUsd(details?.paymentOptions);
206
+ console.error(`Calling ${ns}/${ep} [${method}] — cost: ${cost}`);
207
+ const apinow = createClient({ privateKey });
208
+ const result = await apinow.call(`/api/endpoints/${ns}/${ep}`, {
209
+ method: method,
210
+ ...(body ? { body } : {}),
211
+ });
212
+ console.log(JSON.stringify(result, null, 2));
213
+ }
214
+ catch (err) {
215
+ console.error(`Error: ${err.message}`);
216
+ process.exit(1);
217
+ }
218
+ });
219
+ program.parse();
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "apinow-sdk",
3
- "version": "0.14.0",
4
- "description": "Pay-per-call API SDK for APINow.fun — wraps x402 so you don't have to",
3
+ "version": "0.15.0",
4
+ "description": "Pay-per-call API SDK & CLI for APINow.fun — wraps x402 so you don't have to",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "type": "module",
8
+ "bin": {
9
+ "apinow": "dist/cli.js"
10
+ },
8
11
  "files": [
9
12
  "dist"
10
13
  ],
@@ -20,7 +23,8 @@
20
23
  "base",
21
24
  "web3",
22
25
  "ai",
23
- "llm"
26
+ "llm",
27
+ "cli"
24
28
  ],
25
29
  "author": "ApiNow.fun",
26
30
  "license": "MIT",
@@ -30,8 +34,9 @@
30
34
  },
31
35
  "homepage": "https://apinow.fun",
32
36
  "dependencies": {
33
- "@x402/fetch": "^2.3.0",
34
37
  "@x402/evm": "^2.3.1",
38
+ "@x402/fetch": "^2.3.0",
39
+ "commander": "^13.1.0",
35
40
  "viem": "^2.38.3"
36
41
  },
37
42
  "devDependencies": {