Get Supported Blockchains and Their Metadata

Overview

The /chains endpoint lists supported Blockchains with their metadata, such as name, logo, etc., which can be used to present Blockchain information to the user.

Let’s first use curl to familiarize ourselves with the data:

curl  --header 'X-API-Key: <YOUR API KEY>' 'https://svc.blockdaemon.com/defi/chains?chainID=10" | jq

❗️

Note:

  • The chains endpoint provides multiple parameters to filter the returned Blockchains. In this example we use chainID=10 to return only Optimism. A full API reference documentation can be found here.
  • This example use the jq tool to format the response to make it easier to read. Feel free to omit it.

The response will look like this:

 [
  {
    "blockExplorerUrls": [
      "https://optimistic.etherscan.io",
      "https://optimism.blockscout.com"
    ],
    "chainID": "10",
    "chainIconURI": "https://defi-images-blockdaemon-research-development-int-aa593eebdaa2fd.gitlab.io/chains/10.webp",
    "chainName": "Optimism",
    "chainType": "evm",
    "nativeCurrency": {
      "decimals": 18,
      "name": "Ether",
      "symbol": "ETH"
    },
    "networkType": "mainnet",
    "rpc": [
      "https://optimism.drpc.org",
      "wss://optimism.drpc.org"
    ]
  }
]

Code Example

https://github.com/Blockdaemon/defi-api-examples/blob/main/src/main/scripts/get-chains.ts contains example code for using the /chains endpoint programmatically.

Please refer to Set Up the Development Environment for the basic setup of the project. To run the script use the command: npx ts-node src/main/scripts/get-chains.ts

Let’s go through the relevant code sections step by step:

  1. We initialise the object ChainsApi with our configuration object (see: Set Up the Development Environment) which includes the API key. The ChainsApi object can then be used to interact programmatically with the /chains endpoint.

    const api = new ChainsApi(apiConfig);
    
  2. We configure the GetChainsRequest object which allows us to request only the subset of Blockchain metadata we are interested in. In this example we query only Polygon Blockchains. To get all available metadata for all supported Blockchains, just leave the object empty. For a list of all possible parameters, please refer to the API specification at Chains.

    const chainsParameters: GetChainsRequest = {};
    
  3. Finally we query the API using the ChainsApi object together with the the parameter object and log the returned data.

    const chains = await api.getChains(chainsParameters);
    log.info("Got chains");
    log.info(chains);
    

👋 Need Help?

Contact us through email or our support page for any issues, bugs, or assistance you may need.