<p>Turns out that electrum doesn't have to be in daemon mode to receive api requests, but it has to be in daemon mode before it supports them.</p>

CC BY-SA 4.0
2018-10-29T18:04:01.677Z
2018-10-29T18:04:01.677Z
user3338098
<p>OK so this is difficult. First you have to set up your Electrum "config" file (it doesn't have an extension) to make sure you can connect. You cannot use mine because it won't work but this is an example of mine. You have to add in "rpcport": XXXXX, with whatever port you want, make sure not to mess up the "," after each comment or Electrum will overwrite your file. </p>

<pre><code>{

"auto_connect": true,
"config_version": 2,
"console-history": [],
"enable_opreturn": false,
"fee_per_kb": 1000,
"gui_last_wallet": "c:\\users\\xxx\\appdata\\roaming\\electroncash\\wallets\\default_wallet",
"is_maximized": false,
"recently_open": [
    "c:\\users\\xxx\\appdata\\roaming\\electroncash\\wallets\\default_wallet"
],
"rpcpassword": "5555555555555555==",
"rpcport": 54777,
"rpcuser": "user"

}
</code></pre>

<p>After you restart Electrum and log in using your password you can now connect using the code below. There are a few problems though. First, Electrum requires a wallet path, but you cannot specify the wallet path using code. This is some problem with linux I think they didn't add this functionality to windows. So the only way you can make sure your wallet is loaded is by the config file above where it says "recently_open" that has to point to your wallet you want to use. If you open the windows Electrum application and change wallets for some reason and then try to use this code below it won't work. Which is stupid but that's why people are having so much trouble with this. </p>

<p>There is a variable you can specify in "params" which is wallet_path but if you try to use it it won't work which is absolutely stupid, just fails so don't bother. </p>

<p>Thanks to this post: <a href="https://github.com/spesmilo/electrum/issues/5124" rel="nofollow noreferrer">https://github.com/spesmilo/electrum/issues/5124</a>
I was able to get mine to work but like I said the wallet_path doesn't work that's why those people where still having trouble. </p>

<p>In this example I'm using "getunusedaddress" instead of get wallet balance but they are both protected resources so you can just change between them. </p>

<pre><code>import java.util.Base64;
import java.io.*;
import java.net.*;
import java.net.HttpURLConnection;


public class daemon_test {



    public static void main(String[] args) {

        String s1 = new String("default_wallet");

        //Set password 

        connect_daemon("{\"id\":\"myquery\",\"method\":\"daemon\",\"params\":{\"config_options\":{\"subcommand\":\"load_wallet\", \"password\":\"susdoimsa;df\"}}}");
        connect_daemon("{\"id\":\"myquery\",\"method\":\"help\"}");
        connect_daemon("{\"id\":\"myquery\",\"method\":\"getunusedaddress\"}");


    }



    public static String connect_daemon(String url_prams) {

        String responseh = new String("");

        try {

            String url = "http://127.0.0.1:54777";//&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; set the PORT you are using
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setReadTimeout(30000);

            //SET USER NAME &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
            String encoding = Base64.getEncoder().encodeToString("user:5555555555555555==".getBytes());

            //add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("Authorization", "Basic " + encoding);
            //con.setRequestProperty("method", "Basic " + encoding);

            String urlParameters = url_prams;//command=returnTradeHistory

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            //System.out.println("\nSending 'POST' request to URL : " + url);
            //System.out.println("Post parameters : " + urlParameters);
            //System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            //BufferedReader in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            System.out.println(response.toString());
            responseh = response.toString();

        }catch(Exception e){

            e.printStackTrace();
            responseh = e.getMessage();

        }//*****************

        return responseh;

    }

}
</code></pre>

<p>This is the output:</p>

<pre><code>{"result": "qp0c59uccxftskajs5fxy2q7wn4cd03s9qzhlqfh4r", "id": "myquery","error": null}
</code></pre>

CC BY-SA 4.0
2019-09-11T18:25:16.807Z
2019-09-11T18:34:41.743Z
2019-09-11T18:34:41.743Z
Bump
<pre><code>$ ./electrum-3.2.3.exe --testnet setconfig rpcport 7777
true
$ ./electrum-3.2.3.exe --testnet setconfig rpcuser blah
true
$ ./electrum-3.2.3.exe --testnet setconfig rpcpassword blah
true

$ ./electrum-3.2.3.exe --testnet daemon start
Traceback (most recent call last):
  File "run_electrum", line 433, in &lt;module&gt;
AttributeError: module 'os' has no attribute 'fork'
</code></pre>

<p>I can't install on linux as centos 7.5 can't compile python3.  I won't try any further since I have already wasted a day on compilation issues.</p>

<pre><code>$ ./electrum-3.2.3.exe --testnet
</code></pre>

<p>starts up a gui client which is fine</p>

<pre><code>$ curl --data-binary '{"id":"curltext","method":"blockchain.scripthash.get_balance","params":["mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk"]}' http://blah:blah@127.0.0.1:7777
{"result": null, "id": "curltext", "error": {"code": -32601, "message": "Method blockchain.scripthash.get_balance not supported."}}
$ curl --data-binary '{"id":"curltext","method":"get_balance","params":["mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk"]}' http://blah:blah@127.0.0.1:7777
{"result": null, "id": "curltext", "error": {"code": -32601, "message": "Method get_balance not supported."}}
$ curl --data-binary '{"id":"curltext","method":"getbalance","params":["mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk"]}' http://blah:blah@127.0.0.1:7777
{"result": null, "id": "curltext", "error": {"code": -32601, "message": "Method getbalance not supported."}}
</code></pre>

<p>I also tried without specifying the bitcoin address with the same results.</p>

<p><a href="https://bitcointalk.org/index.php?topic=1894185.0" rel="nofollow noreferrer">https://bitcointalk.org/index.php?topic=1894185.0</a>
discusses the same issue and states
<code>isnt giving an error about the wallet being loaded, which is why I am getting the error above</code>.  Assuming the poster didn't mean "isn't" but rather "is", how would I "unload the wallet"?</p>

<p>note: <code>mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk</code> is an address in electrum's wallet</p>

CC BY-SA 4.0
2018-10-19T00:17:41.190Z
try getaddressbalance. also `electrum help` on the command line on a linux system will show you some docs.
Abdussamad
<address><json-rpc><electrum><windows>
How to use electrum RPC to get address balance on windows
user3338098
# How to use electrum RPC to get address balance on windows

(Asked by: username_0 on 2018-10-17)

```
$ ./electrum-3.2.3.exe --testnet setconfig rpcport 7777
true
$ ./electrum-3.2.3.exe --testnet setconfig rpcuser blah
true
$ ./electrum-3.2.3.exe --testnet setconfig rpcpassword blah
true

$ ./electrum-3.2.3.exe --testnet daemon start
Traceback (most recent call last):
  File "run_electrum", line 433, in <module>
AttributeError: module 'os' has no attribute 'fork'

```

I can't install on linux as centos 7.5 can't compile python3. I won't try any further since I have already wasted a day on compilation issues.

```
$ ./electrum-3.2.3.exe --testnet

```

starts up a gui client which is fine

```
$ curl --data-binary '{"id":"curltext","method":"blockchain.scripthash.get_balance","params":["mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk"]}' http://blah:blah@127.0.0.1:7777
{"result": null, "id": "curltext", "error": {"code": -32601, "message": "Method blockchain.scripthash.get_balance not supported."}}
$ curl --data-binary '{"id":"curltext","method":"get_balance","params":["mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk"]}' http://blah:blah@127.0.0.1:7777
{"result": null, "id": "curltext", "error": {"code": -32601, "message": "Method get_balance not supported."}}
$ curl --data-binary '{"id":"curltext","method":"getbalance","params":["mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk"]}' http://blah:blah@127.0.0.1:7777
{"result": null, "id": "curltext", "error": {"code": -32601, "message": "Method getbalance not supported."}}

```

I also tried without specifying the bitcoin address with the same results.

<https://bitcointalk.org/index.php?topic=1894185.0>
discusses the same issue and states
`isnt giving an error about the wallet being loaded, which is why I am getting the error above`. Assuming the poster didn't mean "isn't" but rather "is", how would I "unload the wallet"?

note: `mveNDYcr9Bb1xjnNeCRumiDHKU3n3CJBuk` is an address in electrum's wallet

**Tags:** address, json-rpc, electrum, windows

> **Comments:**
> **username_1**: try getaddressbalance. also `electrum help` on the command line on a linux system will show you some docs.  

---

## 2 answers

### Answer by username_0 on 2018-10-29. Score: 0 | Accepted answer

Turns out that electrum doesn't have to be in daemon mode to receive api requests, but it has to be in daemon mode before it supports them.


---

### Answer by username_2 on 2019-09-11. Score: 0

OK so this is difficult. First you have to set up your Electrum "config" file (it doesn't have an extension) to make sure you can connect. You cannot use mine because it won't work but this is an example of mine. You have to add in "rpcport": XXXXX, with whatever port you want, make sure not to mess up the "," after each comment or Electrum will overwrite your file.

```
{

"auto_connect": true,
"config_version": 2,
"console-history": [],
"enable_opreturn": false,
"fee_per_kb": 1000,
"gui_last_wallet": "c:\\users\\xxx\\appdata\\roaming\\electroncash\\wallets\\default_wallet",
"is_maximized": false,
"recently_open": [
    "c:\\users\\xxx\\appdata\\roaming\\electroncash\\wallets\\default_wallet"
],
"rpcpassword": "5555555555555555==",
"rpcport": 54777,
"rpcuser": "user"

}

```

After you restart Electrum and log in using your password you can now connect using the code below. There are a few problems though. First, Electrum requires a wallet path, but you cannot specify the wallet path using code. This is some problem with linux I think they didn't add this functionality to windows. So the only way you can make sure your wallet is loaded is by the config file above where it says "recently\_open" that has to point to your wallet you want to use. If you open the windows Electrum application and change wallets for some reason and then try to use this code below it won't work. Which is stupid but that's why people are having so much trouble with this.

There is a variable you can specify in "params" which is wallet\_path but if you try to use it it won't work which is absolutely stupid, just fails so don't bother.

Thanks to this post: <https://github.com/spesmilo/electrum/issues/5124>
I was able to get mine to work but like I said the wallet\_path doesn't work that's why those people where still having trouble.

In this example I'm using "getunusedaddress" instead of get wallet balance but they are both protected resources so you can just change between them.

```
import java.util.Base64;
import java.io.*;
import java.net.*;
import java.net.HttpURLConnection;

public class daemon_test {

    public static void main(String[] args) {

        String s1 = new String("default_wallet");

        //Set password 

        connect_daemon("{\"id\":\"myquery\",\"method\":\"daemon\",\"params\":{\"config_options\":{\"subcommand\":\"load_wallet\", \"password\":\"susdoimsa;df\"}}}");
        connect_daemon("{\"id\":\"myquery\",\"method\":\"help\"}");
        connect_daemon("{\"id\":\"myquery\",\"method\":\"getunusedaddress\"}");

    }

    public static String connect_daemon(String url_prams) {

        String responseh = new String("");

        try {

            String url = "http://127.0.0.1:54777";//<<<<<<<<<<< set the PORT you are using
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setReadTimeout(30000);

            //SET USER NAME >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            String encoding = Base64.getEncoder().encodeToString("user:5555555555555555==".getBytes());

            //add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("Authorization", "Basic " + encoding);
            //con.setRequestProperty("method", "Basic " + encoding);

            String urlParameters = url_prams;//command=returnTradeHistory

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            //System.out.println("\nSending 'POST' request to URL : " + url);
            //System.out.println("Post parameters : " + urlParameters);
            //System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            //BufferedReader in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            System.out.println(response.toString());
            responseh = response.toString();

        }catch(Exception e){

            e.printStackTrace();
            responseh = e.getMessage();

        }//*****************

        return responseh;

    }

}

```

This is the output:

```
{"result": "qp0c59uccxftskajs5fxy2q7wn4cd03s9qzhlqfh4r", "id": "myquery","error": null}

```


---

