એજન્ટ-ચાલિત API ઍક્સેસ માટે Stripe થી Blockchain0x પર સ્થળાંતર કરો.
Stripe ને બદલો નહીં; તેને સાથે ચલાવો. એક જ ગેટ દરેક સુરક્ષિત અંતિમ બિંદુને બે ઓથ પદ્ધતિઓ સાથે આગળ રાખે છે: એક સક્રિય Stripe સબ્સ્ક્રિપ્શન (માનવ માટે), અન્યથા x402 રીસીવ-સાઇડ એડેપ્ટર (એજન્ટ માટે), જે 402 પડકાર આપે છે અને પુનરાવર્તન પર X-Payment હેડર ચકાસે છે. હેન્ડલર લોજિક બદલાતી નથી.
તમે શરૂ કરવા પહેલાં.
- એક કાર્યરત Stripe એકીકરણમાં ઓછામાં ઓછું એક સક્રિય ઉત્પાદન/કિંમત (સબ્સ્ક્રિપ્શન અથવા એકવાર).
- એક Blockchain0x એજન્ટ પ્રોફાઇલ અને API કી ( એજન્ટને ચુકવણીઓ ઉમેરો જુઓ).
- તમારા વેબ ફ્રેમવર્કમાં એક ઓથ/મિડલવેર સ્તર જ્યાં તમે હાલમાં સ્ટ્રાઇપને પ્રવેશ માટે કૉલ કરો છો.
- એક ફીચર-ફ્લેગ મિકેનિઝમ (એન્વી વેર, LaunchDarkly, સરળ બૂલિયન - કંઈપણ જે તમને ફરીથી ડિપ્લોય કર્યા વિના વર્તનને ટોગલ કરવાની મંજૂરી આપે છે).
- સમજવું x402 પેટર્ન - નીચે આપેલ ડેકોરેટર તેને અમલમાં લાવે છે.
ડ્યુઅલ-ઑથ ગેટ લખો.
ગેટ એક જ ગ્લૂનો ટુકડો છે. તે પહેલા સક્રિય સ્ટ્રાઇપ સબ્સ્ક્રિપ્શન માટે તપાસ કરે છે (માનવ માર્ગ); જો તે ન હોય, તો તે વિનંતીને x402 રિસીવ-સાઇડ એડેપ્ટર (એજન્ટ માર્ગ) ને હસ્તાંતર કરે છે, જે 402 પડકાર આપે છે અને એજન્ટ ફરી પ્રયાસ કરતા X-Payment હેડરનું માન્યકરણ કરે છે. નોડ ઉદાહરણ createX402Middleware નો ઉપયોગ કરે છે; એક પાયથન સેવા હાથથી સમાન વાયર બોલે છે.
import express from "express";
import Stripe from "stripe";
import { createClient } from "@blockchain0x/node";
import { createX402Middleware } from "@blockchain0x/x402/server/express";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
const sdk = createClient({ apiKey: process.env.BLOCKCHAIN0X_API_KEY! });
// Agents pay via x402: this middleware issues the 402 challenge and verifies
// the X-Payment header on the retry. Configure price + recipient per the docs.
const x402 = createX402Middleware({ sdk });
// Humans with an active Stripe subscription skip the paywall; everyone else
// falls through to the x402 challenge.
function stripeOrX402(req: express.Request, res: express.Response, next: express.NextFunction) {
const customer = req.cookies?.stripe_customer_id;
if (!customer) return x402(req, res, next); // agent path
stripe.subscriptions
.list({ customer, status: "active", limit: 1 })
.then((subs) => (subs.data.length > 0 ? next() : x402(req, res, next)))
.catch(next);
}from functools import wraps
from flask import request, jsonify
import stripe, os
stripe.api_key = os.environ["STRIPE_SECRET_KEY"]
# The x402 receive-side adapter is Node; a Python service speaks the wire
# directly: advertise requirements in a 402, accept a resent X-Payment header.
def stripe_or_x402(resource: str):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
# 1. Human path: active Stripe subscription.
customer_id = request.cookies.get("stripe_customer_id")
if customer_id:
subs = stripe.Subscription.list(customer=customer_id, status="active", limit=1)
if subs["data"]:
return fn(*args, **kwargs)
# 2. Agent path: a valid X-Payment header ("exact-usdc:<base64(json)>")
# means the caller paid; verify it, then let the request through.
if request.headers.get("X-Payment"):
return fn(*args, **kwargs)
# 3. No auth - advertise the x402 requirements in a 402.
return jsonify({
"version": 1,
"resource": resource,
"accepts": [{"scheme": "exact-usdc", "network": "eip155:8453"}],
}), 402
return wrapper
return decoratorતેને એન્ડપોઈન્ટ પર લાગુ કરો.
હેન્ડલર પોતે બદલાતું નથી - ડેકોરેટર ઓથેન્ટિકેશન/ચુકવણી તર્કને સંભાળે છે, પછી ફક્ત ચુકવણી નક્કી થાય ત્યારે જ અસ્તિત્વમાં અમલમાં મોકલે છે. આ જ છે જે માઇગ્રેશનને ઓછા જોખમવાળું બનાવે છે: માનવ પ્રવાહો અસ્પષ્ટ છે, તમે માત્ર એક વિકલ્પ માર્ગ ઉમેર્યો છે.
// Apply the gate to the endpoint. The x402 middleware handles the 402
// challenge and the X-Payment verification; your handler only runs once paid.
app.get("/api/premium-feature",
stripeOrX402,
async (req, res) => {
const result = await runPremiumFeature();
res.json(result);
},
);@app.get("/api/premium-feature")
@stripe_or_x402("/api/premium-feature")
def premium_feature():
return run_premium_feature()Shadow માં roll out કરો, પછી enabled mode.
બધા માટે બંને paths ને એકસાથે flip કરશો નહીં. સલામત rollout pattern ચાર phases માં છે - shadow, silent agent enablement, public agent enablement, observe. Stripe flow સમગ્ર સમય અપરિવર્તિત રહે છે; agent traffic ધીમે ધીમે વધે છે.
# Rollout plan: keep Stripe-only working while you add the agent path.
# Week 1 - shadow mode
# - Deploy the decorator with the agent-path branch behind a feature flag (off).
# - Human Stripe flow continues unchanged.
# - Sandbox-test the agent path against Base Sepolia.
# Week 2 - silent agent enablement
# - Turn the agent-path branch on for a single internal agent.
# - Verify the 402 issues correctly and settlement works end-to-end.
# - Wire alerting on the 402-issued / 402-settled rate.
# Week 3 - public agent enablement
# - Document the x402 contract in your developer docs.
# - Announce to existing customers building agents.
# - Continue measuring: human Stripe flow should be unchanged.
# Week 4+ - observe
# - Track the ratio of agent settlements to human subscriptions.
# - As agent traffic grows, you may decide to keep Stripe only for humans
# and let everything else go through x402. That is a later decision -
# the architecture above supports either trajectory.ચાર ભૂલો જે ડ્યુઅલ-રેલને દુખદાયક બનાવે છે.
તેને વધારવા માટે સ્ટ્રાઇપને બદલીને પ્રયાસ કરવો
સ્ટ્રાઇપ એક વખતના માનવ ચેકઆઉટ અને માનવ સબ્સ્ક્રિપ્શન માટે યોગ્ય છે. એજન્ટ ટ્રાફિકને સ્ટ્રાઇપ દ્વારા જોરદાર બનાવવાનો પ્રયાસ કરવો (પ્રતિ-કોલ ઇનવોઇસ, ડાયનામિક પ્રાઇસિંગ) કાર્ડ-નેટવર્કની ન્યૂનતમ અને ફી માળખા સામે તૂટે છે. સફળ પેટર્ન વધારણા છે: સ્ટ્રાઇપને તે જેમાં સારો છે તે કરવા દો (માનવોએ કાર્ડ સાથે ચૂકવણી કરવી) અને ટ્રાફિક માટે x402 / એજન્ટ ચુકવણીઓ ઉમેરો જે માટે સ્ટ્રાઇપ ક્યારેય ડિઝાઇન કરવામાં આવ્યું નથી. એક અથવા બીજું પસંદ ન કરો.
402ની જગ્યાએ એજન્ટને 401 પાછું આપવું
અધિકાંશ અસ્તિત્વમાં રહેલા અંતરાલો 401 Unauthorized પાછું આપે છે જ્યારે કોઈ Stripe સત્ર નથી. એજન્ટો 401 સાથે શું કરવું તે જાણતા નથી - તેઓ ફક્ત 402 ચુકવણીની જરૂરિયાતને 'આગળ વધવા માટે ચૂકવો' સંકેત તરીકે સમજે છે. ગેટને ભેદભાવ કરવો જોઈએ: 'આ કૉલર અધિકૃત નથી અને ચૂકવણી કરી શકતો નથી' (સાચો 401, 401 પાછું આપો) વિરુદ્ધ 'આ કૉલર માન્ય નથી પરંતુ ચૂકવણી કરી શકે છે' (x402 402 પડકાર પાછું આપો).
એજન્ટોને સ્ટ્રાઇપના ભાવને બાયપાસ કરવાની મંજૂરી આપવી
જો તમારું Stripe સબ્સ્ક્રિપ્શન અનલિમિટેડ કોલ્સ માટે $20/મહિને છે અને તમારું x402 કોટ $0.01/કોલ છે, તો એક એજન્ટ ટેકનિકલી $0.01 એકવાર ચૂકવી શકે છે અને એક કોલ મેળવી શકે છે જ્યાં એક માનવ $20 માટે ઘણા ચૂકવે છે. તે ક્યારેક ઉપયોગમાં આવતી એજન્ટ ટ્રાફિક માટે ઠીક છે અને ઉચ્ચ-વોલ્યુમ એજન્ટ ટ્રાફિક માટે તૂટેલું છે. પ્રતિ-કોલ કિંમત સેટ કરો જેથી એક ભારે એજન્ટ સ્વાભાવિક રીતે સબ્સ્ક્રિપ્શન થ્રેશોલ્ડ પર પહોંચે - પછી તેમને સ્વિચ કરવાની વિકલ્પ આપો.
કયા માર્ગે ગયા તે લોગિંગ નથી
એક ડિબગિંગ વિનંતી આવે છે: 'આ ગ્રાહકે કહ્યું કે તેમણે ચૂકવણી કરી પરંતુ અમે ડિલિવર નથી કર્યું'. જો તમે લોગ નથી કર્યો કે કઈ ઓથ પાથે કોલને મંજૂરી આપી, તો તમે નથી જાણતા કે સ્ટ્રાઇપના રેકોર્ડ્સ અથવા Blockchain0xના રેકોર્ડ્સ પર જોવું છે કે નહીં. હંમેશા નિર્ણયને લોગ કરો: કઈ શાખા મેળવે છે, customer_id અથવા X-Payment / ટ્રાન્ઝેક્શન સંદર્ભ, અને એક સહસંબંધ ID. તેના વિના, દરેક ડ્યુઅલ-રેલ ઘટના triage કરવા માટે બે ગણી લાંબી લાગે છે.
જ્યારે ડ્યુઅલ-રેેલ જીવંત થાય છે.
આર્કિટેક્ચર સાથે, બાકીનું કાર્યાત્મક છે. વેબહૂક મજબૂતી સ્ટ્રાઇપ અને Blockchain0x ઇવેન્ટ સ્ટ્રીમ બંનેને સંભાળે છે. ખર્ચ નિયંત્રણો તમારી પાસે કાર્યરત કોઈપણ એજન્ટોને સુરક્ષિત કરે છે. પ્રી-લાંચ સુરક્ષા સમીક્ષા એક જ રેલ ઇન્ટિગ્રેશનની જેમ લાગુ પડે છે.
વેબહૂક પેટર્ન જે વિકાસકર્તાઓ સૌથી વધુ પૂછે છે
એજન્ટ ખર્ચ નિયંત્રણો સેટ કરો જે પ્રોમ્પ્ટ ઇન્જેક્શનને જીવંત રહે છે
લાઇવ જવા પહેલા તમારા એજન્ટ વોલેટને સુરક્ષિત કરો
પૂર્ણ સંદર્ભ docs.blockchain0x.com પર છે. સંબંધિત ઉત્પાદન સપાટી: ચુકવણી API. તુલનાત્મક ફ્રેમિંગ: તુલનાઓ.