Quickstart
Overview
Crosshatch is an embedded wallet for all JavaScript runtimes, including the browser, Bun, Node.js, Deno and Workerd.
Install
npm
npm install crosshatchCore Functionality
Whenever you encounter an x402 payment required:
import { propose } from "crosshatch"
const decision = await propose({ required })
if (decision._tag === "Approved") {
const { payload } = decision
// Use the payment payload as you see fit.
}This will open a secure dialog to create, link and or fund a Crosshatch wallet. Thereafter, payloads will be created without additional prompts. Program dynamic micropayments from the user's wallet within the agreed upon allowance.
Fetch Example
Crosshatch's fetch wraps globalThis.fetch so that 402s are intercepted and retried automatically.
import { fetch } from "crosshatch/X402"Socket Client Example
402s and payment payloads can be communicated over any transport.
The following shows how a client can listen to a socket server that requires payment per clip buffered.
import { propose } from "crosshatch"
import { Required, Requirements } from "crosshatch/X402"
const socket = new WebSocket("wss://your-socket-server.com")
socket.addEventListener("message", async (raw) => {
// The server sends the resource (a `clip`) and
// the payment required for the next clip.
const { clip, required } = JSON.parse(raw)
// Use the resource.
player.buffer(clip)
// Pay for the subsequent resource.
const decision = await propose({ required })
if (decision._tag === "Approved") {
socket.send(JSON.stringify(decision.payload))
}
})