The Mexican–American War, also known as the Mexican War and in Mexico the American intervention in Mexico, was an armed conflict between the United States of America and the United Mexican States (Mexico) from 1846 to 1848. It followed in the wake of the 1845 American annexation of the independent Republic of Texas, which Mexico still considered its northeastern province and a part of its territory after its "de facto" secession in the 1836 Texas Revolution a decade earlier. 

After its Treaty of Córdoba with obtaining independence in 1821, from the Kingdom of Spain and its Spanish Empire as New Spain for the past 300 years, and a brief experiment with a monarchy government, Mexico became a republic in 1824. It was characterized by considerable instability, leaving it ill-prepared for international conflict only two decades later when war broke out in 1846. Native American raids in Mexico's sparsely settled north in the decades preceding the war prompted the Mexican government to sponsor migration from the U.S.A. on its northeast border (since 1803 with the Louisiana Purchase from the French Empire (France) of Emperor Napoleon I) to the Mexican province of Texas to create a buffer. However, the newly-named "Texians" revolted against the Mexican government of President / dictator Antonio Lopez de Santa Ana, who had usurped the Mexican constitution of 1824, in the subsequent 1836 Texas Revolution, creating a republic not recognized by Mexico, which still claimed it as part of its national territory. In 1845, the Texan Republic agreed to an offer of annexation by the U.S. Congress, and became the 28th state in the Union on December 29 that year.
How long was it?
 a brief experiment with a monarchy government