<p>The API returns <code>No free outputs to spend</code> when there's no unspent output, you need to account for that case. You can do a simple check on the status code (200: OK, 500: Error).</p>

CC BY-SA 3.0
2017-07-25T13:22:02.230Z
2017-07-25T13:22:02.230Z
alcio
<p>A script that calls the blockchain.info API to find the UTXO related to an address</p>

<pre><code># get unspent outputs from blockchain API

import json
import requests

# example address
address = '1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX'

# The API URL is https://blockchain.info/unspent?active=&lt;address&gt;
# It returns a JSON object with a list "unspent_outputs", containing UTXO, like this:
#{      "unspent_outputs":[
#   {
#     "tx_hash":"ebadfaa92f1fd29e2fe296eda702c48bd11ffd52313e986e99ddad9084062167",
#     "tx_index":51919767,
#     "tx_output_n": 1,
#     "script":"76a9148c7e252f8d64b0b6e313985915110fcfefcf4a2d88ac",
#     "value": 8000000,
#     "value_hex": "7a1200",
#     "confirmations":28691
#   },
# ...
#]}

resp = requests.get('https://blockchain.info/unspent?active=%s' % address)
utxo_set = json.loads(resp.text)["unspent_outputs"]

for utxo in utxo_set:
    print "%s:%d - %ld Satoshis" % (utxo['tx_hash'], utxo['tx_output_n'], utxo['value'])
</code></pre>

<p>Running this script, shows the error:</p>

<pre><code>Traceback (most recent call last):
  File "get-utxo.py", line 26, in &lt;module&gt;
    utxo_set = json.loads(resp.text)["unspent_outputs"]
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
</code></pre>

<bitcoind><json-rpc><python>
When I call the blockchain.info API, I an error. What can i do to solve this problem?
Tonytan
# When I call the blockchain.info API, I an error. What can i do to solve this problem?

(Asked by: username_0 on 2017-07-25)

A script that calls the blockchain.info API to find the UTXO related to an address

```
# get unspent outputs from blockchain API

import json
import requests

# example address
address = '1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX'

# The API URL is https://blockchain.info/unspent?active=<address>
# It returns a JSON object with a list "unspent_outputs", containing UTXO, like this:
#{      "unspent_outputs":[
#   {
#     "tx_hash":"ebadfaa92f1fd29e2fe296eda702c48bd11ffd52313e986e99ddad9084062167",
#     "tx_index":51919767,
#     "tx_output_n": 1,
#     "script":"76a9148c7e252f8d64b0b6e313985915110fcfefcf4a2d88ac",
#     "value": 8000000,
#     "value_hex": "7a1200",
#     "confirmations":28691
#   },
# ...
#]}

resp = requests.get('https://blockchain.info/unspent?active=%s' % address)
utxo_set = json.loads(resp.text)["unspent_outputs"]

for utxo in utxo_set:
    print "%s:%d - %ld Satoshis" % (utxo['tx_hash'], utxo['tx_output_n'], utxo['value'])

```

Running this script, shows the error:

```
Traceback (most recent call last):
  File "get-utxo.py", line 26, in <module>
    utxo_set = json.loads(resp.text)["unspent_outputs"]
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

```

**Tags:** bitcoind, json-rpc, python


---

## 1 answers

### Answer by username_1 on 2017-07-25. Score: 2

The API returns `No free outputs to spend` when there's no unspent output, you need to account for that case. You can do a simple check on the status code (200: OK, 500: Error).


---

