The Anglo-Nepalese War and the Rana Era. The Anglo-Nepalese War (1814-1816) was fought between the expanding Gurkha kingdom of Prithvi Narayan Shah's heirs and the expanding East India Company, over the Himalayan borderlands. Two years of mountain war - the Gurkhas' kukris against the Company's guns, in forts like Nalapani, where Balbhadra Kunwar's defense became legend on both sides - ended with the Treaty of Sugauli: Nepal ceded a third of its territory (Kumaon, Garhwal, Sikkim's claims) and accepted a British Resident, but was never conquered - the only South Asian state to fight the Company and survive intact.
The war gave both sides a lasting institution: so impressed were the British that they began enlisting the Gurkhas into their own army - the Gurkha regiments that serve Britain and India to this day.
Inside Nepal, the war's aftermath brought a bloody politics of noble factions that ended in the Kot Massacre of 1846: Jung Bahadur Rana butchered his rivals, made himself prime minister, and founded the Rana hereditary premiership (1846-1951) - an extraordinary arrangement in which the Shah kings were kept as ceremonial prisoners in their palace while the Rana prime ministers, office passing brother to brother, ruled as shoguns, building their Italianate palaces (the Singha Durbar), visiting Europe (Jung Bahadur's famous 1850 tour), and keeping Nepal deliberately closed to the world.
The Ranas were loyal allies of Britain - Nepal's troops served in 1857 and both world wars - and their century preserved Nepal's independence at the price of stagnation and isolation. In 1951, after the Congress-backed revolution, King Tribhuvan ended Rana rule and opened the country; the Rana palaces still stand in Kathmandu as monuments of the century when a hereditary premiership ruled a sealed kingdom.