Lamphun was founded by Queen Chama Thevi as the capital of the Haripunchai Kingdom, the last and most northerly Mon kingdom in the area which now forms Thailand.:77 Around 25 km south of Chiang Mai, it was constructed in the shape of a conch shell, following the Khuang River on its east side and divided by moats at the remaining points of the compass.
Queen Chama Thevi is remembered in the wat of her name, which is said to be the resting place of her ashes. Near the town's main morning market in the south-west of the city is a statue of the queen at which offerings are still made today by citizens.
While still living in the north King Mangrai was visited by some merchants from the Mon Kingdom, and hearing of the wealth of Lamphun he determined to conquer it, even against the advice of his councillors.
As it was deemed impossible to take the city by force, he sent a skillful merchant called Ai Fa to gain the confidence of the King Yi Ba, and in time he became the chief minister and managed to undermine the king's authority.
In 1281, with the people in a state of discontent, Mangrai defeated the Mon Kingdom, and added the city and its wealth to his kingdom, while Yi Ba, the last king of Hariphunchai, was forced to flee south to Lampang. Lamphun was then incorporated into the new Lanna Kingdom. Ai Fa was subsequently appointed king, and King Mangrai began building the fortress Wiang Kum Kam as his new capital.