synthra-x402 1.0.0 → 1.0.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 +101 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# synthra-x402
|
|
2
|
+
|
|
3
|
+
The official SDK for interacting with the **Synthra API Marketplace**. This SDK automatically handles x402-avm payments (L402 protocol) using prepaid USDC on the Algorand blockchain.
|
|
4
|
+
|
|
5
|
+
It allows human developers and autonomous AI agents to consume premium APIs without manually managing cryptocurrency payments or transaction signing.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install synthra-x402
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## For Human Developers (Browser)
|
|
14
|
+
|
|
15
|
+
If you are building a web application, you can initialize the client using the user's active wallet provider (e.g., Pera Wallet, Defly, etc.). The SDK will automatically prompt the user to approve USDC payments when required.
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { createSynthraClient } from 'synthra-x402/client'
|
|
19
|
+
|
|
20
|
+
// Assuming you are using @txnlab/use-wallet
|
|
21
|
+
const { activeAccount, signTransactions } = useWallet();
|
|
22
|
+
|
|
23
|
+
const client = createSynthraClient({
|
|
24
|
+
network: 'testnet', // or 'mainnet'
|
|
25
|
+
payTo: 'CREATOR_ALGORAND_ADDRESS_HERE',
|
|
26
|
+
priceUsdc: 0.10, // Cost per request
|
|
27
|
+
signer: {
|
|
28
|
+
address: activeAccount.address,
|
|
29
|
+
signTransactions: async (txns, indexesToSign) => {
|
|
30
|
+
return signTransactions(txns, indexesToSign);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Just fetch normally! The SDK handles the 402 payment flow under the hood
|
|
36
|
+
const response = await client.fetch('https://api.weather.in/cities');
|
|
37
|
+
const data = await response.json();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## For Autonomous Agents (Node.js)
|
|
41
|
+
|
|
42
|
+
Agents can consume APIs entirely autonomously by signing the USDC payment with their own private key. No human intervention is needed.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { createSynthraClient } from 'synthra-x402/client'
|
|
46
|
+
import algosdk from 'algosdk';
|
|
47
|
+
|
|
48
|
+
const secretKey = Buffer.from(process.env.AGENT_PRIVATE_KEY, "base64");
|
|
49
|
+
const address = algosdk.encodeAddress(secretKey.slice(32));
|
|
50
|
+
|
|
51
|
+
const agentClient = createSynthraClient({
|
|
52
|
+
network: 'testnet', // or 'mainnet'
|
|
53
|
+
payTo: 'CREATOR_ALGORAND_ADDRESS_HERE',
|
|
54
|
+
priceUsdc: 0.10,
|
|
55
|
+
signer: {
|
|
56
|
+
address,
|
|
57
|
+
signTransactions: async (txns, indexesToSign) => {
|
|
58
|
+
return txns.map((txn, i) => {
|
|
59
|
+
if (indexesToSign && !indexesToSign.includes(i)) return null;
|
|
60
|
+
const decoded = algosdk.decodeUnsignedTransaction(txn);
|
|
61
|
+
return algosdk.signTransaction(decoded, secretKey).blob;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// The agent can now consume external APIs from the marketplace autonomously
|
|
68
|
+
const response = await agentClient.fetch('https://api.weather.in/cities');
|
|
69
|
+
const data = await response.json();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Protecting your own APIs (Express.js Middleware)
|
|
73
|
+
|
|
74
|
+
You can also use this package to easily protect your own Express.js API endpoints and monetize them on the Synthra Marketplace.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import express from 'express';
|
|
78
|
+
import { synthraApiAuth } from 'synthra-x402/server';
|
|
79
|
+
|
|
80
|
+
const app = express();
|
|
81
|
+
|
|
82
|
+
// Protect the endpoint requiring 0.10 USDC per request
|
|
83
|
+
app.use("/api/premium", synthraApiAuth({
|
|
84
|
+
network: "testnet",
|
|
85
|
+
priceUsdc: 0.10,
|
|
86
|
+
payTo: "YOUR_ALGORAND_WALLET_ADDRESS"
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
app.get("/api/premium", (req, res) => {
|
|
90
|
+
res.json({ secret: "This data was paid for using x402!" });
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
app.listen(8080);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## How it works (Prepaid Tokens)
|
|
97
|
+
|
|
98
|
+
1. You use the `synthra-x402` SDK to make a request to a marketplace endpoint.
|
|
99
|
+
2. The server responds with a `402 Payment Required` invoice.
|
|
100
|
+
3. The SDK intercepts the response and automatically handles purchasing a prepaid token (Macaroon) via your wallet or private key.
|
|
101
|
+
4. You only pay network fees ONCE. Subsequent requests simply mathematically decrement your token budget off-chain, enabling thousands of requests per second with zero blockchain latency.
|