Search code examples
rpchedera-hashgraphhedera

How can I connect to Hedera Testnet over RPC? ​


I am unable to connect to a Hedera Testnet RPC endpoints over HTTP, reliably, I get the following "Unknown error" intermittently: ​

[Request ID: fe7b9928-a23f-0d72-61a1-b7cd23658c01] Unknown error invoking RPC

​ I'm connecting using the following RPC endpoint: ​

https://testnet.hashio.io/api

​ Are there alternative (more reliable) ways to connect to the Hedera Testnet? ​


Solution

  • There are 3 options to connect to Hedera Testnet:

    The above are listed in increasing order of difficulty levels, and increasing order of reliability levels. Unfortunately for increased reliability you need to do a bit more work.

    Currently, you're using Hashio, and I speculate that you may be getting rate limited; or this service might be under higher-than-usual load levels causing it to drop some of its incoming requests. Therefore I suggest that you switch to using Arkhia, and see if that works out for you (chances are it will). And if you really want to minimise reliance on centralised services, or need to debug individual RPC requests, you can consider running your own instance of the RPC relay locally.


    RPC endpoint options

    Hashio

    This is a "public" RPC endpoint, hosted by Swirlds Labs, the same organisation that develops Hedera. As such, it is free to use, and does not require any sign up, and also has the most restrictive rate limits on requests made to it. This makes it easy to use, but not too reliable.

    To connect to the Hedera Testnet via Hashio, simply use this URL when initialising the wallet/ web3 provider instance:

    https://testnet.hashio.io/api
    

    That's all that's needed - no further settings/ config needed

    Arkhia

    This is run by a 3rd party organisation, and has some paid components, with a free tier. As such, it is free to use only up until a point, and requires a sign up thereafter. It has less restrictive rate limits on requests made to it. This makes it about average in terms of ease of use, and also more reliable than using the public RPC endpoint.

    To connect to Hedera Testnet via Arkhia, use this URL when initialising the wallet/ web3 provider instance:

    https://pool.arkhia.io/hedera/testnet/json-rpc/v1/YOUR_API_KEY
    

    In order to get YOUR_API_KEY, you will need to do the following pre-requisite steps:

    • (1) Sign up for an account at auth.arkhia.io/signup
    • (2) Click on link in your confirmation email
    • (3) Click on the "create project" button in the top-right corner of the Arkhia dashboard arkhia-init-step-03.png
    • (4) Fill in whatever you like in the modal dialog that pops up arkhia-init-step-04
    • (5) Click on the "Manage" button on the right side of your newly created project arkhia-init-step-05
    • (6) In the project details, copy the "API Key" field, and the "JSON-RPC" field as well (in case it is different from what I've pasted above). arkhia-init-step-06-07
    • (7) Use the API key in the RPC URL - done!

    hedera-json-rpc-relay on localhost

    This is run by you on your own computer. (Decentralisation for the win!) As such it is free to use without limits (except that of your own hardware). It does not require sign up, but there are several additional steps required to set it up, and requires developer skills/ command line skills to get working.

    To connect to Hedera Testnet via your own instance of hedera-json-rpc-relay, use this URL when initialising the wallet/ web3 provider instance:

    http://localhost:7546
    

    Note that 7546 is the default port number for this project, and you can change this in its config if you wish.

    In order to get this service running, you will need to do the following pre-requisite steps:

    • (1) Clone the git project
    git clone [email protected]:hashgraph/hedera-json-rpc-relay.git
    
    • (2) Create or edit a file named .env in the root directory of this project, with the following fields set:
    HEDERA_NETWORK=testnet
    OPERATOR_ID_MAIN=YOUR_OPERATOR_ID
    OPERATOR_KEY_MAIN=YOUR_OPERATOR_KEY
    CHAIN_ID=0x128
    MIRROR_NODE_URL=https://testnet.mirrornode.hedera.com/
    
    • (3) Visit the Hedera Portal, and create a Testnet account localhost-init-step-03
    • (4) Copy-paste the confirmation code sent to your email localhost-init-step-04
    • (5) Fill out this form localhost-init-step-05
    • (6) In the top-left select between Hedera Testnet (default) and Hedera Previewnet localhost-init-step-06
    • (7A) From the next screen that shows your accounts, from the "Account Ed25519" section, copy the value of "DER encoded private key", and replace YOUR_OPERATOR_KEY in the .env file with it localhost-init-step-07a
    • (7B) Alternatively, from the "Account ECDSA" section, copy the value of the "HEX encoded private key", and replace YOUR_OPERATOR_KEY in the .env file with it localhost-init-step-07n
    • (8) From the same screen, copy the value of "Account ID", and replace YOUR_OPERATOR_ID in the .env file with it
    • (9) Run npm install to install dependencies. Recommended that you have NodeJs version 18 or later for this.
    • (10) Run npm run start to start the RPC relay server.

    Full reference for configuring hedera-json-rpc-relaydocs/configuration.md.