4

How can I get the bitcoin balance of a public key in C#?

E.g. for this public key: 1FqLoEpbQpMxow5bqEPpFbPVnBEbFcsg3J

How can I get its balance in C#?

emcor
  • 141
  • 1
  • 5
  • That looks like an address to me, not a public key. Edited accordingly. Related unanswered question: [C# parse bitcoin blockchain to get balance of an address](http://bitcoin.stackexchange.com/q/37998/5406) – Murch Jun 07 '16 at 13:26
  • Something like https://api.blockcypher.com/v1/btc/main/addrs/38DGj87axzmQiZeAd1w1y5FEmuu5a7pfBa – LF00 Apr 02 '18 at 02:53

3 Answers3

2

If you are ok with a third party service check out the Blockchain API.

They have a C# library and it's fairly easy to use.

If you need only the balance of an address you can use the code below as described on their BlockExplorer page:

 // calculate the balanace of an address by fetching a list of all its unspent outputs
 var outs = blockExplorer.GetUnspentOutputs("1EjmmDULiZT2GCbJSeXRbjbJVvAPYkSDBw");
 long totalUnspentValue = outs.Sum(x => x.Value);
Andras
  • 121
  • 3
  • I tried to implement your example, but it gives only various error messages: using System; using System.Collections.Generic; using System.Linq; using Info.Blockchain.API; using Info.Blockchain.API.BlockExplorer; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var blockExplorer = new BlockExplorer(); var outs = blockExplorer.GetUnspentOutputs("1EjmmDULiZT2GCbJSeXRbjbJVvAPYkSDBw"); long totalUnspentValue = outs.Sum(x => x.Value); } } } – emcor Jun 07 '16 at 18:05
  • It appears this library is outdated and the new version requires a wallet account on their website. However, I am looking for an independent solution because it is faster. – emcor Jun 07 '16 at 20:58
  • @emcor You're right! I've just tested their current version and it seems that they changed a lot and did not update their samples in the documentation. They have their async methods generated and the BlockExplorer must be accessed through the BlockchainApiHelper which needs some parameters we get after applying for an API key (https://api.blockchain.info/v2/apikey/request/ or https://blockchain.info/api/api_create_code). Sorry for the outdated information, my bad! – Andras Jun 08 '16 at 08:52
1

Take a look at Blockparser: https://github.com/znort987/blockparser**

It will let you parse blockchain data to verify balances (among other things)

dontmindme
  • 487
  • 3
  • 7
  • 1
    Thank you. This package requires C++ and Unix, is there a solution in C# on windows? – emcor Jun 07 '16 at 07:33
  • @emcor There may be but I am not familiar with one, sorry. Perhaps someone else can make a better suggestion. – dontmindme Jun 07 '16 at 07:37
0
using Info.Blockchain.API.BlockExplorer;
using Info.Blockchain.API.Models;

internal decimal CheckBalance(string[] base58Addresses)  {
    BlockExplorer be = new BlockExplorer();
    IEnumerable<UnspentOutput> outs = be.GetUnspentOutputsAsync(base58Addresses).Result;
    decimal totalUnspentValue = outs.Sum(x => x.Value.GetBtc());

    return totalUnspentValue;
 }
Iman
  • 101
  • 2