Bitcoin API Quickstart

💡 Available only with Blast access

Introduction

The Bitcoin API gives developers access to interact with the Bitcoin blockchain through a standard set of JSON-RPC methods. With this API, you can retrieve block and transaction data, inspect the mempool, broadcast transactions, and more.

Unlike EVM chains, Bitcoin uses a UTXO-based model and exposes its functionality via the JSON-RPC protocol. This quickstart will help you make your first request.


What is the Bitcoin Chain API?

The Bitcoin Chain API allows applications to communicate with a Bitcoin node using the JSON-RPC protocol. Unlike Ethereum’s account-based model, Bitcoin relies on a UTXO (Unspent Transaction Output) model, which means that balances are tracked by outputs that are explicitly spent or unspent.

Alchemy’s Bitcoin API gives developers a consistent interface to query blockchain data, submit transactions, and monitor network activity. This includes:

  • Retrieving raw or decoded transaction data
  • Querying block headers and full blocks
  • Monitoring mempool entries and states
  • Submitting and testing raw transactions

If you’ve worked with Ethereum or other JSON-RPC-compatible chains, the structure will feel familiar, though the data returned may differ in structure and semantics.


Getting Started Instructions

1. Choose a Package Manager (npm or yarn)

Your first step involves selecting a package manager, which will be crucial for managing your project’s dependencies. The choice between npm and yarn depends on your personal preference or project requirements.

npmyarn
Begin with npm by following the npm documentation.For yarn, refer to yarn’s installation guide.

2. Set Up Your Project

To kickstart your project, open your terminal and execute the following commands:

mkdir bitcoin-api-quickstart
cd bitcoin-api-quickstart
npm init --yes

This creates a new directory named bitcoin-api-quickstart and initializes a Node.js project within it.

3. Make Your First Request

Install Axios, a popular HTTP client, to make API requests:

bash
$npm install axios
># Or with yarn
># yarn add axios

Create an index.js file in your project directory and paste the following code:

javascript
1const axios = require('axios');
2
3const url = `https://bitcoin-mainnet.alchemy-blast.com/v2/${yourAPIKey}`;
4
5const payload = {
6jsonrpc: '2.0',
7id: 1,
8method: 'getblockcount',
9params: []
10};
11
12axios.post(url, payload)
13.then(response => {
14console.log('Current block height:', response.data.result);
15})
16.catch(error => {
17console.error('Error fetching block count:', error);
18});

Remember to replace yourAPIKey with your actual Alchemy API key that you can get from your Alchemy dashboard.

4. Run Your Script

To execute your script and make a request to the Bitcoin App, run:

bash
$node index.js

You should see the current block number on Bitcoin App (in hexadecimal format) outputted to your console:

Current block height: 0x6d68e

Next Steps

Well done! You’ve just made your first request to the Bitcoin App API. With this foundation, you can dive deeper into the array of JSON-RPC methods available on Bitcoin App and start building your dApps on it!