z402/packages/storage-postgres
2025-11-11 20:19:26 +03:00
..
2025-11-11 19:31:01 +03:00
2025-11-11 20:19:26 +03:00
2025-11-11 20:19:26 +03:00
2025-11-11 19:31:01 +03:00
2025-11-11 19:31:01 +03:00
2025-11-11 19:31:01 +03:00

z402-storage-postgres

Storage backend for z402.

Provides:

  • Crash-safe payment tracking
  • Double-spend resistance
  • Idempotent transaction recovery

Install

npm install z402-storage-postgres

Basic Example

import express from "express";
import { createPaymentContext, type PaymentResult } from "z402/express";
import { initPostgresDb } from "z402-storage-postgres";
import { Connection, clusterApiUrl } from "@solana/web3.js";

async function main() {
  const app = express();

  const db = await initPostgresDb({
    url: process.env.DATABASE_URL!, // works for neon / supabase / render / vercel
    driver: "neon", // change to "pg" for normal Postgres servers
  });

  const z402 = await createPaymentContext({
    connection: new Connection(clusterApiUrl("devnet")),
    db,
  });

  // Example paid endpoint
  app.get("/paid", z402.route("DestinationWalletPubkeyHere", "1000000"), (req, res) => {
    console.log(res.locals.payment as PaymentResult)
     res.send({"message": "Payment accepted! here is your paid content.", payment: res.locals.payment });
  });

  app.listen(3000, () => console.log("Example server running on http://localhost:3000"));
}

main();