支付订阅

使用 Pay.sh

您可以通过在设备上配置 Pay 客户端或搭建 Pay 服务器的方式,使用 Pay.sh 设置订阅。使用以下命令安装 Pay:

brew install pay

npm install -g @solana/pay

使用以下命令验证安装:

pay --version

工具链将自动为您配置地址。运行以下命令以完成钱包设置并充值:

pay setup

作为客户端管理订阅

用户通过正常调用 pay 命令来订阅受订阅限制的 API 端点。API 负责在初始 402 Status 响应中发送订阅详情。Pay 会自动处理订阅事宜,包括在 Solana SPL 订阅程序上执行相关指令。

用户可以使用以下命令管理订阅:

pay subscriptions # default: list + show available subcommands
pay subscriptions list # same, scriptable
pay subscriptions list --network mainnet
pay subscriptions list --json

您可以在 Pay.sh > 使用 Pay > 管理订阅文档 中了解更多关于用户订阅管理的内容。

作为服务提供商管理订阅(演示)

Pay 服务器支持为每个端点配置订阅者及其订阅信息。以下是一个使用 pay 的端点示例,您可以在项目中直接尝试:

# monthly.yml
name: Subscriptions Example
subdomain: subscriptions-example
title: "Subscription Example"
description: "Demo Subscriptions"
category: ai_ml
version: v1
routing:
type: respond
operator:
currencies:
usd: ["USDC", "USDT", "CASH"]
network: "localnet"
fee_payer: true
challenge_binding_secret: d858d9104afd728f58a1f73d806dd75d8f32ebd37c9707145816b490e8631d41
# insert your own challenge_binding_secret here. use `openssl rand -hex 32`
endpoints:
- method: GET
path: "api/v1/pro/feed"
resource: "pro-feed"
description: "Pro feed — monthly subscription, 30-day billing period."
subscription:
period: "30d"
price_usd: 9.99
currency: USDC
plan_id_numeric: 1

Pay 通过其 endpoints YAML 规范中的 subscription: 属性来处理订阅,这与按需付费端点所使用的 metering: 属性有所不同。

启动上述示例中的服务器后,将创建一个名为 api/v1/pro/feed 的端点,要求用户每 30 天向其支付 9.99 USDC 的周期性费用。

您可以运行以下命令进行体验:

pay server start monthly.yml

运行此命令后,系统将提示您创建一个计划。这将生成一个计划并更新 yml 文件。如果您打算再次使用,请记下计划地址,并在后续修改配置时使用该地址。

您可以通过在另一个终端上以客户端身份运行以下命令来测试订阅是否正常工作:

# First call — activates the subscription, $9.99 USDC charge settles.
pay --sandbox curl http://127.0.0.1:1402/api/v1/pro/feed
# Same call within 30 days — no payment prompt, just the response.
pay --sandbox curl http://127.0.0.1:1402/api/v1/pro/feed

您应该会看到如下所示的 OK 状态响应:

{ "status": "ok" }

您可以通过 Pay.sh > Building with Pay > Subscriptions > YAML Specification docs 了解更多关于在 Pay 服务器上管理订阅的信息。

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有