# @alchemy/x402 中文文档

规范地址：https://npmcn.com/package/%40alchemy/x402/

版本：0\.6\.6
npm 元数据更新时间：2026-03-11T19:22:11.671Z
中文译文更新时间：2026-11-06T09:40:41.310Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40alchemy/x402
原文对照：https://npmcn.com/package/%40alchemy/x402/?lang=original
项目主页：https://github.com/alchemyplatform/alchemy-x402#readme
源码仓库：https://github.com/alchemyplatform/alchemy-x402

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：48.5 kB，10 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=20\.0\.0
- 运行依赖：12 个
- 首次发布：2026-02-25T19:12:01.425Z
- 最新版本发布：2026-03-11T19:22:11.317Z
- 近 30 天下载量：3555（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 7 个）

## 中文简介

Alchemy x402 认证和付款的 CLI 和库

## 安装

```sh
npm install @alchemy/x402
```

也可以使用：`pnpm add @alchemy/x402`、`yarn add @alchemy/x402`、`bun add @alchemy/x402`

## README

# @alchemy/x402 (中文(简体) )

用于Alchemy x402认证和付款的CLI和库. Handles SIWE (Sign-In With Ethereum) 和 SIWS (Sig-In With Solana) 认证, 加上对Alchemy代理网关的每个请求付款x402 .

## 安装

```bash
npm install @alchemy/x402
# or
yarn add @alchemy/x402
# or
pnpm add @alchemy/x402
# or
bun add @alchemy/x402
```

## 国 际

所有命令接受一个 `--architecture` 要在 EVM 和 SVM (索拉纳) 之间切换的旗帜 。 默认为 `evm`。 。 。 .

### EVM (Ethereum, Base等) (英语)

```bash
# Generate a new EVM wallet
npx @alchemy/x402 wallet generate

# Import an existing wallet (accepts hex key or path to a key file)
npx @alchemy/x402 wallet import --private-key 0xac09...
npx @alchemy/x402 wallet import --private-key /path/to/keyfile

# Generate a SIWE token
npx @alchemy/x402 sign --private-key /path/to/keyfile --expires-after 1h

# Create an x402 payment from a PAYMENT-REQUIRED header
npx @alchemy/x402 pay --private-key /path/to/keyfile --payment-required <header>
```

### SVM(索拉纳语)

```bash
# Generate a new Solana wallet
npx @alchemy/x402 wallet generate --architecture svm

# Import an existing wallet (accepts base58 key, JSON byte array, or path to a key file)
npx @alchemy/x402 wallet import --private-key <base58-key> --architecture svm
npx @alchemy/x402 wallet import --private-key /path/to/keyfile --architecture svm

# Generate a SIWS token
npx @alchemy/x402 sign --private-key /path/to/keyfile --expires-after 1h --architecture svm

# Create an x402 payment from a PAYMENT-REQUIRED header
npx @alchemy/x402 pay --private-key /path/to/keyfile --payment-required <header> --architecture svm
```

## 图书馆

```ts
import {
  // EVM
  signSiwe,
  generateWallet,
  getWalletAddress,
  createPayment,
  buildX402Client,
  // SVM (Solana)
  signSiws,
  generateSolanaWallet,
  getSolanaWalletAddress,
  createSolanaPayment,
  buildSolanaX402Client,
  // Enum
  Architecture,
} from "@alchemy/x402";
```

### 生成钱包

```ts
// EVM
const evmWallet = generateWallet();
// { privateKey: "0x...", address: "0x..." }

const evmAddress = getWalletAddress("0x<private-key>");

// SVM (Solana)
const solWallet = generateSolanaWallet();
// { privateKey: "<base58>", address: "<base58>" }

const solAddress = getSolanaWalletAddress("<base58-secret-key>");
```

### 签名认证符

```ts
// EVM — SIWE (Sign-In With Ethereum)
const siweToken = await signSiwe({
  privateKey: "0x<private-key>",
  expiresAfter: "1h", // optional, default "1h"
});

// SVM — SIWS (Sign-In With Solana)
const siwsToken = await signSiws({
  privateKey: "<base58-secret-key>",
  expiresAfter: "1h", // optional, default "1h"
});
```

### 创建一个 x402 付款

```ts
// EVM
const evmPayment = await createPayment({
  privateKey: "0x<private-key>",
  paymentRequiredHeader: "<base64-encoded PAYMENT-REQUIRED header>",
});

// SVM (Solana)
const solPayment = await createSolanaPayment({
  privateKey: "<base58-secret-key>",
  paymentRequiredHeader: "<base64-encoded PAYMENT-REQUIRED header>",
});
```

### 使用@ x402/ fatch

402自动付款处理的全程请求协调,使用 `buildX402Client` 与 `@x402/fetch`数字 :

```ts
import { buildX402Client, signSiwe } from "@alchemy/x402";
import { wrapFetchWithPayment } from "@x402/fetch";

const privateKey = "0x<private-key>";
const client = buildX402Client(privateKey);
const siweToken = await signSiwe({ privateKey });

// Wrap fetch with SIWE auth
const authedFetch: typeof fetch = async (input, init) => {
  const headers = new Headers(init?.headers);
  headers.set("Authorization", `SIWE ${siweToken}`);
  return fetch(input, { ...init, headers });
};

// Wrap with automatic x402 payment handling
const paymentFetch = wrapFetchWithPayment(authedFetch, client);

const response = await paymentFetch("https://x402.alchemy.com/...");
```

## 私钥输入

### EVM( 视频)

所有 EVM 命令和函数接受私有密钥为:

-   十六进制字符串为 `0x` 前缀 : `0xac09...`
-   原始十六进制字符串 : `ac09...`
-   文件路径 : `/path/to/keyfile`

### SVM(索拉纳语)

所有 SVM 命令和函数接受私有密钥为:

-   密码为64字节的密钥
-   JSON 字节数组( Solana CLI 格式): `[1, 2, 3, ...]`
-   包含任一格式的文件路径 : `/path/to/keyfile`

## 维护者

见 [安保人员](https://npmcn.com/package/%40alchemy/x402/MAINTAINERS.md)。 。 。 .

## 常见问题

### @alchemy/x402 是什么？

Alchemy x402 认证和付款的 CLI 和库

### 如何安装 @alchemy/x402？

在项目目录运行 npm install @alchemy/x402，或使用 pnpm add / yarn add / bun add。它还提供命令行工具 alchemy-x402，可以用 npx alchemy-x402 直接运行。

### @alchemy/x402 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @alchemy/x402 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @alchemy/x402 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=20\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @alchemy/x402 可以商用吗？

@alchemy/x402 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @alchemy/x402 的下载量有多少？

根据 npm 官方下载统计，@alchemy/x402 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,555 次，日均约 119 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @alchemy/x402 还在维护吗？

最新版本 0\.6\.6 发布于 2026/03/12（7 个月前）；至今共发布 7 个版本，其中过去 12 个月发布了 7 个；首次发布于 2026/02/26。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

