I have an API route where I generate a new wallet with web3.Keypair function. Then I want to getOrCreateAssociatedTokenAccount for my spl token and get the balance. The first few times I call this API it says TokenAccountNotFoundError but after calling it a few more times it is working. I saw you can pass a Commitment and ConfirmOptions Object as a parameter but I am not sure what values I should put in. I checked out the getOrCreateAssociatedTokenAccount function and as far as I understand the TokenAccountNotFoundError should never return calling this function. Even if I generate a new wallet, send my spl token there (which means an associated account is already created) and then I use getOrCreateAssociatedTokenAccount on this wallet it will give the same error and after a few tries, it is working. On devnet everything is fine but it just happens on mainnet. Any help appreciated.

2 Answers 2


it is possible the TokenAccount is in the process of creation, and you should wait for confirmed commitment.

From the solana official docs...

For processing many dependent transactions in series, it's recommended to use "confirmed" commitment, which balances speed with rollback safety. For total safety, it's recommended to use"finalized" commitment.

This should already help, if not - I recommend waiting 1 or 2 secs before calling or switch to finalized if waiting 1 or 2 secs sounds hacky.

  • This is the correct answer, need to wait until the tx is confirmed at the minimum. Unfortunately on the front end this can hang for 2-5 seconds during congestion, so suggest you do this in an async way.
    – Whiteseal
    Commented Sep 28, 2022 at 8:18
  • 1
    I await getOrCreateAssociatedTokenAccount with commitment "finalized" but still get an TokenAccountNotFoundError. After retrying a few seconds later it is working. What am I missing?
    – Noah
    Commented Sep 28, 2022 at 8:35
  • One explanation I can think of - it is possible the RPC you are talking to is lagging by a few seconds... Commented Sep 28, 2022 at 9:47

I had a similar problem, so the error is because we are not waiting for the confirmed state of the account creation, and there seems to be a couple of seconds lag on the network. Here is a solution you can use.

  const connection = new web3.Connection(web3.clusterApiUrl("devnet"), {
    commitment: "confirmed",

This will ensure your networks wait for a "confirmed" state before proceeding thereby fixing the issue.

Your Answer

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

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