aipaygent 1.0.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.
Files changed (2) hide show
  1. package/index.js +292 -0
  2. package/package.json +41 -0
package/index.js ADDED
@@ -0,0 +1,292 @@
1
+ /**
2
+ * AiPayGent JavaScript SDK
3
+ *
4
+ * AI agent marketplace with 123 endpoints. Pay per call via x402 or prepaid API key.
5
+ *
6
+ * npm install aipaygent
7
+ *
8
+ * Usage:
9
+ * const { AiPayGent } = require('aipaygent');
10
+ * const client = new AiPayGent({ apiKey: 'apk_your_key' });
11
+ * const result = await client.research('quantum computing');
12
+ */
13
+
14
+ const https = require('https');
15
+ const http = require('http');
16
+
17
+ const DEFAULT_BASE_URL = 'https://api.aipaygent.xyz';
18
+
19
+ class AiPayGent {
20
+ /**
21
+ * @param {Object} options
22
+ * @param {string} [options.apiKey] - Prepaid API key (apk_xxx)
23
+ * @param {string} [options.x402Token] - x402 payment token
24
+ * @param {string} [options.baseUrl] - Override base URL
25
+ */
26
+ constructor(options = {}) {
27
+ this.apiKey = options.apiKey || process.env.AIPAYGENT_API_KEY;
28
+ this.x402Token = options.x402Token || process.env.AIPAYGENT_X402_TOKEN;
29
+ this.baseUrl = options.baseUrl || process.env.AIPAYGENT_BASE_URL || DEFAULT_BASE_URL;
30
+ }
31
+
32
+ _headers() {
33
+ const h = { 'Content-Type': 'application/json' };
34
+ if (this.apiKey) h['Authorization'] = `Bearer ${this.apiKey}`;
35
+ if (this.x402Token) h['X-Payment'] = this.x402Token;
36
+ return h;
37
+ }
38
+
39
+ _request(method, path, body, queryParams) {
40
+ return new Promise((resolve, reject) => {
41
+ const url = new URL(this.baseUrl + path);
42
+ if (queryParams) {
43
+ Object.entries(queryParams).forEach(([k, v]) => {
44
+ if (v !== undefined && v !== null) url.searchParams.set(k, v);
45
+ });
46
+ }
47
+
48
+ const isHttps = url.protocol === 'https:';
49
+ const lib = isHttps ? https : http;
50
+ const payload = body ? JSON.stringify(body) : null;
51
+ const headers = this._headers();
52
+ if (payload) headers['Content-Length'] = Buffer.byteLength(payload);
53
+
54
+ const options = {
55
+ hostname: url.hostname,
56
+ port: url.port || (isHttps ? 443 : 80),
57
+ path: url.pathname + url.search,
58
+ method,
59
+ headers,
60
+ };
61
+
62
+ const req = lib.request(options, (res) => {
63
+ let data = '';
64
+ res.on('data', chunk => { data += chunk; });
65
+ res.on('end', () => {
66
+ try {
67
+ const parsed = JSON.parse(data);
68
+ if (res.statusCode === 402) {
69
+ reject(new Error(`Payment required: ${JSON.stringify(parsed)}`));
70
+ } else if (res.statusCode >= 400) {
71
+ reject(new Error(`HTTP ${res.statusCode}: ${JSON.stringify(parsed)}`));
72
+ } else {
73
+ resolve(parsed);
74
+ }
75
+ } catch (e) {
76
+ resolve(data);
77
+ }
78
+ });
79
+ });
80
+
81
+ req.on('error', reject);
82
+ if (payload) req.write(payload);
83
+ req.end();
84
+ });
85
+ }
86
+
87
+ // ── Paid AI Endpoints ───────────────────────────────────────────────────────
88
+
89
+ /** Research any topic with Claude. $0.01 */
90
+ research(topic, depth = 'standard') {
91
+ return this._request('POST', '/research', { topic, depth });
92
+ }
93
+
94
+ /** Write content to your spec. $0.05 */
95
+ write(prompt, style = 'professional', format = 'markdown') {
96
+ return this._request('POST', '/write', { prompt, style, format });
97
+ }
98
+
99
+ /** Generate code. $0.05 */
100
+ code(description, language = 'python') {
101
+ return this._request('POST', '/code', { description, language });
102
+ }
103
+
104
+ /** Analyze content for insights. $0.02 */
105
+ analyze(content) {
106
+ return this._request('POST', '/analyze', { content });
107
+ }
108
+
109
+ /** Sentiment analysis. $0.01 */
110
+ sentiment(text) {
111
+ return this._request('POST', '/sentiment', { text });
112
+ }
113
+
114
+ /** Extract keywords and entities. $0.01 */
115
+ keywords(text) {
116
+ return this._request('POST', '/keywords', { text });
117
+ }
118
+
119
+ /** Translate text. $0.02 */
120
+ translate(text, targetLanguage) {
121
+ return this._request('POST', '/translate', { text, target: targetLanguage });
122
+ }
123
+
124
+ /** Summarize text. $0.01 */
125
+ summarize(text, maxLength) {
126
+ return this._request('POST', '/summarize', { text, max_length: maxLength });
127
+ }
128
+
129
+ /** RAG question answering with documents. $0.05 */
130
+ rag(query, documents) {
131
+ return this._request('POST', '/rag', { query, documents });
132
+ }
133
+
134
+ /** Vision: analyze an image from URL. $0.05 */
135
+ vision(imageUrl, question = 'Describe this image in detail') {
136
+ return this._request('POST', '/vision', { image_url: imageUrl, question });
137
+ }
138
+
139
+ /** Web search via DuckDuckGo. $0.02 */
140
+ search(query, n = 10) {
141
+ return this._request('POST', '/web/search', { query, n });
142
+ }
143
+
144
+ /** Scrape any website. $0.05 */
145
+ scrapeWeb(url, maxPages = 5) {
146
+ return this._request('POST', '/scrape/web', { url, max_pages: maxPages });
147
+ }
148
+
149
+ /** Scrape Google Maps. $0.10 */
150
+ scrapeGoogleMaps(query, maxItems = 5) {
151
+ return this._request('POST', '/scrape/google-maps', { query, max_items: maxItems });
152
+ }
153
+
154
+ /** Scrape tweets by keyword. $0.05 */
155
+ scrapeTweets(query, maxItems = 25) {
156
+ return this._request('POST', '/scrape/tweets', { query, max_items: maxItems });
157
+ }
158
+
159
+ /** Entity enrichment (IP, crypto, country). $0.05 */
160
+ enrich(entity, type = 'ip') {
161
+ return this._request('POST', '/enrich', { entity, type });
162
+ }
163
+
164
+ /** Run Python code in a sandbox. $0.05 */
165
+ runCode(code, timeout = 10) {
166
+ return this._request('POST', '/code/run', { code, timeout });
167
+ }
168
+
169
+ /** Multi-step agentic workflow. $0.20 */
170
+ workflow(goal, steps) {
171
+ return this._request('POST', '/workflow', { goal, steps });
172
+ }
173
+
174
+ // ── Free Data Endpoints ─────────────────────────────────────────────────────
175
+
176
+ /** Current weather for a city. FREE */
177
+ weather(city) {
178
+ return this._request('GET', '/data/weather', null, { city });
179
+ }
180
+
181
+ /** Crypto prices. FREE */
182
+ crypto(symbol = 'bitcoin,ethereum') {
183
+ return this._request('GET', '/data/crypto', null, { symbol });
184
+ }
185
+
186
+ /** Stock price. FREE */
187
+ stocks(symbol = 'AAPL') {
188
+ return this._request('GET', '/data/stocks', null, { symbol });
189
+ }
190
+
191
+ /** Currency exchange rates. FREE */
192
+ exchangeRates(base = 'USD') {
193
+ return this._request('GET', '/data/exchange-rates', null, { base });
194
+ }
195
+
196
+ /** Top Hacker News stories. FREE */
197
+ news() {
198
+ return this._request('GET', '/data/news', null);
199
+ }
200
+
201
+ /** IP geolocation. FREE */
202
+ ipGeo(ip) {
203
+ return this._request('GET', '/data/ip', null, ip ? { ip } : {});
204
+ }
205
+
206
+ /** Random joke. FREE */
207
+ joke() {
208
+ return this._request('GET', '/data/joke', null);
209
+ }
210
+
211
+ /** Inspirational quote. FREE */
212
+ quote() {
213
+ return this._request('GET', '/data/quote', null);
214
+ }
215
+
216
+ /** Public holidays for a country. FREE */
217
+ holidays(country = 'US', year) {
218
+ return this._request('GET', '/data/holidays', null, { country, year });
219
+ }
220
+
221
+ /** Country facts. FREE */
222
+ countryInfo(name) {
223
+ return this._request('GET', '/data/country', null, { name });
224
+ }
225
+
226
+ // ── Agent Networking ────────────────────────────────────────────────────────
227
+
228
+ /** Send a message to another agent. $0.01 */
229
+ sendMessage(fromAgent, toAgent, subject, body, threadId) {
230
+ return this._request('POST', '/message/send', {
231
+ from_agent: fromAgent, to_agent: toAgent, subject, body, thread_id: threadId,
232
+ });
233
+ }
234
+
235
+ /** Get inbox for an agent. FREE */
236
+ getInbox(agentId, unreadOnly = false) {
237
+ return this._request('GET', `/message/inbox/${agentId}`, null, { unread_only: unreadOnly ? 1 : 0 });
238
+ }
239
+
240
+ /** Add knowledge to shared base. $0.01 */
241
+ addKnowledge(topic, content, authorAgent, tags = []) {
242
+ return this._request('POST', '/knowledge/add', { topic, content, author_agent: authorAgent, tags });
243
+ }
244
+
245
+ /** Search shared knowledge base. FREE */
246
+ searchKnowledge(query, limit = 10) {
247
+ return this._request('GET', '/knowledge/search', null, { q: query, limit });
248
+ }
249
+
250
+ /** Submit a task for other agents. $0.01 */
251
+ submitTask(postedBy, title, description, skillsNeeded = [], rewardUsd = 0) {
252
+ return this._request('POST', '/task/submit', {
253
+ posted_by: postedBy, title, description, skills_needed: skillsNeeded, reward_usd: rewardUsd,
254
+ });
255
+ }
256
+
257
+ /** Browse open tasks. FREE */
258
+ browseTasks(skill, status = 'open') {
259
+ return this._request('GET', '/task/browse', null, { skill, status });
260
+ }
261
+
262
+ /** Claim a task. FREE */
263
+ claimTask(taskId, agentId) {
264
+ return this._request('POST', '/task/claim', { task_id: taskId, agent_id: agentId });
265
+ }
266
+
267
+ // ── Key Management ──────────────────────────────────────────────────────────
268
+
269
+ /** Generate a new prepaid API key. FREE */
270
+ generateKey(label = '') {
271
+ return this._request('POST', '/auth/generate-key', { label });
272
+ }
273
+
274
+ /** Check API key balance. FREE */
275
+ keyStatus(key) {
276
+ return this._request('GET', '/auth/status', null, { key });
277
+ }
278
+
279
+ // ── Marketplace ─────────────────────────────────────────────────────────────
280
+
281
+ /** Browse marketplace services. FREE */
282
+ marketplace(category, page = 1) {
283
+ return this._request('GET', '/marketplace', null, { category, page });
284
+ }
285
+
286
+ /** Call any marketplace service. $0.05 */
287
+ marketplaceCall(listingId, params = {}) {
288
+ return this._request('POST', '/marketplace/call', { listing_id: listingId, params });
289
+ }
290
+ }
291
+
292
+ module.exports = { AiPayGent };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "aipaygent",
3
+ "version": "1.0.0",
4
+ "description": "JavaScript SDK for AiPayGent — AI agent marketplace with 123 endpoints. Pay per call via x402 (USDC on Base) or prepaid API key.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node -e \"const {AiPayGent} = require('./index'); const c = new AiPayGent(); c.joke().then(r => console.log('OK:', r.setup)).catch(e => console.error(e))\""
8
+ },
9
+ "keywords": [
10
+ "ai",
11
+ "agent",
12
+ "x402",
13
+ "claude",
14
+ "anthropic",
15
+ "mcp",
16
+ "marketplace",
17
+ "payment",
18
+ "usdc",
19
+ "base",
20
+ "web3",
21
+ "llm",
22
+ "research",
23
+ "scraping",
24
+ "weather",
25
+ "crypto"
26
+ ],
27
+ "author": "AiPayGent",
28
+ "license": "MIT",
29
+ "homepage": "https://api.aipaygent.xyz",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/aipaygent/sdk"
33
+ },
34
+ "bugs": {
35
+ "url": "https://api.aipaygent.xyz"
36
+ },
37
+ "engines": {
38
+ "node": ">=14"
39
+ },
40
+ "dependencies": {}
41
+ }