mppx

中文文档
0.17.1发布于 2026/11/04MITTypeScriptES Module构建来源已验证
原始使用文档
文档目录11 节
  1. Documentation
  2. Install
  3. Quick Start
  4. Server
  5. Client
  6. Examples
  7. CLI
  8. Payments Proxy
  9. Protocol
  10. License
  11. Accepted Tempo currencies
mppx

TypeScript SDK for the Machine Payments Protocol

Documentation · Install · Quick Start · Examples · CLI · Payments Proxy · Protocol

Version MIT License


Documentation

Full documentation, API reference, and guides are available at mpp.dev/sdk/typescript.

Contributors changing Tempo sessions should read the session design before altering credential, recovery, accounting, or transport behavior.

Install

npm i mppx

Quick Start

Server

import { Mppx, tempo } from 'mppx/server'

const mppx = Mppx.create({
  methods: [
    tempo({
      recipient: '0x742d35Cc6634c0532925a3b844bC9e7595F8fE00',
    }),
  ],
  secretKey: process.env.MPP_SECRET_KEY!,
})

export async function handler(request: Request) {
  const response = await mppx.charge({ amount: '1' })(request)

  if (response.status === 402) return response.challenge

  return response.withReceipt(Response.json({ data: '...' }))
}

Generate MPP_SECRET_KEY with at least 32 bytes, for example: openssl rand -base64 32.

Client

import { Mppx, tempo } from 'mppx/client'
import { privateKeyToAccount } from 'viem/accounts'

Mppx.create({
  methods: [tempo({ account: privateKeyToAccount('0x...') })],
})

// Global fetch now handles 402 automatically
const res = await fetch('https://mpp.dev/api/ping/paid')

Examples

ExampleDescription
chargePayment-gated photo generation API
charge-wagmiPayment-gated charge with Wagmi + React
session/multi-fetchMultiple paid requests over a single payment channel
session/ssePay-per-token LLM streaming with SSE
stripeStripe SPT charge with automatic client
npx gitpick wevm/mppx/examples/charge

CLI

mppx includes a basic CLI for making HTTP requests with automatic payment handling. Tempo session channels are retained and reused automatically until you close them.

# create account - stored in keychain, autofunded on testnet
mppx account create

# make request - automatic payment handling, curl-like api
mppx example.com

# pay an x402 offer on a server that advertises both protocols
mppx example.com --protocol x402

# open another session instead of reusing the preferred channel
mppx example.com --session new

# inspect and close retained sessions
mppx sessions list
mppx sessions view <channel-id>
mppx sessions close <channel-id>
mppx sessions close --all --yes

# explicitly trust a custom session escrow advertised by the server
mppx example.com -M allowCustomEscrow=true

--session auto is the default. Pass new to open another channel or a channel ID to select one explicitly.

Tempo session clients accept only the canonical escrow contract by default. A server may advertise its configured custom escrow in the payment challenge, but the client rejects it unless -M allowCustomEscrow=true is supplied. This opt-in trusts the server-selected address; clients that do not support custom escrows should leave it unset. See the session escrow trust documentation.

--protocol auto is the default: MPP is preferred when available, and x402 is used otherwise. Pass mpp or x402 to require one protocol. x402 payments use the same EVM account as EVM charges, so MPPX_PRIVATE_KEY or a stored account works for both.

Payment extensions can enforce policy or prepare funds after challenge selection and confirmation, immediately before credential creation:

import { defineConfig, Extension } from 'mppx/cli'

export default defineConfig({
  extensions: [
    Extension.from({
      async preparePayment({ challenge }) {
        await prepareFunds(challenge)
      },
    }),
  ],
})

Extensions run in configuration order. Throwing rejects the payment before Mppx signs it.

You can also install globally to use the mppx CLI from anywhere:

npm i -g mppx

Payments Proxy

mppx exports a Proxy server handler so that you can create or define a 402-protected payments proxy for any API.

import { openai, stripe, Proxy } from 'mppx/proxy'
import { Mppx, tempo } from 'mppx/server'

const mppx = Mppx.create({
  methods: [tempo()],
  secretKey: process.env.MPP_SECRET_KEY!,
})

const proxy = Proxy.create({
  services: [
    openai({
      apiKey: 'sk-...',
      routes: {
        'POST /v1/chat/completions': mppx.charge({ amount: '0.05' }),
        'POST /v1/completions': mppx.tempo.session({
          amount: '0.0001',
          unitType: 'token',
        }),
        'GET /v1/models': true,
      },
    }),
    stripe({
      apiKey: 'sk-...',
      routes: {
        'POST /v1/charges': mppx.charge({ amount: '0.01' }),
        'GET /v1/customers/:id': true,
      },
    }),
  ],
})

createServer(proxy.listener) // Node.js
Bun.serve(proxy) // Bun
Deno.serve(proxy.fetch) // Deno
app.use(proxy.listener) // Express
app.all('*', (c) => proxy.fetch(c.req.raw)) // Hono
app.all('*', (c) => proxy.fetch(c.request)) // Elysia
export const GET = proxy.fetch // Next.js
export const POST = proxy.fetch // Next.js

This exposes the following routes:

RoutePricing
POST /openai/v1/chat/completionscharge $0.005
POST /openai/v1/completionssession $0.0001 per token
GET /openai/v1/modelsfree
POST /stripe/v1/chargescharge $0.01
GET /stripe/v1/customers/:idfree

Protocol

Built on the "Payment" HTTP Authentication Scheme. See mpp-specs for the full specification.

License

MIT

Accepted Tempo currencies

tempo(), tempo.charge(), tempo.session(), and tempo.subscription() offer OUSD first, followed by USDC.e on mainnet or pathUSD on Moderato (testnet: true). Each factory returns a group accepted directly by Mppx.create. Clients choose one offer; ordering does not trigger an automatic swap.

import { Mppx, tempo } from 'mppx/server'
import { ousd, usdce } from 'viem/tokens'

const mppx = Mppx.create({
  methods: [
    tempo.charge({
      currencies: [ousd, usdce],
      recipient: '0x742d35Cc6634c0532925a3b844bC9e7595F8fE00',
    }),
  ],
})

Omit currencies for the network defaults, or use currencies: [ousd] to accept only OUSD on mainnet. Explicit lists replace the defaults. The deprecated currency option also restricts acceptance to one token. Wire requests and handler overrides continue to use singular currency.

Use configured handlers such as mppx.tempo.charge when composing payments. Code that directly inspects a Method can destructure the group: const [charge] = tempo.charge({ currencies: [ousd] }). Existing sessions and subscriptions continue using their originally authorized currency.

版本与发布记录

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

版本发布日期距今
0.17.1latest2026/11/04今天
0.0.0-main-20261104022639main2026/11/04今天
0.0.0-main-202611031718532026/11/041 天前
0.0.0-main-202611030136142026/11/031 天前
0.0.0-main-202611022248492026/11/032 天前
0.0.0-main-202611022106472026/11/032 天前

关于 mppx 的常见问题

如何安装 mppx?

在项目目录运行 npm install mppx,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:viem(>=2.57.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 mppx,可以用 npx mppx 直接运行。

mppx 支持 TypeScript 吗?

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

mppx 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

mppx 可以商用吗?

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

mppx 还在维护吗?

最新版本 0.17.1 发布于 2026/11/04(今天);至今共发布 305 个版本,其中过去 12 个月发布了 305 个;首次发布于 2026/02/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。