@alchemy/x402

中文文档
0.6.6发布于 2026/03/12MITTypeScriptESM + CJS构建来源已验证近 30 天下载 3555

CLI and library for Alchemy x402 authentication and payments

原始使用文档
文档目录13 节
  1. Install
  2. CLI
  3. EVM (Ethereum, Base, etc.)
  4. SVM (Solana)
  5. Library
  6. Generate a wallet
  7. Sign an authentication token
  8. Create an x402 payment
  9. Use with @x402/fetch
  10. Private key input
  11. EVM
  12. SVM (Solana)
  13. For maintainers

@alchemy/x402

CLI and library for Alchemy x402 authentication and payments. Handles SIWE (Sign-In With Ethereum) and SIWS (Sign-In With Solana) auth, plus x402 per-request payments for the Alchemy agentic gateway.

Install

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

CLI

All commands accept an --architecture flag to switch between EVM and SVM (Solana). Defaults to evm.

EVM (Ethereum, Base, etc.)

# 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 (Solana)

# 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

Library

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

Generate a wallet

// 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>");

Sign an authentication token

// 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"
});

Create an x402 payment

// 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>",
});

Use with @x402/fetch

For full request orchestration with automatic 402 payment handling, use buildX402Client with @x402/fetch:

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/...");

Private key input

EVM

All EVM commands and functions accept private keys as:

  • Hex string with 0x prefix: 0xac09...
  • Raw hex string: ac09...
  • File path: /path/to/keyfile

SVM (Solana)

All SVM commands and functions accept private keys as:

  • Base58-encoded 64-byte secret key
  • JSON byte array (Solana CLI format): [1, 2, 3, ...]
  • File path containing either format: /path/to/keyfile

For maintainers

See MAINTAINERS.md.

版本与发布记录

@alchemy/x402 共发布 7 个版本,首次发布于 2026/02/26;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.6latest2026/03/127 个月前
0.6.42026/03/127 个月前
0.6.32026/03/127 个月前
0.5.02026/03/107 个月前
0.4.02026/02/268 个月前
0.3.02026/02/268 个月前

关于 @alchemy/x402 的常见问题

@alchemy/x402 是什么?

CLI and library for Alchemy x402 authentication and payments

如何安装 @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 和提交记录判断。