/ doc / JSON-RPC-interface.md
JSON-RPC-interface.md
  1  # JSON-RPC Interface
  2  
  3  The headless daemon `bitcoind` has the JSON-RPC API enabled by default, the GUI
  4  `bitcoin-qt` has it disabled by default. This can be changed with the `-server`
  5  option. In the GUI it is possible to execute RPC methods in the Debug Console
  6  Dialog.
  7  
  8  ## Endpoints
  9  
 10  There are two JSON-RPC endpoints on the server:
 11  
 12  1. `/`
 13  2. `/wallet/<walletname>/`
 14  
 15  ### `/` endpoint
 16  
 17  This endpoint is always active.
 18  It can always service non-wallet requests and can service wallet requests when
 19  exactly one wallet is loaded.
 20  
 21  ### `/wallet/<walletname>/` endpoint
 22  
 23  This endpoint is only activated when the wallet component has been compiled in.
 24  It can service both wallet and non-wallet requests.
 25  It MUST be used for wallet requests when two or more wallets are loaded.
 26  
 27  This is the endpoint used by bitcoin-cli when a `-rpcwallet=` parameter is passed in.
 28  
 29  Best practice would dictate using the `/wallet/<walletname>/` endpoint for ALL
 30  requests when multiple wallets are in use.
 31  
 32  ### Examples
 33  
 34  ```sh
 35  # Get block count from the / endpoint when rpcuser=alice and rpcport=38332
 36  $ curl --user alice --data-binary '{"jsonrpc": "2.0", "id": "0", "method": "getblockcount", "params": []}' -H 'content-type: application/json' localhost:38332/
 37  
 38  # Get balance from the /wallet/walletname endpoint when rpcuser=alice, rpcport=38332 and rpcwallet=desc-wallet
 39  $ curl --user alice --data-binary '{"jsonrpc": "2.0", "id": "0", "method": "getbalance", "params": []}' -H 'content-type: application/json' localhost:38332/wallet/desc-wallet
 40  
 41  ```
 42  
 43  ## Parameter passing
 44  
 45  The JSON-RPC server supports both _by-position_ and _by-name_ [parameter
 46  structures](https://www.jsonrpc.org/specification#parameter_structures)
 47  described in the JSON-RPC specification. For extra convenience, to avoid the
 48  need to name every parameter value, all RPC methods accept a named parameter
 49  called `args`, which can be set to an array of initial positional values that
 50  are combined with named values.
 51  
 52  Examples:
 53  
 54  ```sh
 55  # "params": ["mywallet", false, false, "", false, false, true]
 56  bitcoin-cli createwallet mywallet false false "" false false true
 57  
 58  # "params": {"wallet_name": "mywallet", "load_on_startup": true}
 59  bitcoin-cli -named createwallet wallet_name=mywallet load_on_startup=true
 60  
 61  # "params": {"args": ["mywallet"], "load_on_startup": true}
 62  bitcoin-cli -named createwallet mywallet load_on_startup=true
 63  ```
 64  
 65  ## Versioning
 66  
 67  The RPC interface might change from one major version of Bitcoin Core to the
 68  next. This makes the RPC interface implicitly versioned on the major version.
 69  The version tuple can be retrieved by e.g. the `getnetworkinfo` RPC in
 70  `version`.
 71  
 72  Usually deprecated features can be re-enabled during the grace-period of one
 73  major version via the `-deprecatedrpc=` command line option. The release notes
 74  of a new major release come with detailed instructions on what RPC features
 75  were deprecated and how to re-enable them temporarily.
 76  
 77  ## JSON-RPC 1.1 vs 2.0
 78  
 79  The server recognizes [JSON-RPC v2.0](https://www.jsonrpc.org/specification) requests
 80  and responds accordingly. A 2.0 request is identified by the presence of
 81  `"jsonrpc": "2.0"` in the request body. If that key + value is not present in a request,
 82  the legacy JSON-RPC v1.1 protocol is followed instead, which was the only available
 83  protocol in v27.0 and prior releases.
 84  
 85  || 1.1 | 2.0 |
 86  |-|-|-|
 87  | Request marker | `"version": "1.1"` (or none) | `"jsonrpc": "2.0"` |
 88  | Response marker | (none) | `"jsonrpc": "2.0"` |
 89  | `"error"` and `"result"` fields in response | both present | only one is present |
 90  | HTTP codes in response | `200` unless there is any kind of RPC error (invalid parameters, method not found, etc) | Always `200` unless there is an actual HTTP server error (request parsing error, endpoint not found, etc) |
 91  | Notifications: requests that get no reply | (not supported) | Supported for requests that exclude the "id" field. Returns HTTP status `204` "No Content" |
 92  
 93  ## Security
 94  
 95  The RPC interface allows other programs to control Bitcoin Core,
 96  including the ability to spend funds from your wallets, affect consensus
 97  verification, read private data, and otherwise perform operations that
 98  can cause loss of money, data, or privacy.  This section suggests how
 99  you should use and configure Bitcoin Core to reduce the risk that its
100  RPC interface will be abused.
101  
102  - **Securing the executable:** Anyone with physical or remote access to
103    the computer, container, or virtual machine running Bitcoin Core can
104    compromise either the whole program or just the RPC interface.  This
105    includes being able to record any passphrases you enter for unlocking
106    your encrypted wallets or changing settings so that your Bitcoin Core
107    program tells you that certain transactions have multiple
108    confirmations even when they aren't part of the best block chain.  For
109    this reason, you should not use Bitcoin Core for security sensitive
110    operations on systems you do not exclusively control, such as shared
111    computers or virtual private servers.
112  
113  - **Securing local network access:** By default, the RPC interface can
114    only be accessed by a client running on the same computer and only
115    after the client provides a valid authentication credential (username
116    and passphrase).  Any program on your computer with access to the file
117    system and local network can obtain this level of access.
118    Additionally, other programs on your computer can attempt to provide
119    an RPC interface on the same port as used by Bitcoin Core in order to
120    trick you into revealing your authentication credentials.  For this
121    reason, it is important to only use Bitcoin Core for
122    security-sensitive operations on a computer whose other programs you
123    trust.
124  
125  - **Securing remote network access:** You may optionally allow other
126    computers to remotely control Bitcoin Core by setting the `rpcallowip`
127    and `rpcbind` configuration parameters.  These settings are only meant
128    for enabling connections over secure private networks or connections
129    that have been otherwise secured (e.g. using a VPN or port forwarding
130    with SSH or stunnel).  **Do not enable RPC connections over the public
131    Internet.**  Although Bitcoin Core's RPC interface does use
132    authentication, it does not use encryption, so your login credentials
133    are sent as clear text that can be read by anyone on your network
134    path.  Additionally, the RPC interface has not been hardened to
135    withstand arbitrary Internet traffic, so changing the above settings
136    to expose it to the Internet (even using something like a Tor onion
137    service) could expose you to unconsidered vulnerabilities.  See
138    `bitcoind -help` for more information about these settings and other
139    settings described in this document.
140  
141      Related, if you use Bitcoin Core inside a Docker container, you may
142      need to expose the RPC port to the host system.  The default way to
143      do this in Docker also exposes the port to the public Internet.
144      Instead, expose it only on the host system's localhost, for example:
145      `-p 127.0.0.1:8332:8332`
146  
147  - **Secure authentication:** By default, when no `rpcpassword` is specified, Bitcoin Core generates unique
148    login credentials each time it restarts and puts them into a file
149    readable only by the user that started Bitcoin Core, allowing any of
150    that user's RPC clients with read access to the file to login
151    automatically.  The file is `.cookie` in the Bitcoin Core
152    configuration directory, and using these credentials is the preferred
153    RPC authentication method.  If you need to generate static login
154    credentials for your programs, you can use the script in the
155    `share/rpcauth` directory in the Bitcoin Core source tree.  As a final
156    fallback, you can directly use manually-chosen `rpcuser` and
157    `rpcpassword` configuration parameters---but you must ensure that you
158    choose a strong and unique passphrase (and still don't use insecure
159    networks, as mentioned above).
160  
161  - **Secure string handling:** The RPC interface does not guarantee any
162    escaping of data beyond what's necessary to encode it as JSON,
163    although it does usually provide serialized data using a hex
164    representation of the bytes. If you use RPC data in your programs or
165    provide its data to other programs, you must ensure any problem strings
166    are properly escaped. For example, the `createwallet` RPC accepts
167    arguments such as `wallet_name` which is a string and could be used
168    for a path traversal attack without application level checks. Multiple
169    websites have been manipulated because they displayed decoded hex strings
170    that included HTML `<script>` tags. For this reason, and others, it is
171    recommended to display all serialized data in hex form only.
172  
173  ## RPC consistency guarantees
174  
175  State that can be queried via RPCs is guaranteed to be at least up-to-date with
176  the chain state immediately prior to the call's execution. However, the state
177  returned by RPCs that reflect the mempool may not be up-to-date with the
178  current mempool state.
179  
180  ### Transaction Pool
181  
182  The mempool state returned via an RPC is consistent with itself and with the
183  chain state at the time of the call. Thus, the mempool state only encompasses
184  transactions that are considered mine-able by the node at the time of the RPC.
185  
186  The mempool state returned via an RPC reflects all effects of mempool and chain
187  state related RPCs that returned prior to this call.
188  
189  ### Wallet
190  
191  The wallet state returned via an RPC is consistent with itself and with the
192  chain state at the time of the call.
193  
194  Wallet RPCs will return the latest chain state consistent with prior non-wallet
195  RPCs. The effects of all blocks (and transactions in blocks) at the time of the
196  call is reflected in the state of all wallet transactions. For example, if a
197  block contains transactions that conflicted with mempool transactions, the
198  wallet would reflect the removal of these mempool transactions in the state.
199  
200  However, the wallet may not be up-to-date with the current state of the mempool
201  or the state of the mempool by an RPC that returned before this RPC. For
202  example, a wallet transaction that was BIP-125-replaced in the mempool prior to
203  this RPC may not yet be reflected as such in this RPC response.
204  
205  ## Limitations
206  
207  There is a known issue in the JSON-RPC interface that can cause a node to crash if
208  too many http connections are being opened at the same time because the system runs
209  out of available file descriptors. To prevent this from happening you might
210  want to increase the number of maximum allowed file descriptors in your system
211  and try to prevent opening too many connections to your JSON-RPC interface at the
212  same time if this is under your control. It is hard to give general advice
213  since this depends on your system but if you make several hundred requests at
214  once you are definitely at risk of encountering this issue.