How did Panama gain its independence from Colombia?

Panama became independent from Spain in 1821, and separately, again, from Colombia, in 1903, due to US efforts to build the Panama Canal.

Panama was part of the colony of New Granada that became independent from Spain in 1819, and after declaring independence in 1821 became part of the Gran Colombia state (with Venezuela and Colombia). After the dissolution of Gran Colombia by 1830, Panama was again in union with the separated republic of Colombia.

However, the relationship between the Colombian government in Bogota and the local ones on the isthmus was seldom smooth. On numerous occasions, either Colombian or US forces intervened (under the Bidlack-Mallarino treaty of 1846).

During the Thousand Day War of 1899-1902 (Colombian civil war) separatists again began fighting for Panamanian independence. In 1903, when Colombia would not agree with the US over terms for the Panama Canal, US forces blocked the troops and supplies being sent to suppress the rebels. Agents for the victorious Panamanians quickly agreed to cede the Canal Zone to the United States, and it did not return to Panama's control until 1999 under the terms of the 1977 Torrijos-Carter treaty.