A native of Paris, Maine (part of Massachusetts until 1820), Hamlin managed his father's farm before becoming a newspaper editor. He studied law, was admitted to the bar in 1833, and began to practice in Hampden, Maine. Originally a Democrat, Hamlin began his political career with election to the Maine House of Representatives in 1835 and an appointment to the military staff of the Governor of Maine. As an officer in the militia, he took part in the 1839 negotiations that helped end the Aroostook War. In the 1840s Hamlin was elected to and served in the United States House of Representatives. In 1848 the state house elected him to the United States Senate, where he served until January 1857. He served temporarily as governor for six weeks in the beginning of 1857, after which he returned to the Senate. Hamlin was an active opponent of slavery; he supported the Wilmot Proviso and opposed the Compromise of 1850. In 1854, he strongly opposed passage of the Kansas–Nebraska Act. Hamlin's increasingly anti-slavery views caused him to leave the Democratic Party for the newly formed Republican Party in 1856.