Correct Bitcoin Fee

im always have the error of fee is too hight or low and sometimes insufficient priority… is there a way to get the correct fee for the transaction… i tried transaction.getFee() and transaction._estimateFee()

but has error insufficient priority.

Have you tried using the estimatefee RPC method from bitcoind to determine fees per kilobyte?

bitcoin-cli estimatefee 2