@payai/x402-axios-starter 0.1.2 → 2.3.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.
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 ed2438706202fc45046405ba147c5d555393c82f. See LICENSE and upstream LICENSE notices.
2
+ commit 6a99ba028b0d657122db8a06328cd432bdd8c7c0. See LICENSE and upstream LICENSE notices.
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@payai/x402-axios-starter",
3
- "version": "0.1.2",
3
+ "version": "2.3.0",
4
4
  "private": false,
5
+ "publishConfig": {
6
+ "access": "public",
7
+ "registry": "https://registry.npmjs.org"
8
+ },
5
9
  "description": "Create an x402 Axios client in less than 2 minutes!",
6
10
  "type": "module",
7
11
  "bin": {
@@ -33,7 +37,6 @@
33
37
  "url": "https://github.com/PayAINetwork/x402-axios-starter"
34
38
  },
35
39
  "config": {
36
- "x402AxiosVersion": "0.5.1"
40
+ "x402AxiosVersion": "2.3.0"
37
41
  }
38
42
  }
39
-
@@ -1,3 +1,4 @@
1
+ EVM_PRIVATE_KEY=
2
+ SVM_PRIVATE_KEY=
1
3
  RESOURCE_SERVER_URL=http://localhost:4021
2
- ENDPOINT_PATH=/weather
3
- PRIVATE_KEY=
4
+ ENDPOINT_PATH=/weather
@@ -1,80 +1,56 @@
1
- # x402-axios Example Client
1
+ # @x402/axios Example Client
2
2
 
3
- This is an example client that demonstrates how to use the `x402-axios` package to make HTTP requests to endpoints protected by the x402 payment protocol.
3
+ Example client demonstrating how to use `@x402/axios` to make HTTP requests to endpoints protected by the x402 payment protocol.
4
+
5
+ ```typescript
6
+ import { x402Client, wrapAxiosWithPayment } from "@x402/axios";
7
+ import { registerExactEvmScheme } from "@x402/evm/exact/client";
8
+ import { privateKeyToAccount } from "viem/accounts";
9
+ import axios from "axios";
10
+
11
+ const client = new x402Client();
12
+ registerExactEvmScheme(client, { signer: privateKeyToAccount(process.env.EVM_PRIVATE_KEY) });
13
+
14
+ const api = wrapAxiosWithPayment(axios.create(), client);
15
+
16
+ const response = await api.get("http://localhost:4021/weather");
17
+ console.log(response.data);
18
+ ```
4
19
 
5
20
  ## Prerequisites
6
21
 
