Bitcoin
Blockchain
C#
.NET
Cryptocurrency

Query LOCAL Bitcoin blockchain with C .NET

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you want trustworthy Bitcoin data without depending on a third-party API, the standard approach is to query your own local node. In .NET, that usually means talking to Bitcoin Core over JSON-RPC, either directly or through a library such as NBitcoin.

Preparing a Local Bitcoin Core Node

To query the chain locally, you need a running bitcoind or Bitcoin Core instance with RPC enabled. A typical bitcoin.conf setup for local development looks like this:

ini
1server=1
2rpcuser=myuser
3rpcpassword=strongpassword
4rpcallowip=127.0.0.1

After starting the node, make sure it has synced enough for the data you want to query. If the node is far behind, RPC calls succeed but the chain data may not reflect current state.

Querying Through NBitcoin

NBitcoin wraps Bitcoin RPC in a .NET-friendly API. Add it to your project:

bash
dotnet add package NBitcoin

Then query block height, a block hash, and the corresponding block:

csharp
1using System;
2using System.Threading.Tasks;
3using NBitcoin;
4using NBitcoin.RPC;
5
6class Program
7{
8    static async Task Main()
9    {
10        var credentials = new RPCCredentialString {
11            UserPassword = new NetworkCredential("myuser", "strongpassword")
12        };
13
14        var rpc = new RPCClient(credentials, new Uri("http://127.0.0.1:8332"), Network.Main);
15
16        int height = await rpc.GetBlockCountAsync();
17        uint256 hash = await rpc.GetBlockHashAsync(height);
18        Block block = await rpc.GetBlockAsync(hash);
19
20        Console.WriteLine($"Height: {height}");
21        Console.WriteLine($"Hash:   {hash}");
22        Console.WriteLine($"Txs:    {block.Transactions.Count}");
23    }
24}

This is the usual entry point for local blockchain inspection from C#.

Querying Specific Transactions

Once you can load blocks, you can inspect transactions by hash. For example:

csharp
1using System;
2using System.Threading.Tasks;
3using NBitcoin;
4using NBitcoin.RPC;
5
6class TxLookup
7{
8    static async Task Main()
9    {
10        var credentials = new RPCCredentialString {
11            UserPassword = new NetworkCredential("myuser", "strongpassword")
12        };
13
14        var rpc = new RPCClient(credentials, new Uri("http://127.0.0.1:8332"), Network.Main);
15        uint256 txId = uint256.Parse("your-transaction-id-here");
16
17        var tx = await rpc.GetRawTransactionAsync(txId);
18        Console.WriteLine(tx.GetHash());
19        Console.WriteLine(tx.Outputs.Count);
20    }
21}

For wallet-related or mempool-related queries, the exact RPC call changes, but the connection pattern stays the same.

Direct JSON-RPC Without a Helper Library

If you do not want an extra package, you can call the RPC endpoint with plain HTTP. The structure is standard JSON-RPC.

csharp
1using System;
2using System.Net.Http;
3using System.Text;
4using System.Threading.Tasks;
5
6class RawRpcExample
7{
8    static async Task Main()
9    {
10        using var client = new HttpClient();
11        var auth = Convert.ToBase64String(Encoding.ASCII.GetBytes("myuser:strongpassword"));
12        client.DefaultRequestHeaders.Authorization =
13            new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth);
14
15        var json = """
16        {"jsonrpc":"1.0","id":"demo","method":"getblockcount","params":[]}
17        """;
18
19        var response = await client.PostAsync(
20            "http://127.0.0.1:8332/",
21            new StringContent(json, Encoding.UTF8, "application/json"));
22
23        Console.WriteLine(await response.Content.ReadAsStringAsync());
24    }
25}

This is useful when you want maximum control or need an RPC call your wrapper does not expose conveniently.

Common Pitfalls

The most common mistake is trying to query a local node before enabling RPC in bitcoin.conf. A synced GUI wallet alone is not enough if the RPC server is unavailable.

Another issue is using the wrong network. Mainnet, testnet, and regtest have different ports and different chain data, so make sure the Network value and node configuration agree.

A third pitfall is exposing RPC too broadly. For local development, keep rpcallowip narrow and never publish your node's RPC endpoint carelessly.

Finally, remember that blockchain queries are only as complete as your node state. If the node is still syncing, "missing" recent data may simply not be on disk yet.

Summary

  • Querying a local Bitcoin blockchain from .NET usually means calling Bitcoin Core's JSON-RPC interface.
  • 'NBitcoin provides a convenient wrapper for common block and transaction queries.'
  • Direct HTTP JSON-RPC is also a valid option when you want low-level control.
  • Keep RPC credentials local and locked down.
  • Verify node sync state and network selection before debugging application code.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.