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/core @x402/evm @x402/axiosUsage
import axios from "axios";
import { x402Client, wrapAxiosWithPayment, x402HTTPClient } from "@x402/axios";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
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 = privateKeyToAccount(privateKey as `0x${string}`); // viem
const client = new x402Client();
registerExactEvmScheme(client, { signer }); // x402-axios
// Setup axios client with payment interceptor
const api = wrapAxiosWithPayment(
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)
| Parameter | Type | Description |
|---|---|---|
daccPublickey | string | Conditional: The encrypted wallet data to lookup address from createDaccWallet. |
address | 0x${string} | Conditional: Wallet address to lookup daccPublickey on-chain (createDaccWallet in publicEncryption: true Mode can use instead of daccPublickey). |
passwordSecretkey | string | The user's password used as the key for encryption. |
x402
| Parameter | Type | Description |
|---|---|---|
payTo | 0x${string} | The address where payments will be sent |
endpoints | Record<string, EndpointConfig> | Configuration for protected endpoints |
facilitator | { url: Resource } | x402 facilitator service URL |
Use Cases
AI & ML Services
{
"/chat-gpt": { price: "$0.01", network: "eip155:84532" },
"/image-generate": { price: "$0.05", network: "eip155:84532" },
"/data-analysis": { price: "$0.002", network: "eip155:84532" }
}examples: https://github.com/coinbase/x402/tree/main/examples
Data & Analytics
{
"/weather": { price: "$0.001", network: "eip155:84532" },
"/market-data": { price: "$0.01", network: "eip155:84532" },
"/crypto-prices": { price: "$0.002", network: "eip155:84532" }
}examples: https://github.com/coinbase/x402/tree/main/examples
Premium Content
{
"/premium-article": { price: "$0.1", network: "eip155:84532" },
"/hd-image": { price: "$0.02", network: "eip155:84532" },
"/video-stream": { price: "$0.5", network: "eip155:84532" }
}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 { Hono } from "hono";
import { serve } from "@hono/node-server";
import { paymentMiddleware } from "@x402/hono";
import { x402ResourceServer, HTTPFacilitatorClient } from "@x402/core/server";
import { registerExactEvmScheme } from "@x402/evm/exact/server";
const app = new Hono();
const payTo = process.env.PAY_TO as `0x${string}` || '0x000000000000000000000000000000000000dEaD' as `0x${string}`;
const port = process.env.PORT || 4021;
const network = process.env.NETWORK || 'eip155:84532';
const facilitatorClient = new HTTPFacilitatorClient({
url: process.env.FACILITATOR_URL || "https://x402.org/facilitator"
});
const server = new x402ResourceServer(facilitatorClient);
registerExactEvmScheme(server);
app.use(
paymentMiddleware(
{
"/weather": {
accepts: [
{
scheme: "exact",
price: "$0.001",
network: network as `eip155:${number}`,
payTo,
},
],
description: "dacc-js X x402 for content testing.",
mimeType: "application/json",
},
},
server,
),
);
app.get("/weather", (c) => {
return c.json({ message: "This content is a successful dacc-js x x402 download." });
});
serve({ fetch: app.fetch, port: Number(port) });
console.log(`Seller server is running on port: http://localhost:${port}`);
console.log(`Serving /weather endpoint that costs.`);Client Setup - Buyer
import axios from "axios";
import { x402Client, wrapAxiosWithPayment, x402HTTPClient } from "@x402/axios";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
import { allowDaccWallet } from "dacc-js";
// Dacc-js setup
const daccPublickey = process.env.DACC_PUBLICKEY || ''; // Dacc Wallet public key from created wallet
const passwordSecretkey = process.env.PASSWORD_SECRETKEY || ''; // password to decrypt the private key
const { privateKey} = await allowDaccWallet({
daccPublickey: daccPublickey, // Dacc Wallet public key from created wallet
passwordSecretkey: passwordSecretkey, // password to decrypt the private key
});
// Create signer
const signer = privateKeyToAccount(privateKey as `0x${string}`);
const endpointPath = process.env.ENDPOINT_PATH || '/weather' as string;
// Create x402 client and register EVM scheme
const client = new x402Client();
registerExactEvmScheme(client, { signer });
// Create an Axios instance with payment handling
const api = wrapAxiosWithPayment(
axios.create({ baseURL: process.env.RESOURCE_SERVER_URL || "http://localhost:4021" }),
client,
);
// Make request - payment is handled automatically
const response = await api.get(endpointPath);
console.log("Response:", response.data);
// Get payment receipt
const httpClient = new x402HTTPClient(client);
const paymentResponse = httpClient.getPaymentSettleResponse(
(name) => response.headers[name.toLowerCase()]
);
console.log("Payment settled:", paymentResponse);Notes
- Security: Use strong passwords for
passwordSecretkey(min 12 characters) - Storage: Keep
daccPublickeysafe - 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