Public
Star 历史趋势
数据来源: GitHub API · 生成自 Stargazers.cn
README.md
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.

Install

npm i mppx

Quick Start

Server

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

const mppx = Mppx.create({
  methods: [
    tempo({
      currency: '0x20c0000000000000000000000000000000000000',
      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 { privateKeyToAccount } from 'viem/accounts'
import { Mppx, tempo } from 'mppx/client'

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.

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

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

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

关于 About

TypeScript Interface for Machine Payments Protocol
machine-paymentsmachine-payments-protocolmpppaymentstempoviemwagmi

语言 Languages

TypeScript100.0%
JavaScript0.0%

提交活跃度 Commit Activity

代码提交热力图
过去 52 周的开发活跃度
699
Total Commits
峰值: 97次/周
Less
More

核心贡献者 Contributors