How to check that a Bitcoin address is valid in JavaScript

In this tutorial, we are going to learn how to check that a Bitcoin address is valid using JavaScript and the Bitcoin JSON-RPC API.

In this tutorial, we are going to learn how to check that a Bitcoin address is valid using JavaScript and the Bitcoin JSON-RPC API.

If you're using Node.js, you will first need to install the node-fetch library:

npm install node-fetch

and import it: const fetch = require('node-fetch'); .

Then, you will need a Bitcoin Node API to interact with the Bitcoin blockchain. For that, you can use a node provider service such as:

In our example, we use GetBlock because it's free and simple to use. Once you sign up to the service you want to use, get your Bitcoin API URL, we are going to need it to call the validateaddress method of the RPC API:

const isValidAddress = async (address) => {
  const response = await fetch("YOUR_URL_HERE", {
    method: 'POST',
    headers: { 'content-type': 'text/plain' },
    body: JSON.stringify({
      jsonrpc: '2.0',
      id: 'is-valid-address',
      method: 'validateaddress',
      params: [address],
    }),
  });
  const data = await response.text();
  return JSON.parse(data).result.isvalid;
};

This function takes a Bitcoin address as a parameter, calls the RPC API validateaddress method with the address in the params.

Once we get a response from the API, we read it as text and parse it as JSON. Then we read the result and isvalid properties.

If the result property is empty and error is true, then it means the API call failed.

And that's it! 👏

Thanks for reading this article, subscribe to the newsletter to get exclusive content and to not miss when we post content.