Skip to content

x402 Integration

An open protocol for internet-native payments

x402 Integration enables pay-per-use micropayments for APIs and digital resources. Transform any endpoint into a revenue stream with payments as low as $0.001 per request, using Dacc Wallet for seamless Web3 transactions without traditional wallet friction.

Features

  • Pay-Per-Use: Micropayments from $0.001 per API call
  • Seamless UX: No Web3 wallet, no seed phrases, automatic payments
  • Web2 Solution: Seamless user wallet creation and versatile applications for your ideas
  • Developer-First: 3 lines of code to monetize any endpoint

References

Installation

bun add dacc-js x402-hono x402-axios hono axios @hono/node-server dotenv

Usage

import axios from 'axios';
import { withPaymentInterceptor, createSigner } from 'x402-axios';
import { allowDaccWallet } from 'dacc-js';
 
// Access Dacc Wallet
const { privateKey } = await allowDaccWallet({ // dacc-js
  daccPublickey: 'daccPublickey_XxX...', 
  passwordSecretkey: 'my+Password#123...', 
}); 
 
// Create payment signer
const signer = await createSigner("base-sepolia", privateKey); // x402-axios
 
// Setup axios client with payment interceptor
const api = withPaymentInterceptor( 
  axios.create({ baseURL: 'https://api.example.com' }), 
  signer, 
); 
 
// Make API call (payment happens automatically)
const response = await api.get('/weather');
console.log(response.data);

Arguments

dacc-js (allowDaccWallet)

ParameterTypeDescription
daccPublickeystringConditional: The encrypted wallet data to lookup address from createDaccWallet.
address0x${string}Conditional: Wallet address to lookup daccPublickey on-chain (createDaccWallet in publicEncryption: true Mode can use instead of daccPublickey).
passwordSecretkeystringThe user's password used as the key for encryption.

x402

ParameterTypeDescription
payTo0x${string}The address where payments will be sent
endpointsRecord<string, EndpointConfig>Configuration for protected endpoints
facilitator{ url: Resource }x402 facilitator service URL

Use Cases

AI & ML Services

{
  "/chat-gpt": { price: "$0.01", network: "base-sepolia" },
  "/image-generate": { price: "$0.05", network: "base-sepolia" },
  "/data-analysis": { price: "$0.002", network: "base-sepolia" }
}

examples: https://github.com/coinbase/x402/tree/main/examples

Data & Analytics

{
  "/weather": { price: "$0.001", network: "base-sepolia" },
  "/market-data": { price: "$0.01", network: "base-sepolia" },
  "/crypto-prices": { price: "$0.002", network: "base-sepolia" }
}

examples: https://github.com/coinbase/x402/tree/main/examples

Premium Content

{
  "/premium-article": { price: "$0.1", network: "base-sepolia" },
  "/hd-image": { price: "$0.02", network: "base-sepolia" },
  "/video-stream": { price: "$0.5", network: "base-sepolia" }
}

examples: https://github.com/coinbase/x402/tree/main/examples

Examples

Create Dacc Wallet

import { createDaccWallet } from "dacc-js";
 
// Create new wallet for payments
const wallet = await createDaccWallet({
  passwordSecretkey: 'my+Password#123..', 
  // publicEncryption: true, // Optional: for address-based access
});
 
console.log("Wallet Address:", wallet.address);
console.log("Dacc Public Key:", wallet.daccPublickey);
 
// Fund this address with USDC for payments
console.log("⚠️  Fund", wallet.address, "with USDC for payments");

Server Setup - Seller

import { config } from "dotenv";
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { paymentMiddleware } from "x402-hono";
 
config();
 
const facilitatorUrl = process.env.FACILITATOR_URL || 'https://www.x402.org/facilitator';
const payTo = process.env.PAY_TO || '0x000000000000000000000000000000000000dEaD';
const network = process.env.NETWORK || 'base-sepolia';
 
const app = new Hono();
 
app.use(
  paymentMiddleware(
    payTo,
    {
      "/weather": {
        price: "$0.001",
        network,
      },
      "/premium": {
        price: "$0.01",
        network,
      },
    },
    {
      url: facilitatorUrl,
    },
  ),
);
 
app.get("/weather", c => {
  return c.json({
    report: {
      weather: "sunny",
      temperature: 70,
    },
  });
});
 
serve({
  fetch: app.fetch,
  port: 4021,
});

Client Setup - Buyer

import axios from "axios";
import { config } from "dotenv";
import { withPaymentInterceptor, decodeXPaymentResponse, createSigner } from "x402-axios";
import { allowDaccWallet } from "dacc-js";
 
config();
 
const baseURL = process.env.RESOURCE_SERVER_URL || 'http://localhost:4021';
const daccPublickey = process.env.DACC_PUBLICKEY || '';
const passwordSecretkey = process.env.PASSWORD_SECRETKEY || '';
 
async function main(): Promise<void> {
  // Access Dacc Wallet
  const { privateKey } = await allowDaccWallet({
    daccPublickey: daccPublickey,
    passwordSecretkey: passwordSecretkey,
  });
 
  const signer = await createSigner("base-sepolia", privateKey);
 
  const api = withPaymentInterceptor(
    axios.create({ baseURL }),
    signer,
  );
 
  const response = await api.get('/weather');
  console.log(response.data);
 
  const paymentResponse = decodeXPaymentResponse(response.headers["x-payment-response"]);
  console.log("Payment details:", paymentResponse);
}
 
main();

Notes

  • Security: Use strong passwords for passwordSecretkey (min 12 characters)
  • Storage: Keep daccPublickey safe - it's your wallet identifier
  • Balance: Monitor USDC balance before making transactions
  • Testing: Use testnets (base-sepolia) before production
  • Error Handling: Implement retry logic for failed payments
  • Monitoring: Track payment responses and transaction status

code example: https://github.com/thefactlab-org/dacc-js/tree/main/examples/integration/x402