India and Pakistan came "close" to a "nuclear conflagration" in February 2019, former US Secretary of State Mike Pompeo has said in his new memoir.
This happened after Delhi launched strikes against militants in Pakistani territory following an attack on Indian troops in Kashmir.
Pakistan had then said it had shot down two Indian military jets and captured a fighter pilot.
India and Pakistan claim all of Kashmir, but control only parts of it.
India has long accused Pakistan of backing separatist militants in the Kashmir valley - a charge Islamabad denies. The nuclear-armed neighbours have fought three wars since independence from Britain and partition in 1947. All but one were over Kashmir.
In Never Give An Inch: Fighting for the America I Love, Mr Pompeo says he does "not think the world properly knows just how close the India-Pakistan rivalry came to spilling over into a nuclear conflagration in February 2019".
"The truth is, I don't know precisely the answer either; I just know it was too close," he writes.
Mr Pompeo says he will "never forget the night" he was in Hanoi at a summit "negotiating with the North Koreans on nuclear weapons" when "India and Pakistan started threatening each other in connection with the decades-long dispute over the northern border region of Kashmir".
After the attack on Indian troops that killed more than 40 soldiers - "an Islamist terrorist attack... probably enabled in part by Pakistan's lax counter-terror policies", according to Mr Pompeo - India had responded with air strikes inside Pakistan. "The Pakistanis shot down a plane in a subsequent dogfight and kept the Indian pilot prisoner."
Mr Pompeo said he was awakened in Hanoi to speak with an Indian "counterpart", who is unnamed.
"He believed the Pakistanis had begun to prepare their nuclear weapons for a strike. India, he informed me, was contemplating its own escalation," Mr Pompeo writes.
"I asked him to do nothing and give us a minute to sort things out."
Mr Pompeo writes he began to work with the then National Security Adviser John Bolton who was with him in the "tiny secure communications facility in our hotel".
He says he reached out to Pakistan's then army chief General Qamar Javed Bajwa, with "whom I had engaged many times", and told him what the "Indians had told me".
"He said it wasn't true. As one might expect, he believed the Indians were preparing their nuclear weapons for deployment. It took us a few hours - and remarkably good work by our teams on the ground in New Delhi and Islamabad - to convince each side that the other was not preparing for nuclear war.
"No other nation would have done what we did that night to avoid a horrible outcome," Mr Pompeo writes.
Neither India nor Pakistan have commented so far on Mr Pompeo's claims.
The 2019 attack on Indian soldiers was claimed by a group based in Pakistan, Jaish-e-Mohammad (JeM), and India had vowed to retaliate.
India's aerial attacks across the Line of Control (LoC) dividing Indian and Pakistani territory were the first since a war in 1971. India said it had killed a large number of militants but Pakistan called the claim "reckless".