7
22
  - Node.js v20+ (install via [nvm](https://github.com/nvm-sh/nvm))
8
23
  - pnpm v10 (install via [pnpm.io/installation](https://pnpm.io/installation))
9
- - A running x402 server (you can use the example express server at `examples/typescript/servers/express`)
10
- - A valid Ethereum private key for making payments
24
+ - A running x402 server (see [express server example](../../servers/express))
25
+ - Valid EVM and/or SVM private keys for making payments
11
26
 
12
27
  ## Setup
13
28
 
14
29
  1. Install and build all packages from the typescript examples root:
30
+
15
31
  ```bash
16
32
  cd ../../
17
- pnpm install
18
- pnpm build
33
+ pnpm install && pnpm build
19
34
  cd clients/axios
20
35
  ```
21
36
 
22
- 2. Copy `.env-local` to `.env` and add your Ethereum private key (remember it should have USDC on Base Sepolia, which you can provision using the [CDP Faucet](https://portal.cdp.coinbase.com/products/faucet)):
23
- ```bash
24
- cp .env-local .env
25
- ```
37
+ 2. Copy `.env-local` to `.env` and add your private keys:
26
38
 
27
- 3. Start the example client (remember you need to be running a server locally or point at an endpoint):
28
39
  ```bash
29
- pnpm dev
40
+ cp .env-local .env
30
41
  ```
31
42
 
32
- ## How It Works
43
+ Required environment variables:
33
44
 
34
- The example demonstrates how to:
35
- 1. Create a wallet client using viem
36
- 2. Create an Axios instance with x402 payment handling
37
- 3. Make a request to a paid endpoint
38
- 4. Handle the response or any errors
45
+ - `EVM_PRIVATE_KEY` - Ethereum private key for EVM payments
46
+ - `SVM_PRIVATE_KEY` - Solana private key for SVM payments
39
47
 
40
- ## Example Code
48
+ 3. Run the client:
41
49
 
42
- ```typescript
43
- import { config } from "dotenv";
44
- import { createWalletClient, http, publicActions } from "viem";
45
- import { privateKeyToAccount } from "viem/accounts";
46
- import { withPaymentInterceptor } from "x402-axios";
47
- import axios from "axios";
48
- import { baseSepolia } from "viem/chains";
49
-
50
- config();
51
-
52
- const { RESOURCE_SERVER_URL, PRIVATE_KEY, ENDPOINT_PATH } = process.env;
53
-
54
- // Create wallet client
55
- const account = privateKeyToAccount(PRIVATE_KEY as "0x${string}");
56
- const client = createWalletClient({
57
- account,
58
- transport: http(),
59
- chain: baseSepolia,
60
- }).extend(publicActions);
61
-
62
- // Create Axios instance with payment handling
63
- const api = withPaymentInterceptor(
64
- axios.create({
65
- baseURL: RESOURCE_SERVER_URL,
66
- }),
67
- client
68
- );
69
-
70
- // Make request to paid endpoint
71
- api
72
- .get(ENDPOINT_PATH)
73
- .then(response => {
74
- console.log(response.headers);
75
- console.log(response.data);
76
- })
77
- .catch(error => {
78
- console.error(error.response?.data?.error);
79
- });
50
+ ```bash
51
+ pnpm start
80
52
  ```
53
+
54
+ ## Next Steps
55
+
56
+ See [Advanced Examples](../advanced/) for builder pattern registration, payment lifecycle hooks, and network preferences.
package/template/index.ts CHANGED
@@ -1,43 +1,56 @@
1
- import axios from "axios";
2
1
  import { config } from "dotenv";
3
- import { withPaymentInterceptor, decodeXPaymentResponse, createSigner, type Hex } from "x402-axios";
2
+ import { x402Client, wrapAxiosWithPayment, x402HTTPClient } from "@x402/axios";
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";
8
+ import axios from "axios";
4
9
 
5
10
  config();
6
11
 
7
- const privateKey = process.env.PRIVATE_KEY as Hex | string;
8
- const baseURL = process.env.RESOURCE_SERVER_URL as string; // e.g. https://example.com
9
- const endpointPath = process.env.ENDPOINT_PATH as string; // e.g. /weather
10
-
11
- if (!baseURL || !privateKey || !endpointPath) {
12
- console.error("Missing required environment variables");
13
- process.exit(1);
14
- }
12
+ const evmPrivateKey = process.env.EVM_PRIVATE_KEY as `0x${string}`;
13
+ const svmPrivateKey = process.env.SVM_PRIVATE_KEY as string;
14
+ const baseURL = process.env.RESOURCE_SERVER_URL || "http://localhost:4021";
15
+ const endpointPath = process.env.ENDPOINT_PATH || "/weather";
16
+ const url = `${baseURL}${endpointPath}`;
15
17
 
16
18
  /**
17
- * This example shows how to use the x402-axios package to make a request to a resource server that requires a payment.
19
+ * Example demonstrating how to use @x402/axios to make requests to x402-protected endpoints.
18
20
  *
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
21
+ * This uses the helper registration functions from @x402/evm and @x402/svm to register
22
+ * all supported networks for both v1 and v2 protocols.
23
23
  *
24
+ * Required environment variables:
25
+ * - EVM_PRIVATE_KEY: The private key of the EVM signer
26
+ * - SVM_PRIVATE_KEY: The private key of the SVM signer
24
27
  */
25
28
  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);
29
+ const evmSigner = privateKeyToAccount(evmPrivateKey);
30
+ const svmSigner = await createKeyPairSignerFromBytes(base58.decode(svmPrivateKey));
31
+
32
+ const client = new x402Client();
33
+ registerExactEvmScheme(client, { signer: evmSigner });
34
+ registerExactSvmScheme(client, { signer: svmSigner });
35
+
36
+ const api = wrapAxiosWithPayment(axios.create(), client);
37
+
38
+ console.log(`Making request to: ${url}\n`);
39
+ const response = await api.get(url);
40
+ const body = response.data;
41
+ console.log("Response body:", body);
42
+
43
+ if (response.status < 400) {
44
+ const paymentResponse = new x402HTTPClient(client).getPaymentSettleResponse(
45
+ name => response.headers[name.toLowerCase()],
46
+ );
47
+ console.log("\nPayment response:", paymentResponse);
48
+ } else {
49
+ console.log(`\nNo payment settled (response status: ${response.status})`);
50
+ }
41
51
  }
42
52
 
43
- main();
53
+ main().catch(error => {
54
+ console.error(error?.response?.data?.error ?? error);
55
+ process.exit(1);
56
+ });
@@ -1,19 +1,26 @@
1
1
  {
2
- "name": "axios-client-example",
2
+ "name": "@x402/axios-client-example",
3
3
  "private": true,
4
4
  "type": "module",
5
5
  "scripts": {
6
- "dev": "tsx index.ts",
7
- "dev:multi-network-signer": "tsx multi-network-signer.ts",
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": {
14
- "axios": "^1.7.9",
15
- "dotenv": "^16.5.0",
16
- "x402-axios": "^0.6.0"
16
+ "@scure/base": "^1.2.6",
17
+ "@solana/kit": "^2.1.1",
18
+ "@x402/axios": "^2.3.0",
19
+ "@x402/evm": "^2.3.0",
20
+ "@x402/svm": "^2.3.0",
21
+ "axios": "^1.13.2",
22
+ "dotenv": "^16.4.7",
23
+ "viem": "^2.39.0"
17
24
  },
18
25
  "devDependencies": {
19
26
  "@eslint/js": "^9.24.0",
@@ -1,51 +0,0 @@
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();