13/Integrate
The gateway, route by route
The x402 gateway (apps/x402-gateway/src/app.ts, Hono) serves ten free routes, three paid x402 routes and one internal route the agent pushes to. The base URL is X402_GATEWAY_URL (default http://localhost:4020); this build points at https://parity.mrnetwork.tech. Types come from packages/quant-kernel/src/types.ts. CORS is open to every origin.
Every route
| Method | Path | Price | Purpose |
|---|---|---|---|
| GET | /health | Free | Liveness, network, operator, facilitator, whether HCS publishing is on, agent heartbeat |
| GET | /status | Free | Everything the dashboard shows (below) |
| GET | /agent.json | Free | ERC-8004 registration file |
| GET | /.well-known/agent-registration.json | Free | Same as /agent.json |
| GET | /.well-known/ucp | Free | UCP agent card: identity, audit topics, live prices, guarantee policy |
| GET | /ucp.json | Free | Same as /.well-known/ucp |
| GET | /track-record | Free | Track-record summary, the last 500 predictions and scores, topic id, Mirror Node hint |
| GET | /guarantees | Free | The last 200 clearances and guarantee stats |
| GET | /guarantees/:id | Free | One clearance, or 404 |
| POST | /guarantee/claim | Free | Claim the slippage guarantee for a cleared swap |
| GET | /x402/toxicity-quote | x402, 1× ($0.0100) | Live toxicity quote |
| POST | /x402/rebalance-plan | x402, 2× ($0.0200) | EV-optimal range and hedge plan for your position |
| POST | /x402/protected-execute | x402, 2.5× ($0.0250) | Pre-trade clearance with a slippage guarantee |
| POST | /internal/snapshot | Internal | Agent push; needs x-parity-secret when INTERNAL_SYNC_SECRET is set, loopback only when it is not |
Paid prices are the calm-market defaults; see x402 endpoints and pricing for the formula. Paid routes use x402 v2, scheme exact, on hedera:mainnet, in USDC 0.0.456858.
Free routes
GET /health
{
"ok": true,
"service": "parity-x402-gateway",
"network": "hedera:mainnet",
"operatorAccountId": "0.0.x",
"facilitatorUrl": "https://api.blocky402.com",
"hcsPublisherEnabled": true,
"agentOnline": true,
"agentHeartbeatMs": 1764849600000
}GET /status
{
network: string; // "hedera:mainnet" | "hedera:testnet"
operatorAccountId: string;
hashscanBase: string; // "https://hashscan.io/mainnet"
mirrorNodeUrl: string;
topics: { decision: string | null; pnl: string | null; track: string | null };
agentOnline: boolean; // last push younger than 3 poll intervals + 30 s
agentHeartbeatMs: number | null;
bootstrap: boolean; // true until the agent's first push: numbers are placeholders
snapshot: MarketSnapshot; // includes provenance
mandate: EnsMandate;
ensSource: string; // "mainnet-live" | "sepolia-live" | "cached" | "unavailable" | "local-fallback" | "bootstrap"
ensProfile: unknown | null; // ENSIP-25/26/28/31 profile
latestDecision: RiskDecision;
economics: UnitEconomicsSnapshot;
pricing: EndpointPricing;
treasury: TreasuryState | null;
trackRecord: TrackRecordSummary;
guarantees: GuaranteeStats; // issued, cleared, claimed, honored, refunded, refundedUsd, feesUsd
predictions: Prediction[]; // last 50
scores: PredictionScore[]; // last 50
clearances: Clearance[]; // last 50
executions: ExecutionRecord[]; // last 50
ledger: LedgerEntry[]; // last 100
hook: { status; reason; txHash?; explorerUrl?; chainId?; score? } | null;
}GET /track-record
{
summary: TrackRecordSummary;
predictions: Prediction[]; // last 500
scores: PredictionScore[]; // last 500
topicId: string | null;
verifyHint: string | null; // "{mirror}/api/v1/topics/{topicId}/messages?order=desc&limit=100"
}GET /guarantees and /guarantees/:id
/guarantees returns { clearances: Clearance[], stats: GuaranteeStats } with the last 200 clearances. /guarantees/:id returns one Clearance or 404 { "error": "Unknown clearance" }.
POST /guarantee/claim
curl -s -X POST https://parity.mrnetwork.tech/guarantee/claim \
-H 'content-type: application/json' \
-d '{ "clearanceId": "clr_…", "transactionId": "0.0.1234@1764849600.123456789" }'{
"status": "REFUNDED",
"reason": "Realized 92.4 bps > guaranteed 80 bps: fee refunded",
"transactionId": "0.0.1234@1764849600.123456789",
"fill": { "side": "SELL_HBAR", "hbarAmount": 10, "usdcAmount": 1.71, "executionPrice": 0.171 },
"realizedSlippageBps": 92.4,
"guaranteedMaxSlippageBps": 80,
"refundAmountUsd": 0.025,
"refundTx": "0.0.x@1764849607.000000000",
"evaluatedAtMs": 1764849608000
}The numbers in this example are illustrative. clearanceId is 1 to 64 characters; transactionId must look like 0.0.x@secs.nanos or 0.0.x-secs-nanos.
Paid routes
Before the agent's first push every paid route answers 503 "Risk agent warming up; you were not charged." Without a payment, each answers 402 with the challenge in the payment-required header.
GET /x402/toxicity-quote?pair=HBAR-USDC
{
provider: string; // the ENS name
hederaAccountId: string;
quote: ToxicityQuote; // ewmaAnnualizedVolPct, crossVenueSkewBps, toxicityScore, regime,
// recommendedDynamicFeeBps, recommendedTickHalfWidth, maxSafeSlippageBps,
// recommendedSplitCount, estimated24hLvrUsd
referencePrice: number;
venuePrice: number;
provenance: Record<string, string>;
trackRecord: TrackRecordSummary;
pricing: EndpointPricing;
decisionProofHash: string;
}The handler always quotes the desk's HBAR-USDC snapshot; the pair query is not read.
POST /x402/rebalance-plan
{
"positionNotionalUsd": 2500, // > 0, at most 10,000,000
"positionDeltaRatio": 0.2, // -1 to 1; (HBAR USD - USDC) / total
"currentTick": -170000,
"currentTickLower": -170480,
"currentTickUpper": -169520
}Your fields override the desk's snapshot, and the response is { provider, quote: ToxicityQuote, plan: RebalancePlan }. plan holds the recommended action, tick range, hedge size, projected fees, friction, LVR, expected net EV, lpEdgeDailyPct, all seven range candidates and a rationale.
POST /x402/protected-execute
{
"side": "SELL_HBAR", // or "BUY_HBAR"
"amountIn": 10, // display units of the input token, > 0
"maxSlippageBps": 80, // all-in, > 0 and at most 2000
"pair": "HBAR-USDC", // optional, the only value
"callerAgentId": "my-agent" // optional, up to 200 characters
}{
cleared: boolean;
reasons: string[]; // why (not) cleared
clearance: Clearance; // payer and paymentTx are bound after settlement: fetch /guarantees/:id
expectedCost: ImpactEstimate; // side, amountIn, feeBps, priceImpactBps, allInCostBps, withinSingleRange
toxicity: { score: number; regime: "CALM" | "ELEVATED" | "TOXIC" };
recommendedSplitCount: number;
guarantee: { maxSlippageBps: number; validUntilMs: number; claimUrl: "/guarantee/claim"; policy: string };
}The response header x-parity-clearance-id carries the clearance id. Rules and claim flow are on Slippage guarantee.
Status codes
| Code | Route | Meaning |
|---|---|---|
| 400 | rebalance-plan, protected-execute, claim | The body failed validation; the error tree explains why |
| 401 | /internal/snapshot | Missing or wrong x-parity-secret |
| 402 | /x402/* | Payment required; read the payment-required header |
| 404 | /guarantees/:id, claim | Unknown clearance |
| 409 | claim | The clearance payment has not settled, or a claim is already in progress |
| 422 | claim | The claim was REJECTED; the clearance stays open |
| 502 | claim | A refund is owed but the transfer failed |
| 503 | /x402/* | Agent warming up, or the execution pool is unavailable; you are not charged |
HCS message schemas
All messages are single JSON documents on the topics from pnpm init:topics.
| Schema | Topic | Sent by | Carries |
|---|---|---|---|
parity.decision.v1 | decision | agent, every cycle | poolId, finalAction, approved, proposalSource, toxicityScore, regime, venuePx, refPx, expectedNetEvUsd, lpEdgeDailyPct, failed checks, decisionProofHash, ts |
parity.execution.v1 | decision | agent, after a live swap | action, side, amountIn, quotedOut, costBps, tx, proof |
parity.mode.v1 | decision | agent, on a mode change | from, to, reason, runwayDays, ts |
parity.agent-card.v1 | decision | agent, on start-up | name, account, ens, erc8004, gateway, topics, ts |
parity.prediction.v1 | track | agent | Prediction |
parity.score.v1 | track | agent | PredictionScore |
parity.pnl.v1 | pnl | agent and gateway | LedgerEntry |
parity.clearance.v1 | pnl | gateway, after a clearance is paid | id, payer, cleared, mid, guaranteedBps, notionalUsd, feeUsd, paymentTx, expiresAtMs, proof |
parity.refund.v1 | pnl | gateway, after a claim | clearanceId, payer, status, swapTx, realizedSlippageBps, guaranteedMaxSlippageBps, refundTx, refundAmountUsd |
curl -s "https://mainnet.mirrornode.hedera.com/api/v1/topics/<topicId>/messages?order=desc&limit=100" \
| jq -r '.messages[].message | @base64d'