@payai/x402-fetch-starter 0.1.3 → 0.1.5
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/NOTICE +1 -1
- package/package.json +2 -2
- package/template/.env-local +3 -2
- package/template/README.md +36 -52
- package/template/index.ts +36 -21
- package/template/package.json +11 -4
- package/template/multi-network-signer.ts +0 -48
package/NOTICE
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
This package includes portions derived from coinbase/x402 (examples/typescript/clients/fetch), Apache-2.0,
|
|
2
|
-
commit
|
|
2
|
+
commit 913fa57453809815d3e10d83392dcc60f111b24a. See LICENSE and upstream LICENSE notices.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payai/x402-fetch-starter",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Create an x402 Fetch client in less than 2 minutes!",
|
|
6
6
|
"type": "module",
|
|
@@ -33,6 +33,6 @@
|
|
|
33
33
|
"url": "https://github.com/PayAINetwork/x402-fetch-starter"
|
|
34
34
|
},
|
|
35
35
|
"config": {
|
|
36
|
-
"x402FetchVersion": "
|
|
36
|
+
"x402FetchVersion": "2.2.0"
|
|
37
37
|
}
|
|
38
38
|
}
|
package/template/.env-local
CHANGED
package/template/README.md
CHANGED
|
@@ -1,75 +1,59 @@
|
|
|
1
|
-
# x402
|
|
1
|
+
# @x402/fetch Example Client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Example client demonstrating how to use `@x402/fetch` to make HTTP requests to endpoints protected by the x402 payment protocol.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { x402Client, wrapFetchWithPayment } from "@x402/fetch";
|
|
7
|
+
import { registerExactEvmScheme } from "@x402/evm/exact/client";
|
|
8
|
+
import { registerExactSvmScheme } from "@x402/svm/exact/client";
|
|
9
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
10
|
+
import { createKeyPairSignerFromBytes } from "@solana/kit";
|
|
11
|
+
import { base58 } from "@scure/base";
|
|
12
|
+
|
|
13
|
+
const client = new x402Client();
|
|
14
|
+
registerExactEvmScheme(client, { signer: privateKeyToAccount(process.env.EVM_PRIVATE_KEY) });
|
|
15
|
+
registerExactSvmScheme(client, { signer: (await createKeyPairSignerFromBytes(base58.decode(process.env.SVM_PRIVATE_KEY))) });
|
|
16
|
+
|
|
17
|
+
const fetchWithPayment = wrapFetchWithPayment(fetch, client);
|
|
18
|
+
|
|
19
|
+
const response = await fetchWithPayment("http://localhost:4021/weather");
|
|
20
|
+
console.log(await response.json());
|
|
21
|
+
```
|
|
4
22
|
|
|
5
23
|
## Prerequisites
|
|
6
24
|
|
|
7
25
|
- Node.js v20+ (install via [nvm](https://github.com/nvm-sh/nvm))
|
|
8
26
|
- pnpm v10 (install via [pnpm.io/installation](https://pnpm.io/installation))
|
|
9
|
-
- A running x402 server (
|
|
10
|
-
-
|
|
27
|
+
- A running x402 server (see [express server example](../../servers/express))
|
|
28
|
+
- Valid EVM and/or SVM private keys for making payments
|
|
11
29
|
|
|
12
30
|
## Setup
|
|
13
31
|
|
|
14
32
|
1. Install and build all packages from the typescript examples root:
|
|
33
|
+
|
|
15
34
|
```bash
|
|
16
35
|
cd ../../
|
|
17
|
-
pnpm install
|
|
18
|
-
pnpm build
|
|
36
|
+
pnpm install && pnpm build
|
|
19
37
|
cd clients/fetch
|
|
20
38
|
```
|
|
21
39
|
|
|
22
|
-
2. Copy `.env-local` to `.env` and add your
|
|
23
|
-
```bash
|
|
24
|
-
cp .env-local .env
|
|
25
|
-
```
|
|
40
|
+
2. Copy `.env-local` to `.env` and add your private keys:
|
|
26
41
|
|
|
27
|
-
3. Start the example client:
|
|
28
42
|
```bash
|
|
29
|
-
|
|
43
|
+
cp .env-local .env
|
|
30
44
|
```
|
|
31
45
|
|
|
32
|
-
|
|
46
|
+
Required environment variables:
|
|
33
47
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
2. Wrap the native fetch function with x402 payment handling
|
|
37
|
-
3. Make a request to a paid endpoint
|
|
38
|
-
4. Handle the response or any errors
|
|
48
|
+
- `EVM_PRIVATE_KEY` - Ethereum private key for EVM payments
|
|
49
|
+
- `SVM_PRIVATE_KEY` - Solana private key for SVM payments
|
|
39
50
|
|
|
40
|
-
|
|
51
|
+
3. Run the client:
|
|
41
52
|
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
import { createWalletClient, http } from "viem";
|
|
45
|
-
import { privateKeyToAccount } from "viem/accounts";
|
|
46
|
-
import { wrapFetchWithPayment } from "x402-fetch";
|
|
47
|
-
import { baseSepolia } from "viem/chains";
|
|
48
|
-
|
|
49
|
-
config();
|
|
50
|
-
|
|
51
|
-
const { RESOURCE_SERVER_URL, PRIVATE_KEY, ENDPOINT_PATH } = process.env;
|
|
52
|
-
|
|
53
|
-
// Create wallet client
|
|
54
|
-
const account = privateKeyToAccount(PRIVATE_KEY as `0x${string}`);
|
|
55
|
-
const client = createWalletClient({
|
|
56
|
-
account,
|
|
57
|
-
transport: http(),
|
|
58
|
-
chain: baseSepolia,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// Wrap fetch with payment handling
|
|
62
|
-
const fetchWithPay = wrapFetchWithPayment(fetch, client);
|
|
63
|
-
|
|
64
|
-
// Make request to paid endpoint
|
|
65
|
-
fetchWithPay(`${RESOURCE_SERVER_URL}${ENDPOINT_PATH}`, {
|
|
66
|
-
method: "GET",
|
|
67
|
-
})
|
|
68
|
-
.then(async response => {
|
|
69
|
-
const body = await response.json();
|
|
70
|
-
console.log(body);
|
|
71
|
-
})
|
|
72
|
-
.catch(error => {
|
|
73
|
-
console.error(error.response?.data?.error);
|
|
74
|
-
});
|
|
53
|
+
```bash
|
|
54
|
+
pnpm start
|
|
75
55
|
```
|
|
56
|
+
|
|
57
|
+
## Next Steps
|
|
58
|
+
|
|
59
|
+
See [Advanced Examples](../advanced/) for builder pattern registration, payment lifecycle hooks, and network preferences.
|
package/template/index.ts
CHANGED
|
@@ -1,37 +1,52 @@
|
|
|
1
1
|
import { config } from "dotenv";
|
|
2
|
-
import {
|
|
2
|
+
import { x402Client, wrapFetchWithPayment, x402HTTPClient } from "@x402/fetch";
|
|
3
|
+
import { registerExactEvmScheme } from "@x402/evm/exact/client";
|
|
4
|
+
import { registerExactSvmScheme } from "@x402/svm/exact/client";
|
|
5
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
6
|
+
import { createKeyPairSignerFromBytes } from "@solana/kit";
|
|
7
|
+
import { base58 } from "@scure/base";
|
|
3
8
|
|
|
4
9
|
config();
|
|
5
10
|
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
if (!baseURL || !privateKey || !endpointPath) {
|
|
12
|
-
console.error("Missing required environment variables");
|
|
13
|
-
process.exit(1);
|
|
14
|
-
}
|
|
11
|
+
const evmPrivateKey = process.env.EVM_PRIVATE_KEY as `0x${string}`;
|
|
12
|
+
const svmPrivateKey = process.env.SVM_PRIVATE_KEY as string;
|
|
13
|
+
const baseURL = process.env.RESOURCE_SERVER_URL || "http://localhost:4021";
|
|
14
|
+
const endpointPath = process.env.ENDPOINT_PATH || "/weather";
|
|
15
|
+
const url = `${baseURL}${endpointPath}`;
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
|
-
*
|
|
18
|
+
* Example demonstrating how to use @x402/fetch to make requests to x402-protected endpoints.
|
|
18
19
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* This uses the helper registration functions from @x402/evm and @x402/svm to register
|
|
21
|
+
* all supported networks for both v1 and v2 protocols.
|
|
22
|
+
*
|
|
23
|
+
* Required environment variables:
|
|
24
|
+
* - EVM_PRIVATE_KEY: The private key of the EVM signer
|
|
25
|
+
* - SVM_PRIVATE_KEY: The private key of the SVM signer
|
|
23
26
|
*/
|
|
24
27
|
async function main(): Promise<void> {
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
+
const evmSigner = privateKeyToAccount(evmPrivateKey);
|
|
29
|
+
const svmSigner = await createKeyPairSignerFromBytes(base58.decode(svmPrivateKey));
|
|
30
|
+
|
|
31
|
+
const client = new x402Client();
|
|
32
|
+
registerExactEvmScheme(client, { signer: evmSigner });
|
|
33
|
+
registerExactSvmScheme(client, { signer: svmSigner });
|
|
34
|
+
|
|
35
|
+
const fetchWithPayment = wrapFetchWithPayment(fetch, client);
|
|
28
36
|
|
|
37
|
+
console.log(`Making request to: ${url}\n`);
|
|
29
38
|
const response = await fetchWithPayment(url, { method: "GET" });
|
|
30
39
|
const body = await response.json();
|
|
31
|
-
console.log(body);
|
|
40
|
+
console.log("Response body:", body);
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
|
|
42
|
+
if (response.ok) {
|
|
43
|
+
const paymentResponse = new x402HTTPClient(client).getPaymentSettleResponse(name =>
|
|
44
|
+
response.headers.get(name),
|
|
45
|
+
);
|
|
46
|
+
console.log("\nPayment response:", JSON.stringify(paymentResponse, null, 2));
|
|
47
|
+
} else {
|
|
48
|
+
console.log(`\nNo payment settled (response status: ${response.status})`);
|
|
49
|
+
}
|
|
35
50
|
}
|
|
36
51
|
|
|
37
52
|
main().catch(error => {
|
package/template/package.json
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "fetch-client-example",
|
|
2
|
+
"name": "@x402/fetch-client-example",
|
|
3
3
|
"private": true,
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"
|
|
7
|
-
"dev
|
|
6
|
+
"start": "tsx index.ts",
|
|
7
|
+
"dev": "tsx index.ts builder-pattern",
|
|
8
|
+
"dev:hooks": "tsx index.ts hooks",
|
|
9
|
+
"dev:mechanism-helper-registration": "tsx index.ts mechanism-helper-registration",
|
|
8
10
|
"format": "prettier -c .prettierrc --write \"**/*.{ts,js,cjs,json,md}\"",
|
|
9
11
|
"format:check": "prettier -c .prettierrc --check \"**/*.{ts,js,cjs,json,md}\"",
|
|
10
12
|
"lint": "eslint . --ext .ts --fix",
|
|
11
13
|
"lint:check": "eslint . --ext .ts"
|
|
12
14
|
},
|
|
13
15
|
"dependencies": {
|
|
16
|
+
"@scure/base": "^1.2.6",
|
|
17
|
+
"@solana/kit": "^2.1.1",
|
|
18
|
+
"@x402/evm": "^2.2.0",
|
|
19
|
+
"@x402/fetch": "^2.2.0",
|
|
20
|
+
"@x402/svm": "^2.2.0",
|
|
14
21
|
"dotenv": "^16.4.7",
|
|
15
|
-
"
|
|
22
|
+
"viem": "^2.39.0"
|
|
16
23
|
},
|
|
17
24
|
"devDependencies": {
|
|
18
25
|
"@eslint/js": "^9.24.0",
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { config } from "dotenv";
|
|
2
|
-
import {
|
|
3
|
-
decodeXPaymentResponse,
|
|
4
|
-
wrapFetchWithPayment,
|
|
5
|
-
createSigner,
|
|
6
|
-
type Hex,
|
|
7
|
-
type MultiNetworkSigner,
|
|
8
|
-
} from "x402-fetch";
|
|
9
|
-
|
|
10
|
-
config();
|
|
11
|
-
|
|
12
|
-
const evmPrivateKey = process.env.EVM_PRIVATE_KEY as Hex;
|
|
13
|
-
const svmPrivateKey = process.env.SVM_PRIVATE_KEY as string;
|
|
14
|
-
const baseURL = process.env.RESOURCE_SERVER_URL as string; // e.g. https://example.com
|
|
15
|
-
const endpointPath = process.env.ENDPOINT_PATH as string; // e.g. /weather
|
|
16
|
-
const url = `${baseURL}${endpointPath}`; // e.g. https://example.com/weather
|
|
17
|
-
|
|
18
|
-
if (!baseURL || !evmPrivateKey || !svmPrivateKey || !endpointPath) {
|
|
19
|
-
console.error("Missing required environment variables");
|
|
20
|
-
process.exit(1);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* This example shows how to use the x402-fetch package to make a request to a resource server that requires a payment.
|
|
25
|
-
*
|
|
26
|
-
* To run this example, you need to set the following environment variables:
|
|
27
|
-
* - PRIVATE_KEY: The private key of the signer
|
|
28
|
-
* - RESOURCE_SERVER_URL: The URL of the resource server
|
|
29
|
-
* - ENDPOINT_PATH: The path of the endpoint to call on the resource server
|
|
30
|
-
*/
|
|
31
|
-
async function main(): Promise<void> {
|
|
32
|
-
const evmSigner = await createSigner("base-sepolia", evmPrivateKey);
|
|
33
|
-
const svmSigner = await createSigner("solana-devnet", svmPrivateKey);
|
|
34
|
-
const signer = { evm: evmSigner, svm: svmSigner } as MultiNetworkSigner;
|
|
35
|
-
const fetchWithPayment = wrapFetchWithPayment(fetch, signer);
|
|
36
|
-
|
|
37
|
-
const response = await fetchWithPayment(url, { method: "GET" });
|
|
38
|
-
const body = await response.json();
|
|
39
|
-
console.log(body);
|
|
40
|
-
|
|
41
|
-
const paymentResponse = decodeXPaymentResponse(response.headers.get("x-payment-response")!);
|
|
42
|
-
console.log(paymentResponse);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
main().catch(error => {
|
|
46
|
-
console.error(error?.response?.data?.error ?? error);
|
|
47
|
-
process.exit(1);
|
|
48
|
-
});
|