ఏజెంట్-చాలించిన API యాక్సెస్ కోసం స్ట్రైప్ నుండి Blockchain0x కు మిగులు.
Stripeని మార్చవద్దు; దానితో పాటు నడపండి. ప్రతి రక్షిత ఎండ్పాయింట్ ముందు ఒకే గేటు రెండు ఆథ్ పద్ధతులతో ఉంటుంది: ఒక క్రియాశీలమైన Stripe సబ్స్క్రిప్షన్ (మానవుల కోసం), లేకపోతే x402 రిసీవ్-సైడ్ అడాప్టర్ (ఏజెంట్ల కోసం), ఇది 402 సవాలు జారీ చేస్తుంది మరియు పునరుద్ధరణపై X-Payment హెడ్డర్ను నిర్ధారిస్తుంది. హ్యాండ్లర్ తర్కం మారదు.
మీరు ప్రారంభించడానికి ముందు.
- కనీసం ఒక చురుకైన ఉత్పత్తి/ధర (సబ్స్క్రిప్షన్ లేదా ఒకసారి)తో పనిచేసే Stripe ఇంటిగ్రేషన్.
- ఒక Blockchain0x ఏజెంట్ ప్రొఫైల్ మరియు API కీ ( add-payments-to-agent చూడండి).
- మీ వెబ్ ఫ్రేమ్వర్క్లో ప్రస్తుతానికి మీరు యాక్సెస్ను గేట్ చేయడానికి స్ట్రైప్ను కాల్ చేసే ఆథ్/మిడిల్వేర్ పొర.
- ఒక ఫీచర్-ఫ్లాగ్ యంత్రాంగం (ఎన్వ్ వేర్, LaunchDarkly, సరళమైన బూలియన్ - మళ్లీ మోహరించకుండా ప్రవర్తనను టాగిల్ చేయడానికి అనుమతించే ఏదైనా).
- x402 pattern యొక్క అర్థం - క్రింద ఉన్న డెకొరేటర్ దీన్ని అమలు చేస్తుంది.
ద్వంద్వ-అథ్ గేట్ను రాయండి.
గేట్ ఒకే ఒక్క గ్లూ భాగం. ఇది మొదట ఒక క్రియాశీలమైన Stripe సబ్స్క్రిప్షన్ కోసం తనిఖీ చేస్తుంది (మానవ మార్గం); ఎవరూ లేకపోతే, ఇది x402 రిసీవ్-సైడ్ అడాప్టర్ (ఏజెంట్ మార్గం) కు అభ్యర్థనను అందిస్తుంది, ఇది 402 ఛాలెంజ్ను జారీ చేస్తుంది మరియు ఏజెంట్ పునరావృతం చేసినప్పుడు X-Payment హెడ్డర్ను ధృవీకరిస్తుంది. నోడ్ ఉదాహరణ createX402Middleware ను ఉపయోగిస్తుంది; ఒక Python సేవ చేత అదే వైర్ను మాట్లాడుతుంది.
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 mode లో rollout చేసి, తర్వాత enabled mode కు మార్చండి.
అందరికీ ఒకేసారి రెండు paths ను flip చేయవద్దు. Safe rollout pattern నాలుగు phases లో ఉంటుంది - shadow, silent agent enablement, public agent enablement, observe. Stripe flow మొత్తం సమయంలో unchanged గా ఉంటుంది; agent traffic gradually పెరుగుతుంది.
# 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 తిరిగి ఇవ్వడం
చాలా ఉన్న ఎండ్పాయింట్లు Stripe సెషన్ లేకపోతే 401 అనధికారికంగా తిరిగి వస్తాయి. ఏజెంట్లు 401 తో ఏమి చేయాలో తెలియదు - వారు 'చెల్లించడానికి కొనసాగించండి' సంకేతంగా 402 చెల్లింపు అవసరం మాత్రమే అర్థం చేసుకుంటారు. గేట్ తేడా చేయాలి: 'ఈ కాలర్ అనధికారికంగా ఉంది మరియు చెల్లించలేడు' (నిజమైన 401, 401ని తిరిగి ఇవ్వండి) మరియు 'ఈ కాలర్ నిర్ధారించబడలేదు కానీ చెల్లించవచ్చు' (x402 402 సవాలును తిరిగి ఇవ్వండి).
ఏజెంట్లను Stripe ధరలను దాటించడానికి అనుమతించడం
మీ Stripe సబ్స్క్రిప్షన్ $20/నెలగా అన్లిమిటెడ్ కాల్స్ కోసం ఉంటే మరియు మీ x402 కోట్ $0.01/కాల్ అయితే, ఒక ఏజెంట్ సాంకేతికంగా $0.01ని ఒకసారి చెల్లించి ఒక కాల్ పొందవచ్చు, అక్కడ ఒక వ్యక్తి $20ని అనేక కాల్స్ కోసం చెల్లిస్తాడు. ఇది తాత్కాలిక-ఉపయోగ ఏజెంట్ ట్రాఫిక్కు బాగుంది మరియు అధిక-వాల్యూమ్ ఏజెంట్ ట్రాఫిక్కు విరుగ్గా ఉంది. ఒక భారీ ఏజెంట్ సహజంగా సబ్స్క్రిప్షన్ థ్రెషోల్డ్ను చేరేలా కాల్-ప్రతి ధరను సెట్ చేయండి - ఆపై వారికి మారడానికి ఎంపికను అందించండి.
ఎక్కడికి వెళ్లిన దారిని లాగించడం లేదు
ఒక డీబగ్గింగ్ అభ్యర్థన వస్తుంది: 'ఈ కస్టమర్ వారు చెల్లించారు కానీ మేము డెలివరీ చేయలేదు' అని చెబుతున్నారు. మీరు కాల్ను ఆమోదించిన ఏ ఆథ్ మార్గాన్ని లాగ్ చేయకపోతే, మీరు స్ట్రైప్ యొక్క రికార్డులను లేదా Blockchain0x యొక్క రికార్డులను చూడాలా అని తెలియదు. ఎప్పుడూ నిర్ణయాన్ని లాగ్ చేయండి: ఏ బ్రాంచ్ సరిపోతుంది, customer_id లేదా X-Payment / లావాదేవీ సూచన, మరియు ఒక సంబంధిత ID. దీనివల్ల, ప్రతి డ్యుయల్-రైల్ సంఘటనను ట్రైజ్ చేయడానికి రెండు రెట్లు ఎక్కువ సమయం పడుతుంది.
ఒకసారి డ్యుయల్-రైల్ ప్రత్యక్షమవుతుంది.
ఆర్కిటెక్చర్ ఏర్పాటు చేయబడినప్పుడు, మిగతా పని ఆపరేషనల్. వెబ్హుక్ బలవంతం Stripe మరియు Blockchain0x ఈవెంట్ స్ట్రీమ్లను నిర్వహిస్తుంది. ఖర్చు నియంత్రణలు మీరు నడుపుతున్న ఏజెంట్లను రక్షిస్తాయి. ప్రీ-లాంచ్ భద్రతా సమీక్ష ఒకే రైల్స్ ఇంటిగ్రేషన్ వంటి విధంగా వర్తిస్తుంది.
డెవలపర్లు ఎక్కువగా అడిగే వెబ్హుక్ ప్యాటర్న్స్
ప్రాంప్ట్ ఇంజెక్షన్ను తట్టుకునే ఏజెంట్ ఖర్చు నియంత్రణలను ఏర్పాటు చేయండి
ప్రత్యక్షంగా వెళ్లడానికి ముందు మీ ఏజెంట్ వాలెట్ను సురక్షితంగా ఉంచండి
docs.blockchain0x.comలో పూర్తి సూచిక. సంబంధిత ఉత్పత్తి ఉపరితలం: చెల్లింపు API. పోల్చడం: పోల్చింపులు.