ChainRead / Guides

Base gas price API: what a transaction costs right now

Agents that send transactions should know the fee before they sign. One request returns the fee parameters and the cost in dollars.

The problem

Gas is exposed as eth_gasPrice and eth_feeHistory in wei, which a program must convert to gwei, combine with a gas-unit estimate and multiply by an ETH price from somewhere else. On Base, fees are often a fraction of a cent, so a little precision matters, and the number you want is usually "is this swap worth it?" in dollars.

How /gas answers it

GET /gas?chain=base (or ethereum) reads the latest block and recent fee history and returns baseFeeGwei, gasPriceGwei, three priority-fee tiers (low, standard, fast), and suggestedMaxFeeGwei and suggestedMaxPriorityFeeGwei that you can pass straight into a transaction. It also gives an ETH/USD price from a Chainlink feed and an estimates block with the cost of an ETH transfer (21,000 gas), an ERC-20 transfer (65,000) and a DEX swap (180,000) in ETH and USD. Those gas-unit figures are fixed typical values, so use eth_estimateGas for an exact number on a specific call.

Request and response

Example: Base mainnet.

$ curl -i "https://chainread.imac2014ville.workers.dev/gas?chain=base"
HTTP/2 402
payment-required: eyJ4NDAyVmVyc2lvbiI6Mi4uLn0=   # base64 JSON: scheme "exact", network eip155:8453,
                                                  # asset USDC, amount 1000 (= $0.001)
# An x402 client signs the payment, then retries with a PAYMENT-SIGNATURE header.

The paid response (abridged; fees fluctuate block to block):

{
  "ok": true,
  "chain": "base",
  "blockNumber": 52390330,
  "baseFeeGwei": 0.005,
  "gasPriceGwei": 0.006,
  "priorityFeeGwei": { "low": 0.00066, "standard": 0.001, "fast": 0.0011 },
  "suggestedMaxFeeGwei": 0.011,
  "suggestedMaxPriorityFeeGwei": 0.001,
  "ethUsd": 2485.89,
  "estimates": {
    "ethTransfer":   { "gasUnits": 21000,  "costEth": 1.26e-7,   "costUsd": 0.00031322 },
    "erc20Transfer": { "gasUnits": 65000,  "costEth": 3.9e-7,    "costUsd": 0.0009695 },
    "dexSwap":       { "gasUnits": 180000, "costEth": 0.00000108, "costUsd": 0.00268476 }
  },
  "note": "Estimates cover L2 execution gas only; Base also charges a small L1 data fee on top."
}

JavaScript with @x402/fetch

import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(process.env.PRIVATE_KEY); // wallet holding USDC on Base
const pay = wrapFetchWithPaymentFromConfig(fetch, {
  schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(signer) }],
});
const res = await pay("https://chainread.imac2014ville.workers.dev/gas?chain=base");
const g = await res.json();
if (g.estimates.dexSwap.costUsd > 0.05) console.log("swap is expensive right now");

Pricing

/gas costs $0.001 per call, the same price as /block and /tx-status. There is no API key and no account: each request is paid in USDC on Base over x402. Malformed input returns 400 and failed lookups return a non-2xx status, so those are not charged.

Limitations

More guides

Sister services