Your API has not been initialized correctly and is not connected to a chain

the API is successfully connected and return value, but all ContractPromise cause this error.

Anyone know how to solve this? error screenshot

1 Answer 1


Following your code you shared with me in this question I can see there is an issue with the initialisation of the api variable you are using.

In the useEffect() of your Main.js file you are using the variable api.

const getToken1Contract = new ContractPromise(api, PSP22_ABI, address0)

You initialize it in the TransactionContext.js file in the method connectWallet()

const api = await ApiPromise.create({ provider })

But the issue here is that when app is rendering is trying to access this api variable before the connectWallet() is executed.

You can do that in the setup in Main.js:

 const provider = new WsProvider(WS_PROVIDER)
 const api = await ApiPromise.create({ provider })

Or check that the api variable is initialised it before used and show a message to connect the wallet.

  • I realized why it's happened. And I could fix them! Thanks your help! I changed it to execute after checking api is available like this if (api) {setup()}
    – Steve
    Nov 15, 2022 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.