By July 1831, as the new countries of Venezuela and Ecuador were being established, the isthmus would again reiterate its independence, now under the same General Alzuru as supreme military commander. Abuses committed by Alzuru's short-lived administration were countered by military forces under the command of Colonel Tomás de Herrera, resulting in the defeat and execution of Alzuru in August, and the reestablishment of ties with New Granada.
In November 1840, during a civil war that had begun as a religious conflict, the isthmus under the leadership of -now General- Tomás Herrera, who assumed the title of Superior Civil Chief, declared its independence as did multiple other local authorities. The State of Panama took in March 1841 the name of 'Estado Libre del Istmo', or the Free State of the Isthmus. The new state established external political and economic ties and by March 1841, had drawn up a constitution which included the possibility for Panama to rejoin New Granada, but only as a federal district. Herrera's style was first changed to Superior Chief of State in March 1841 and in June 1841 to President. By the time the civil conflict ended and the government of New Granada and the government of the Isthmus had negotiated the Isthmus's reincorporation to the union, Panama's First Republic had been free for 13 months. Reunification happened on December 31, 1841.
In the end, the union between Panama and the Republic of New Granada (under its various names United States of Colombia 1863-1886 and the Republic of Colombia since 1886) was made possible by the active participation of U.S.A. under the 1846 Bidlack Mallarino treaty until 1903.
In the 1840s, two decades after the Monroe Doctrine declared U.S. intentions to be the dominant anti-European imperial power in the Western Hemisphere, North American and French interests became excited about the prospects of constructing railroads and/or canals through Central America to quicken trans-oceanic travel. At the same time it was clear that New Granada's control over the isthmus was turning increasingly untenable. In 1846, the United States and New Granada signed the Bidlack Mallarino Treaty, granting the U.S. rights to build railroads through Panama, and -most significantly- the power to militarily intervene against revolt to guarantee New Granadine control of Panama. The world's first transcontinental railroad, the Panama Railway, was completed in 1855 across the Isthmus from Aspinwall/Colón to Panama City. From 1850 until 1903, the United States used troops to suppress separatist uprisings and quell social disturbances on many occasions, creating a long-term animosity among the Panamanian people against the US military and resentment against Bogotá. The Bidlack-Mallarino Treaty had ushered a new era of U.S. intervention and conflicts which would linger on into the new millennium. The first of many such conflicts was known as the Watermelon War of 1856, where U.S. soldiers mistreated locals causing large-scale race riots that U.S. Marines eventually put down.
Under a federalist constitution that was later brought up in 1858 (and another one in 1863), Panama and other constituent states gained almost complete autonomy on many levels of their administration, which led to an often anarchic national state of affairs that lasted roughly until Colombia's return to centralism in 1886 with the establishment of a new Republic of Colombia.
As was often the case in the new world after independence, the local administrative and political structures were controlled by the remnants of the colonial aristocracy. In the case of Panama, this elite was constituted by a group of under ten extended families. Though Panama has made enormous advances in social mobility and racial integration, it is still true that much of Panama's economic and social life is controlled by a small number of families. The derogatory term rabiblanco ("white tail"), of uncertain origin, has been used for generations to refer to the usually Caucasian members of the elite families.
In 1852 the isthmus would adopt trial by jury in criminal cases and—30 years after abolition—would finally declare and enforce an end to slavery. In 1855, the first Transcontinental railway of the New World, the Panama Railway, was built across the isthmus from Colón to Panama City to transport fortune hunters who wanted quick passage to the gold fields of California. The existence of the railroad made speculation about a Panamanian canal feasible.