Mexico obtained independence from the Spanish Empire with the Treaty of Córdoba in 1821 after a decade of conflict between the royal army and insurgents for independence, with no foreign intervention. The conflict ruined the silver-mining districts of Zacatecas and Guanajuato. Mexico began as a sovereign nation with its future financial stability from its main export destroyed. Mexico briefly experimented with monarchy, but became a republic in 1824. This government was characterized by instability, and it was ill-prepared for a major international conflict when war broke out with the U.S. in 1846. Mexico had successfully resisted Spanish attempts to reconquer its former colony in the 1820s and resisted the French in the so-called Pastry War of 1838 but the secessionists' success in Texas and the Yucatán against the centralist government of Mexico showed its political weakness as the government changed hands multiple times. The Mexican military and the Catholic Church in Mexico, both privileged institutions with conservative political views, were stronger politically than the Mexican state.
The United States' 1803 Louisiana Purchase resulted in an undefined border between Spanish colonial territories and the U.S. Some of the boundary issues between the U.S. and Spain were resolved with the Adams-Onís Treaty of 1818. With the Industrial Revolution across the Atlantic increasing the demand for cotton for textile factories, there was a large external market of a valuable commodity produced by enslaved African-American labor in the southern states. This demand helped fuel expansion into northern Mexico. Northerners in the U.S. sought to develop the country's existing resources and expand the industrial sector without expanding the nation's territory. The existing balance of sectional interests would be disrupted by the expansion of slavery into new territory. The Democratic Party, to which President Polk belonged, in particular strongly supported expansion.