Getting started
Install @integraledger/lcp, assemble an ATR, and carry its hash through one x402 payment.
For AI agents: this page as Markdown, and llms.txt, the index of every page.
This page takes you from an empty folder to one x402 payment whose signed nonce is the hash of the agreement's record.
You play both parties, the seller and the buyer, in one program. Nothing touches a network: a Map stands in for the
seller's storage, and a random key for the buyer's wallet.
Three terms carry the whole page:
- The Agentic Transaction Record (ATR) is the agreement's record: the exact bytes the seller serves;
assemblewrites them as one JSON object. - The ATR hash (H) is SHA-256 over the ATR's exact bytes.
- Binding is how H rides in a pairing's payment: the field its specification defines. Here it is the
nonceof the EIP-3009 authorization the buyer signs.
Requirements
- Node.js
>=24.22.1. Node runs the TypeScript files on this page directly, by stripping their types. - An ESM project. With TypeScript, set
"module"and"moduleResolution"to"nodenext"(or"moduleResolution"to"bundler"), so the package's subpath exports resolve.
Install
mkdir lcp-start && cd lcp-start
npm init -y
npm pkg set type=module
npm install @integraledger/lcp viem@integraledger/lcp is the package; viem signs for the buyer in step 3. Any EIP-712 signer works
in its place: the package holds no key and signs nothing.
1. Assemble an ATR
The seller writes the record. assemble(id, binding, content) writes one JSON object in a fixed order: the format
marker, the per-transaction id, the binding slot that ties the record to one payment, and then the parties' content,
byte for byte as given. It returns the bytes and H.
Save this as assemble.ts and run node assemble.ts:
import { , } from "@integraledger/lcp";
const = new ().('{"text":"One market report for 10000 base units of USDC."}');
const = await ("0f8fad5b-d9cb-469f-a165-70867728950e", ["x402", { : [] }], [["terms", ]]);
if (()) throw new (.);
.(new ().(.));
.(.);{"atrVersion":"1","id":"0f8fad5b-d9cb-469f-a165-70867728950e","x402":{"accepts":[]},"terms":{"text":"One market report for 10000 base units of USDC."}}
0x505084d776e5fe9e674b530133325906510c09f6cab2ac5e94b416c0f0917189This page fixes the id so your output matches. For a real transaction, pass newAtrId(), a random RFC 9562 UUID:
the fresh id makes every H unique, even for identical terms.
Every function in the package that can refuse returns its result or a refusal, a value with
a code, so check isRefusal(result) before you use a result. The writers that only encode a value you supply, such as
toLcpString, throw a TypeError on an argument outside their type instead
(what throws).
2. Compare the bytes with H
The buyer never trusts a record it has not hashed. It fetches the bytes the seller serves, hashes them, and compares
the result with the H the seller advertised. One changed byte, here the final . of the terms turned into !, gives
another hash:
import { , , , } from "@integraledger/lcp";
const = new ().('{"text":"One market report for 10000 base units of USDC."}');
const = await ("0f8fad5b-d9cb-469f-a165-70867728950e", ["x402", { : [] }], [["terms", ]]);
if (()) throw new (.);
const = ..();
.("as served:", (await (), .));
[. - 4] = 0x21;
.("one byte changed:", (await (), .));as served: true
one byte changed: falsehashEquals compares the 32 bytes the two strings decode to, in either case. This comparison is the
buyer gate: if it fails, the buyer signs nothing.
3. Carry H through one x402 payment
The whole exchange, on x402/exact/eip155/eip3009: USDC on Base Sepolia, paid with an EIP-3009 authorization.
Save this as pay.ts and run node pay.ts:
import { , , , , } from "@integraledger/lcp";
import {
,
,
,
type ,
type ,
} from "@integraledger/lcp/x402";
import type { } from "viem";
import { , } from "viem/accounts";
// Seller: the x402 challenge for GET /v1/report, with one option.
const : = {
: "exact",
: "eip155:84532",
: "10000",
: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
: "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
: 60,
: { : "USDC", : "2" },
};
const : = {
: 2,
: { : "https://api.seller.example/v1/report" },
: [],
};
// Seller, a: assemble the ATR for this request. Its binding slot records the options and the request.
const = await ({ : "GET", : "/v1/report", : new () });
if (()) throw new (.);
const = new ().('{"text":"One market report for 10000 base units of USDC."}');
const = await ((), (., ), [["terms", ]]);
if (()) throw new (.);
// Seller, b: store the bytes at an https link, from a random token, before the challenge goes out.
const = `https://atr.seller.example/${.()}`;
const = new ([[, .]]);
// Seller, c: advertise H and the link in the challenge's legalContext extension.
const = .(, ., , );
if (()) throw new (.);
const = .?.["legalContext"]?. as { ?: string } | undefined;
.("1. challenge carries H:", ?. === .);
// Buyer, d: read H and the link, fetch the bytes, and compare.
const = .();
if (()) throw new (.);
const = .(.);
if ( === || !(await (), .)) throw new ("decline: hash-mismatch");
.("2. the served bytes hash to H");
// Buyer, e: build the authorization with H as its nonce, sign it, and complete the payment.
const = (());
const = await .(
{ : , : ..[0]!, : ., : .(.() / 1000) },
.,
);
if (()) throw new (.);
const { , } = .;
.("3. the buyer signs", , "with nonce H:", . === .);
const = .(await .(. as ));
if (()) throw new (.);
// Seller, f: read H back from what the buyer signed, and match it to the H it issued.
const = await .();
if (typeof !== "string") throw new (.);
.("4. the payment is bound to the ATR:", (, .));1. challenge carries H: true
2. the served bytes hash to H
3. the buyer signs TransferWithAuthorization with nonce H: true
4. the payment is bound to the ATR: trueWhat each step did:
| Step | Side | Call | What it gives |
|---|---|---|---|
| a | seller | requestCommitment, tie, assemble | The ATR's bytes and H, with the challenge's options and the request in the binding slot. |
| b | seller | your storage | The bytes at an https link built from a random token, not from H (serving the link), before any buyer can ask for them. |
| c | seller | advertise | The challenge with extensions.legalContext set to H and the link. |
| d | buyer | read, hash, hashEquals | H and the link from the challenge, and the check that the served bytes hash to H. |
| e | buyer | build, complete | EIP-712 typed data whose nonce is H, and the x402 payment around the buyer's signature. |
| f | seller | bound | H read back from the signed authorization. |
On a live network
The program stops at a signed payment in memory. On a live network the same calls run on the two sides of an HTTP
exchange, and a facilitator settles the payment. The functions below are exported by @integraledger/lcp/x402, and
the EvmReader type by @integraledger/lcp/evm:
- The seller answers
402. It sends the advertised challenge in thePAYMENT-REQUIREDheader,encodeHeader(advertised). The buyer reads it withdecodePaymentRequired(header)and runs steps d and e on what that returns (the headers). - The buyer pays. It sends the request again with the payment in the
PAYMENT-SIGNATUREheader,encodeHeader(payment). The seller reads it withdecodePaymentSignature(header)and runs step f: the H thatboundreads must be one it issued for this request, and not one it has already accepted. - A facilitator settles. The seller sends the payment, with the option it answers, to its facilitator's
/verifyand/settle, which submits the authorization; the token contract'sAuthorizationUsedevent then carries H on chain. This repository's settler is such a facilitator (run it). The settle answer names the transaction, and the seller can return it to the buyer in thePAYMENT-RESPONSEheader,encodeHeader(settlement). - The seller reads the settlement.
exactEip3009.reference(payment, option)gives the read keys. With the transaction the settle answer names added astransaction,exactEip3009.status(ref, reader, { act: "payment", finality: "finalized" })reads it through anEvmReaderyou write over your own RPC endpoint, and answerssettledonce the transaction is this payment's at that mark (reading settlement).
To run this on Base Sepolia with the option above, the payer's key needs test USDC at the option's asset, the
facilitator's key needs Base Sepolia ETH for the gas of the transaction it submits, and the reader needs an RPC
endpoint for eip155:84532. The seller guide walks the seller's side.
Next
- Building a seller? Read Seller, then x402 or MPP.
- Building a buyer? Read Buyer and the buyer gate. The buyer packages give you the gate ready-made.
- Paying on another rail? Rails gives the field H rides in on each chain and network.
- Want the details? The ATR, the ATR hash and binding explain each rule the program above relies on.
Last updated on