@payai/x402-fetch-starter 0.1.1 → 0.1.3

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 CHANGED
@@ -1,2 +1,2 @@
1
1
  This package includes portions derived from coinbase/x402 (examples/typescript/clients/fetch), Apache-2.0,
2
- commit 0dfd3e683f1563543bd62a3ad84cbc6851d4054b. See LICENSE and upstream LICENSE notices.
2
+ commit f3d44638965794ff9b550cdbba1f8197ab4a8df5. See LICENSE and upstream LICENSE notices.
package/bin/create.js CHANGED
@@ -10,7 +10,7 @@ const repoRoot = path.join(__dirname, "..");
10
10
  const templateDir = path.join(repoRoot, "template");
11
11
 
12
12
  const nameArg = process.argv[2];
13
- const projectName = nameArg && !nameArg.startsWith("-") ? nameArg : "x402-fetch-app";
13
+ const projectName = nameArg && !nameArg.startsWith("-") ? nameArg : "x402-fetch-client";
14
14
  const targetDir = path.resolve(process.cwd(), projectName);
15
15
 
16
16
  if (fs.existsSync(targetDir) && fs.readdirSync(targetDir).length) {
@@ -78,4 +78,4 @@ After install, open the main file (e.g., index.ts) to see how the example works.
78
78
  Docs: https://docs.payai.network
79
79
 
80
80
  Happy building 🏗️
81
- `);
81
+ `);
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@payai/x402-fetch-starter",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
- "description": "Create an x402 Fetch client without cloning the monorepo.",
5
+ "description": "Create an x402 Fetch client in less than 2 minutes!",
6
6
  "type": "module",
7
7
  "bin": {
8
8
  "create-x402-fetch": "bin/create.js"
@@ -33,6 +33,6 @@
33
33
  "url": "https://github.com/PayAINetwork/x402-fetch-starter"
34
34
  },
35
35
  "config": {
36
- "x402FetchVersion": "0.5.1"
36
+ "x402FetchVersion": "0.6.6"
37
37
  }
38
- }
38
+ }
package/template/index.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import { config } from "dotenv";
2
- import { Hex } from "viem";
3
- import { privateKeyToAccount } from "viem/accounts";
4
- import { decodeXPaymentResponse, wrapFetchWithPayment } from "x402-fetch";
2
+ import { decodeXPaymentResponse, wrapFetchWithPayment, createSigner, type Hex } from "x402-fetch";
5
3
 
6
4
  config();
7
5
 
8
- const privateKey = process.env.PRIVATE_KEY as Hex;
6
+ const privateKey = process.env.PRIVATE_KEY as Hex | string;
9
7
  const baseURL = process.env.RESOURCE_SERVER_URL as string; // e.g. https://example.com
10
8
  const endpointPath = process.env.ENDPOINT_PATH as string; // e.g. /weather
11
9
  const url = `${baseURL}${endpointPath}`; // e.g. https://example.com/weather
@@ -15,20 +13,28 @@ if (!baseURL || !privateKey || !endpointPath) {
15
13
  process.exit(1);
16
14
  }
17
15
 
18
- const account = privateKeyToAccount(privateKey);
16
+ /**
17
+ * This example shows how to use the x402-fetch package to make a request to a resource server that requires a payment.
18
+ *
19
+ * To run this example, you need to set the following environment variables:
20
+ * - PRIVATE_KEY: The private key of the signer
21
+ * - RESOURCE_SERVER_URL: The URL of the resource server
22
+ * - ENDPOINT_PATH: The path of the endpoint to call on the resource server
23
+ */
24
+ async function main(): Promise<void> {
25
+ // const signer = await createSigner("solana-devnet", privateKey); // uncomment for solana
26
+ const signer = await createSigner("base-sepolia", privateKey);
27
+ const fetchWithPayment = wrapFetchWithPayment(fetch, signer);
19
28
 
20
- const fetchWithPayment = wrapFetchWithPayment(fetch, account);
29
+ const response = await fetchWithPayment(url, { method: "GET" });
30
+ const body = await response.json();
31
+ console.log(body);
21
32
 
22
- fetchWithPayment(url, {
23
- method: "GET",
24
- })
25
- .then(async response => {
26
- const body = await response.json();
27
- console.log(body);
33
+ const paymentResponse = decodeXPaymentResponse(response.headers.get("x-payment-response")!);
34
+ console.log(paymentResponse);
35
+ }
28
36
 
29
- const paymentResponse = decodeXPaymentResponse(response.headers.get("x-payment-response")!);
30
- console.log(paymentResponse);
31
- })
32
- .catch(error => {
33
- console.error(error.response?.data?.error);
34
- });
37
+ main().catch(error => {
38
+ console.error(error?.response?.data?.error ?? error);
39
+ process.exit(1);
40
+ });
@@ -0,0 +1,48 @@
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
+ });
@@ -4,25 +4,24 @@
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "tsx index.ts",
7
+ "dev:multi-network-signer": "tsx multi-network-signer.ts",
7
8
  "format": "prettier -c .prettierrc --write \"**/*.{ts,js,cjs,json,md}\"",
8
9
  "format:check": "prettier -c .prettierrc --check \"**/*.{ts,js,cjs,json,md}\"",
9
10
  "lint": "eslint . --ext .ts --fix",
10
11
  "lint:check": "eslint . --ext .ts"
11
12
  },
12
13
  "dependencies": {
13
- "axios": "^1.7.9",
14
14
  "dotenv": "^16.4.7",
15
- "viem": "^2.21.26",
16
- "x402-fetch": "^0.5.1"
15
+ "x402-fetch": "^0.6.6"
17
16
  },
18
17
  "devDependencies": {
19
18
  "@eslint/js": "^9.24.0",
20
- "eslint": "^9.24.0",
21
- "eslint-plugin-jsdoc": "^50.6.9",
22
- "eslint-plugin-prettier": "^5.2.6",
23
19
  "@typescript-eslint/eslint-plugin": "^8.29.1",
24
20
  "@typescript-eslint/parser": "^8.29.1",
21
+ "eslint": "^9.24.0",
25
22
  "eslint-plugin-import": "^2.31.0",
23
+ "eslint-plugin-jsdoc": "^50.6.9",
24
+ "eslint-plugin-prettier": "^5.2.6",
26
25
  "prettier": "3.5.2",
27
26
  "tsx": "^4.7.0",
28
27
  "typescript": "^5.3.0"