You can use Tenderly API to do it, here is a sample:
GET https://api.tenderly.co/api/v1/public-contract/{chainId}/tx/{txHash}
Set the chain id and transaction hash in the request path.
By calling this API, you could check the failure reason in the error_message
field or in the stack_trace[i].error
Here is a sample failed transaction:
https://polygonscan.com/tx/0x7473494c4d6ac6976193f67e624dbd0963a321dc883ea8d90e115f5246a3b6d2
As it is polygon and polygon chain id is 137, you could call this API like this:
curl 'https://api.tenderly.co/api/v1/public-contract/137/tx/0x7473494c4d6ac6976193f67e624dbd0963a321dc883ea8d90e115f5246a3b6d2' \
-H 'authority: api.tenderly.co' \
-H 'accept: application/json, text/plain, */*' \
-H 'accept-language: fa,en-US;q=0.9,en;q=0.8,es;q=0.7' \
-H 'origin: https://dashboard.tenderly.co' \
-H 'referer: https://dashboard.tenderly.co/' \
--compressed
This is the API response:
{
"hash": "0x7473494c4d6ac6976193f67e624dbd0963a321dc883ea8d90e115f5246a3b6d2",
"error_message": "ERC20: transfer amount exceeds balance",
...
}