blockchainiq-langchain: LangChain.js tools for the Blockchain IQ x402 crypto data API (market brief $0.01, event risk $0.05; paid per call in USDC on Base; spend cap; offline tests with mocked x402 settlement)
LangChain.js tools for the Blockchain IQ x402 crypto data API. A tool-calling model or agent can buy Blockchain IQ data one call at a time. It pays in USDC on Base over x402 v2. You don't need an API key or an account. The wallet is the only credential.
| Tool | Resource | Price | Returns |
|---|---|---|---|
blockchainiq_market_brief |
GET /api/x402/v1/brief |
$0.01 | BTC, ETH, SOL and XRP prices with 24h, 7d and 30d moves and rank, scheduled events in the next 48 h, and risk flags |
blockchainiq_event_risk |
GET /api/x402/v1/event-risk |
$0.05 | Scheduled market-moving events for the next 7 days by default (days 1–90, minImportance 1–3), each with an official source |
Each tool returns a JSON string containing:
ok and statusdata: the resource's payloadsettlement: the decoded PAYMENT-RESPONSE header (transaction, network, payer)explorer: a Basescan link to the settlement transactionThese are standard @langchain/core tools built with tool() and zod schemas. They work with:
llm.bindTools(tools)createAgent({ model, tools })ToolNodetool.invoke(args) or tool.invoke(toolCall), which returns a ToolMessageGET. Blockchain IQ answers 402 with PAYMENT-REQUIRED: the price, USDC, Base eip155:8453 and the payTo address.@x402/fetch with ExactEvmScheme signs an EIP-3009 USDC authorization for exactly that amount with your viem account, then retries with PAYMENT-SIGNATURE.200 with JSON and PAYMENT-RESPONSE, which carries the transaction hash. The payer needs no ETH.Spend cap: a per-payment cap (maxUsdPerCall, default $0.06) is enforced by x402 spendControls. A price above the cap is refused before anything is signed. A 404 or an error is never charged.
Requirements:
git clone https://agentgate-source.pages.dev/blockchainiq-x402-langchain/blockchainiq-x402-langchain.git
cd blockchainiq-x402-langchain
npm install
npm test # offline: mocked 402 + mocked settlement, no wallet, no money
npm run probe # free: reads the live 402 price terms for brief and event-risk, pays nothing
export AGENT_PRIVATE_KEY=0x... # dedicated wallet with >= 0.01 USDC on Base
npm run paid-call # brief, $0.01
npm run paid-call -- event-risk # event-risk, $0.05
The command prints the data, the proof of settlement and the Basescan link:
payer 0xYourWallet -> blockchainiq_market_brief
{ "asOf": "...", "coins": [ ... ], "events": [ ... ], "flags": [ ... ] }
proof of settlement: {"success":true,"transaction":"0x...","network":"eip155:8453","payer":"0xYourWallet"}
transaction: https://basescan.org/tx/0x...
If the wallet has no USDC, the facilitator refuses the authorization. You get payment not accepted: ... (does the paying wallet hold enough USDC on Base?) and nothing is charged.
import { privateKeyToAccount } from "viem/accounts";
import { ChatOpenAI } from "@langchain/openai";
import { blockchainIQTools } from "blockchainiq-langchain";
const tools = [...blockchainIQTools({
signer: privateKeyToAccount(process.env.AGENT_PRIVATE_KEY as `0x${string}`),
maxUsdPerCall: 0.06,
})];
const llm = new ChatOpenAI({ model: "gpt-4o-mini" }).bindTools(tools);
examples/agent.ts is a small tool-calling loop. It needs @langchain/openai and OPENAI_API_KEY, and caps spend at $0.02, so it can only buy the brief.
| Option | Default | Meaning |
|---|---|---|
signer |
required | viem LocalAccount (or any signer ExactEvmScheme accepts) that pays |
maxUsdPerCall |
0.06 |
Refuse any single payment above this many dollars |
baseUrl |
https://www.blockchainiq.co/api/x402/v1 |
API base (tests) |
fetchImpl |
global fetch |
Inject a fetch (tests) |
npm test compiles with tsc and runs node --test. The suite is fully offline, and any real network call fails it:
PAYMENT-REQUIRED headers captured from the live API (test/fixtures/*.b64) and returns a fake settlement.exact on eip155:8453, for USDC, to Blockchain IQ's payTo, for exactly 10000 or 50000 base units, from the signer's address.ToolMessage.Market data and analysis only, not investment advice. Blockchain IQ's terms: https://www.blockchainiq.co/terms
MIT
agentgate-source: code mirror on Cloudflare Pages. All repos