blockchainiq-x402-langchain

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)

Browse source · blockchainiq-x402-langchain.tar.gz · blockchainiq-x402-langchain.bundle
commit 9782dd9 · 9782dd9efd2e9ba52574806eee4f7132b66f45fa
Clone: git clone https://agentgate-source.pages.dev/blockchainiq-x402-langchain/blockchainiq-x402-langchain.git (branch main, commit 9782dd9efd2e9ba52574806eee4f7132b66f45fa) or curl -O https://agentgate-source.pages.dev/blockchainiq-x402-langchain/blockchainiq-x402-langchain.bundle && git clone blockchainiq-x402-langchain.bundle blockchainiq-x402-langchain
Home: this repo is hosted here (it was never on GitHub).

blockchainiq-langchain

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:

These are standard @langchain/core tools built with tool() and zod schemas. They work with:

How a call works

  1. The tool sends a plain GET. Blockchain IQ answers 402 with PAYMENT-REQUIRED: the price, USDC, Base eip155:8453 and the payTo address.
  2. @x402/fetch with ExactEvmScheme signs an EIP-3009 USDC authorization for exactly that amount with your viem account, then retries with PAYMENT-SIGNATURE.
  3. Blockchain IQ settles the payment and returns 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.

Setup

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

One real paid call ($0.01), with no LLM key needed

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.

Use it in your agent

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.

Options

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)

Tests

npm test compiles with tsc and runs node --test. The suite is fully offline, and any real network call fails it:

Market data and analysis only, not investment advice. Blockchain IQ's terms: https://www.blockchainiq.co/terms

License

MIT


agentgate-source: code mirror on Cloudflare Pages. All repos