Skip to content

x402 Client with API (daccX402ClientWithAPI)

A function for Client x402 payment HTTP requests

The daccX402ClientWithAPI function allows you to make HTTP requests with automatic x402 payment handling. This integrates with x402 protocol for seamless fast micropayment-protected API access.

Import

import { daccX402ClientWithAPI } from 'dacc-js';

Usage

import { daccX402ClientWithAPI } from 'dacc-js';
 
const response = await daccX402ClientWithAPI({
  // account: "0xPrivatekey...", // Can call with `allowDaccWallet` function
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: "my+Password#123..",
  sellerServerURL: 'http://localhost:4021', // Your seller server URL
  endpointPath: '/weather', // Path on seller server
  method: 'GET', // GET, POST
  headers: { 'Content-Type': 'application/json' },
  // data: { key: 'value' } // Optionals body
});
 
console.log(response); // API response data

Arguments

ParameterTypeDescription
accountAccount or 0x${string}Conditional: The account to use for signing (Account object is private key).
daccPublickeystringConditional: The encrypted wallet data from createDaccWallet.
passwordSecretkeystringConditional: The user's password used to decrypt the wallet.
sellerServerURLstringRequired: Build seller x402 resource server base URL.
endpointPathstringRequired: API endpoint path to request.
method"GET" | "POST" | "PUT" | "PATCH" | "DELETE"Required: HTTP method for the request.
headersRecord<string, string>Required: Additional HTTP headers.
dataanyOptional: Body for requests (POST, PUT, PATCH).

Return Value

The response result is an object containing {data, payment, status}

Parameters

account (conditional)

  • Type: Account | 0x${string}

The user's account to use for signing the payment authorization.

const response = await daccX402ClientWithAPI({
  account: "0xPrivatekey...", // Can call with `allowDaccWallet` function
//   daccPublickey: 'daccPublickey_0x123_XxX..', //
//   passwordSecretkey: 'my+Password#123..', //
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/weather',
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
});

daccPublickey (conditional)

  • Type: string

The user's encrypted public key to address.

daccPublickey is the most reliable addressing solution and supports all types of data storage using daccPublickey.

const response = await daccX402ClientWithAPI({
//   account: "0xPrivatekey...", //
  daccPublickey: 'daccPublickey_0x123_XxX..', 
  passwordSecretkey: 'my+Password#123..', 
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/weather',
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
});

passwordSecretkey (conditional)

  • Type: string

The same password used when creating the wallet.

const response = await daccX402ClientWithAPI({
  daccPublickey: "daccPublickey_0x123_XxX..",
  passwordSecretkey: "my+Password#123..", 
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/weather',
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
});

sellerServerURL

  • Type: string

The base URL of the seller's x402 resource server.

const response = await daccX402ClientWithAPI({
  daccPublickey: "daccPublickey_0x123_XxX..",
  passwordSecretkey: "my+Password#123..",
  sellerServerURL: 'http://localhost:4021', 
  endpointPath: '/weather',
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
});

endpointPath

  • Type: string

The API endpoint path to request on the seller server.

const response = await daccX402ClientWithAPI({
  daccPublickey: "daccPublickey_0x123_XxX..",
  passwordSecretkey: "my+Password#123..",
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/weather', 
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
});

method

  • Type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE"

The HTTP method for the request.

const response = await daccX402ClientWithAPI({
  daccPublickey: "daccPublickey_0x123_XxX..",
  passwordSecretkey: "my+Password#123..",
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/weather',
  method: 'GET', // POST | PUT | DELETE
  headers: { 'Content-Type': 'application/json' },
});

headers

  • Type: Record<string, string>

Additional HTTP headers to include in the request.

const response = await daccX402ClientWithAPI({
  daccPublickey: "daccPublickey_0x123_XxX..",
  passwordSecretkey: "my+Password#123..",
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/weather',
  method: 'GET',
  headers: { 
    'Content-Type': 'application/json'
  }, 
});

data (optional)

  • Type: any

Optional request body for POST, PUT, PATCH requests.

const response = await daccX402ClientWithAPI({
  daccPublickey: "daccPublickey_0x123_XxX..",
  passwordSecretkey: "my+Password#123..",
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/submit',
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  data: { key: 'value' } 
});

Examples

Make a GET request using daccPublickey

import { daccX402ClientWithAPI } from "dacc-js";
 
// Fetch weather data with x402 payment
const response = await daccX402ClientWithAPI({
  daccPublickey: "daccPublickey_0x123_XxX..",
  passwordSecretkey: 'my+Password#123..',
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/weather',
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
});
 
console.log("Response data:", response.data);
console.log("Payment info:", response.payment);
console.log("Status:", response.status);

Make a POST request using private key directly

import { daccX402ClientWithAPI } from "dacc-js";
 
// Submit data with x402 payment using account private key (for testing)
const response = await daccX402ClientWithAPI({
  account: "0xPrivatekey...",
  sellerServerURL: 'http://localhost:4021',
  endpointPath: '/submit',
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  data: { message: 'Hello, x402!' }
});
 
console.log("Response:", response);

Using with Hono Framework

You can wrap daccX402ClientWithAPI in your own API endpoint using frameworks like Hono. This allows you to create a proxy API that handles x402 payments on behalf of your frontend clients.

Hono API Proxy Example

import { Hono } from "hono";
import { daccX402ClientWithAPI } from "dacc-js";
 
const app = new Hono();
 
// Create a proxy endpoint that forwards requests with x402 payment
app.get("/weather", async (c) => {
  const { daccPublickey, passwordSecretkey } = c.req.query();
 
  if (!daccPublickey || !passwordSecretkey) {
    return c.json({ error: "Missing daccPublickey or passwordSecretkey" }, 400);
  }
 
  const response = await daccX402ClientWithAPI({
    daccPublickey: daccPublickey,
    passwordSecretkey: passwordSecretkey,
    sellerServerURL: "http://localhost:4021",
    endpointPath: "/weather",
    method: "GET",
    headers: { "Content-Type": "application/json" },
  });
  
  return c.json({ response: response });
});
 
// POST endpoint example
app.post("/submit", async (c) => {
  const body = await c.req.json();
  const { daccPublickey, passwordSecretkey } = body;
 
  const response = await daccX402ClientWithAPI({
    daccPublickey: daccPublickey,
    passwordSecretkey: passwordSecretkey,
    sellerServerURL: "http://localhost:4021",
    endpointPath: "/submit",
    method: "POST",
    headers: { "Content-Type": "application/json" },
    data: body.data,
  });
  
  return c.json({ response: response });
});
 
export default app;

Calling the Hono Proxy from Frontend

// Frontend - Call your Hono proxy instead of direct x402 server
const fetchWeather = async () => {
  const response = await fetch(
    `http://localhost:3000/weather?daccPublickey=${daccPublickey}&passwordSecretkey=${passwordSecretkey}`
  );
  const result = await response.json();
  console.log("Weather:", result.response.data);
};
 
// Or with POST
const submitData = async (data: any) => {
  const response = await fetch("http://localhost:3000/submit", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      daccPublickey,
      passwordSecretkey,
      data,
    }),
  });
  const result = await response.json();
  console.log("Submit result:", result.response.data);
};