4

I'm new in bitcoin, I just want to return json scrutucture from latest transactions from time to time.

I couldn't figure by searching their reference, I think it requires a level of knowledge in bitcoin I don't have yet.

I just wanted to return plain vanilla json array

thanks

2

First of all, there's no way for blockchain.info to know which transactions you have and don't have if it's not in a block.

You can get the full list of transactions not yet in a block using this call:

https://blockchain.info/unconfirmed-transactions?format=json

Otherwise, you can get a continuous stream of new transactions using the websocket API:

https://blockchain.info/api/api_websocket

You can subscribe to unconfirmed transactions by connecting via websocket to

wss://ws.blockchain.info/inv

and sending this:

{"op":"unconfirmed_sub"}

That socket will receive every transaction that blockchain.info receives.

2

If you are looking how to display the live transaction feed on the blockchain you should check out this step-by-step tutorial on using their websocket API.

BROKEN LINK

If you don't want to do websocket and only want a "plain vanilla" JSON array of the most recent unconfirmed transactions you can simply use https://blockchain.info/unconfirmed-transactions?format=json

EDIT: Link no longer available, pasted below

<html>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

<script>

var btcs = new WebSocket('wss://ws.blockchain.info/inv');

btcs.onopen = function()

    {

    btcs.send( JSON.stringify( {"op":"unconfirmed_sub"} ) );

    };


btcs.onmessage = function(onmsg)

    {

    var response = JSON.parse(onmsg.data);

    var amount = response.x.out[0].value;

    var calAmount = amount / 100000000;

    $('#messages').prepend("<p>" + calAmount + "</p>");

    }

</script>

<body>

<div id="messages"></div>

</body>

</html>    

So we started by creating the variable var btcs and then we had that be equal to the new websocket connection. Now that we have a connection to the websocket we need to send it a message and tell blockchain.info which information we are interested in receiving. We are going to use the onopen and send properties on our btcs variable and create a function that subscribes us to {"op":"unconfirmed_sub"} which is new transactions to the bitcoin network.

JSON.stringify will send a JSON request to the websocket server telling it that we want to receive updates on new transactions to the network. At this point the server will start sending "messages" to the client everytime there is a new transaction. We need to tell javascript what to do everytime a message is received using the onmessage command. Create a new function using onmessage and I'm going to name the function onmsg. Within that function we will parse the JSON using the data command.

At this point you could stick this script into an HTML document and when you run it, it will start shooting out all the new transactions as objects in JSON format into your browsers debug/console. If you look at it you'll notice they all start with utx and then all the transaction data is under x. The data within the x array includes input and out. If we want info form the input area we would call the response variable and then add in .x.input. However there can be multiple inputs and outs so we have to clarify which one. If we add in stright brackets and put a zero [0]that will get us information on the first item in the array. Here's an example that will get the BTC amount that was received by the first output.

  • sadly the tutorial is no longer available. I get a 404 – ekkis Mar 16 at 21:26
  • Edited answer to include the sample html page and instructions – m1xolyd1an Mar 17 at 5:17
  • awesome. thank you. sadly, I can't get it to work on the testnet – ekkis Mar 18 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.