TX
TragenX

Documentation

Integrate x402-stacks into your project in minutes.

Quick Start

npm install x402-stacks

The x402-stacks package provides everything you need to accept and make payments using the x402 protocol on Stacks. It supports settlement in STX, sBTC, and USDCx tokens. Each endpoint gets its own payment gate with independent pricing and token selection.

Monetize Your API

Add the x402-stacks paymentMiddleware to your Express.js server. Each endpoint gets its own payment gate with a config object specifying the pay-to address, amount in micro-units, token type, and network in CAIP-2 format. The middleware returns HTTP 402 with payment details when no payment header is present, and verifies payments via the facilitator when a payment-signature header is included.

server.js
import express from "express";
import { paymentMiddleware } from "x402-stacks";

const app = express();
app.use(express.json());

const summarizeGate = paymentMiddleware({
  payTo: "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
  amount: "5000",           // 5000 microSTX = 0.005 STX
  tokenType: "STX",
  network: "stacks:1",      // CAIP-2 format
  facilitatorUrl: "https://x402-facilitator.stacksx402.com",
  scheme: "exact",
  maxTimeoutSeconds: 300,
});

const imageGate = paymentMiddleware({
  payTo: "SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7",
  amount: "5000",
  tokenType: "sBTC",
  network: "stacks:1",
  facilitatorUrl: "https://x402-facilitator.stacksx402.com",
  scheme: "exact",
});

app.post("/api/summarize", summarizeGate, (req, res) => {
  const result = summarizeText(req.body.text);
  res.json({ summary: result });
});

app.post("/api/generate-image", imageGate, (req, res) => {
  const image = generateImage(req.body.prompt);
  res.json({ image_url: image });
});

app.listen(3001, () => {
  console.log("x402-protected server running on :3001");
});

Register Your Service

List your API on the marketplace registry so buyers and agents can discover it. Use the REST API to register programmatically.

register.js
const response = await fetch(
  "https://stacks-x402-marketplace.vercel.app/api/registry",
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      name: "My AI Service",
      description: "Description of your API",
      provider: "Your Company",
      providerAddress: "SP2J6ZY48GV1EZ...",
      category: "Text Analysis",
      baseUrl: "https://your-api.com",
      tags: ["NLP", "summarization"],
      endpoints: [
        {
          method: "POST",
          path: "/api/analyze",
          description: "Analyze text content",
          pricePerCall: "0.01",
          token: "STX",
        },
      ],
    }),
  }
);

const service = await response.json();
console.log("Registered:", service.id);

x402-stacks Docs

Full protocol documentation and API reference.

Stacks Docs

Build apps and smart contracts on Bitcoin.