@payai/x402-axios-starter 0.1.1 → 0.1.2

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/axios), Apache-2.0,
2
- commit 0dfd3e683f1563543bd62a3ad84cbc6851d4054b. See LICENSE and upstream LICENSE notices.
2
+ commit ed2438706202fc45046405ba147c5d555393c82f. See LICENSE and upstream LICENSE notices.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payai/x402-axios-starter",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "private": false,
5
5
  "description": "Create an x402 Axios client in less than 2 minutes!",
6
6
  "type": "module",
package/template/index.ts CHANGED
@@ -1,12 +1,10 @@
1
1
  import axios from "axios";
2
2
  import { config } from "dotenv";
3
- import { Hex } from "viem";
4
- import { privateKeyToAccount } from "viem/accounts";
5
- import { withPaymentInterceptor, decodeXPaymentResponse } from "x402-axios";
3
+ import { withPaymentInterceptor, decodeXPaymentResponse, createSigner, type Hex } from "x402-axios";
6
4
 
7
5
  config();
8
6
 
9
- const privateKey = process.env.PRIVATE_KEY as Hex;
7
+ const privateKey = process.env.PRIVATE_KEY as Hex | string;
10
8
  const baseURL = process.env.RESOURCE_SERVER_URL as string; // e.g. https://example.com
11
9
  const endpointPath = process.env.ENDPOINT_PATH as string; // e.g. /weather
12
10
 
@@ -15,23 +13,31 @@ if (!baseURL || !privateKey || !endpointPath) {
15
13
  process.exit(1);
16
14
  }
17
15
 
18
- const account = privateKeyToAccount(privateKey);
19
-
20
- const api = withPaymentInterceptor(
21
- axios.create({
22
- baseURL,
23
- }),
24
- account,
25
- );
26
-
27
- api
28
- .get(endpointPath)
29
- .then(response => {
30
- console.log(response.data);
31
-
32
- const paymentResponse = decodeXPaymentResponse(response.headers["x-payment-response"]);
33
- console.log(paymentResponse);
34
- })
35
- .catch(error => {
36
- console.error(error);
37
- });
16
+ /**
17
+ * This example shows how to use the x402-axios 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
+ */
25
+ async function main(): Promise<void> {
26
+ // const signer = await createSigner("solana-devnet", privateKey); // uncomment for solana
27
+ const signer = await createSigner("base-sepolia", privateKey);
28
+
29
+ const api = withPaymentInterceptor(
30
+ axios.create({
31
+ baseURL,
32
+ }),
33
+ signer,
34
+ );
35
+
36
+ const response = await api.get(endpointPath);
37
+ console.log(response.data);
38
+
39
+ const paymentResponse = decodeXPaymentResponse(response.headers["x-payment-response"]);
40
+ console.log(paymentResponse);
41
+ }
42
+
43
+ main();
@@ -0,0 +1,51 @@
1
+ import axios from "axios";
2
+ import { config } from "dotenv";
3
+ import {
4
+ withPaymentInterceptor,
5
+ decodeXPaymentResponse,
6
+ createSigner,
7
+ type Hex,
8
+ MultiNetworkSigner,
9
+ } from "x402-axios";
10
+
11
+ config();
12
+
13
+ const evmPrivateKey = process.env.EVM_PRIVATE_KEY as Hex;
14
+ const svmPrivateKey = process.env.SVM_PRIVATE_KEY as string;
15
+ const baseURL = process.env.RESOURCE_SERVER_URL as string; // e.g. https://example.com
16
+ const endpointPath = process.env.ENDPOINT_PATH as string; // e.g. /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-axios 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
+ */
32
+ async function main(): Promise<void> {
33
+ const evmSigner = await createSigner("base-sepolia", evmPrivateKey);
34
+ const svmSigner = await createSigner("solana-devnet", svmPrivateKey);
35
+ const signer = { evm: evmSigner, svm: svmSigner } as MultiNetworkSigner;
36
+
37
+ const api = withPaymentInterceptor(
38
+ axios.create({
39
+ baseURL,
40
+ }),
41
+ signer,
42
+ );
43
+
44
+ const response = await api.get(endpointPath);
45
+ console.log(response.data);
46
+
47
+ const paymentResponse = decodeXPaymentResponse(response.headers["x-payment-response"]);
48
+ console.log(paymentResponse);
49
+ }
50
+
51
+ main();
@@ -4,6 +4,7 @@
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",
@@ -12,8 +13,7 @@
12
13
  "dependencies": {
13
14
  "axios": "^1.7.9",
14
15
  "dotenv": "^16.5.0",
15
- "viem": "^2.21.26",
16
- "x402-axios": "^0.5.1"
16
+ "x402-axios": "^0.6.0"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@eslint/js": "^9.24.0",