To build products around Bitcoin, developers must choose an API to interact with Bitcoin’s blockchain. One way of doing this is through Bitcoin Core’s RPC API, an API maintained by Bitcoin Core developers, embedded in Bitcoin Core’s software. This a useful but very “low-level” API. For instance, the RPC decode a transaction doesn’t return the fee for the transaction, as fees are implicit in Bitcoin. Here’s an example of the data returned by this RPC:
{
"txid": "3982807c799b754443d3a348eb3555a1838be558da461ae2f674cc32b0dc0405",
"hash": "7cc2f86938aad830f8918dd55dad1df2d48b9b098e5ba8cafb570449dd91979c",
"version": 2,
"size": 217,
"vsize": 136,
"weight": 541,
"locktime": 819763,
"vin": [
{
"txid": "434f73068db744bab1858203dead049d7175a18453f3248606e1a734518b9bac",
"vout": 0,
"scriptSig": {
"asm": "0014df06606e7e6f045e33725cebf6900abe3adfad21",
"hex": "160014df06606e7e6f045e33725cebf6900abe3adfad21"
},
"txinwitness": [
"304402202170799dcd15c4c019d76d6424ccb3196e17f42e1ed3c5c3a39f8a2236df177802203f4b1dc3f026adffaf4328079142ce61067cb51336523cfb1756de3089b021c601",
"03424ed8e0cfb4caebd79c1e2f89c5a7bf3985321863036b517069517df3c10ddc"
],
"sequence": 4294967293
}
],
"vout": [
{
"value": 0.00727306,
"n": 0,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 2b72d056e4ef078ec96ebde15591a58514459632 OP_EQUALVERIFY OP_CHECKSIG",
"desc": "addr(14xjbetKE4bJ2JGa27c56LfjZvQbun3rqv)#h6zlzzxw",
"hex": "76a9142b72d056e4ef078ec96ebde15591a5851445963288ac",
"address": "14xjbetKE4bJ2JGa27c56LfjZvQbun3rqv",
"type": "pubkeyhash"
}
}
]
}
If your software requires knowing the fee from third-party transactions, you’ll have to write code to extract the fee by subtracting the total output amount from the input. This is usually too much of a burden to application developers. This is why Bitcoin Core’s RPC ends up being used as the foundation block for library developers to provide a more feature-rich API for developers.
The Esplora API is an advanced alternative to the Bitcoin Core RPC, designed for indexing the entire Bitcoin blockchain to enable fast and efficient queries. It allows applications to track wallets in real time, keeping up with balances and transactions. This functionality makes it invaluable for developers building Bitcoin-related applications or services, as it offers a more comprehensive and user-friendly way to access blockchain data compared to the traditional Bitcoin Core RPC.
Key features of the Esplora API include:
- Comprehensive Transaction Details: It fetches detailed information about specific transactions, including inputs, outputs, fees, size, and more.
- Detailed Block Information: The API provides block data, such as height, transactions included, timestamp, size, and other relevant metrics.
- Real-Time Address and Scripthash Data: It retrieves information about specific Bitcoin addresses or script hashes, including transaction history and unspent transaction outputs (UTXOs), allowing for real-time monitoring of wallet balances and transactions.
- Mempool Statistics: Offers insights into the current state of the mempool, detailing the number of transactions waiting for confirmation, their total size, and associated fees.
- Blockchain Statistics: Includes endpoints for fetching various statistics like the latest block, current block height, and fee estimates for different confirmation targets.
- Asset Information (for Liquid/Elements): For networks like Liquid, the API can provide details about assets, including their transaction history and total supply.
Esplora's ability to index the entire blockchain and its user-friendly API make it a superior choice for developers who require fast and accurate data retrieval capabilities, surpassing the traditional Bitcoin Core RPC in terms of efficiency and ease of use. Whether it's for tracking wallet balances, checking transaction statuses, or analyzing blockchain data, Esplora provides a robust and scalable solution.
Example APIs that you can use on your application
Transaction Information API (GET /tx/:txid)
This API retrieves details about a specific transaction. It returns various fields, including transaction ID, version, lock time, size, weight, fee, inputs (vin), outputs (vout), and the transaction's status.
Transaction Confirmation Status API (GET /tx/:txid/status)
This endpoint provides the confirmation status of a transaction, indicating whether it's confirmed, along with optional fields like block height and block hash if it is confirmed.
Block Information API (GET /block/:hash)
This API returns information about a specific block, including fields like the block ID, height, version, timestamp, merkle root, number of transactions, size, weight, and the previous block hash. Additional fields are available for Element-based chains like Liquid.
Mempool Transactions API (GET /mempool)
This endpoint provides statistics about the current state of the mempool, including the number of transactions in the mempool, total virtual size, total fee in satoshis, and a fee histogram.
Address Information API (GET /address/:address or GET /scripthash/:hash)
This API is used to get information about a specific Bitcoin address or script hash. It returns fields related to the address or script hash, such as chain and mempool statistics, including transaction count, funded and spent transaction outputs, and their sums.
Example
Here’s a Typescript example of using Esplora to get the OP_RETURN data of a transaction:
import axios from 'axios';
async function getOpReturn(txid: string): Promise<void> {
try {
const url = `https://blockstream.info/api/tx/${txid}`;
const response = await axios.get(URL);
const vout = response.data.vout;
const opReturnOutput = vout.find((output: any) => output.scriptpubkey_type === 'op_return');
if (opReturnOutput) {
const script = opReturnOutput.scriptpubkey_asm.split(' ');
const opReturnData = script.at(-1);
const text = hexToText(opReturnData);
console.log(text);
} else {
console.log("No OP_RETURN found in this transaction.");
}
} catch (error) {
console.error("Error fetching transaction data:", error);
}
}
function hexToText(hexStr: string): string {
var hexes = hexStr.match(/.{1,2}/g) || [];
var result = "";
for (var j = 0; j < hexes.length; j++) {
result += String.fromCharCode(parseInt(hexes[j], 16));
}
return result;
}
getOpReturn('d29c9c0e8e4d2a9790922af73f0b8d51f0bd4bb19940d9cf910ead8fbe85bc9b');
Try it out on your computer to see the output.