Costa Rica obtains its independence along with the other Central American republics that were part of the Captaincy General of Guatemala. The “Acta de los Nublados”, stipulated the independence of the Spanish government and the continuity of the authorities,  the city council of Cartago issued the minutes of October 29, declaring its independence, and, on December 1, a Meeting of Legacies promulgated the Interim Fundamental Social Pact or Pact of Concord, which is considered the first Constitution of the nation because it established the absolute right of the province to constitute its own form of government, also recognizing the civil rights of the residents, proclaiming the freedom of commerce and it was established that the government was in charge of a Superior Governing Board.  On March 3, 1823, the first Congress of Costa Rica was formed with deputies from the four main cities of the Central Valley, deeply local and strongly fragmented among themselves.
Independence brought a confrontation between the cities of Cartago and Heredia, which advocated joining the Mexican Empire of Agustín de Iturbide, and the liberal cities of San José and Alajuela that wanted to be independent. As a result of this confrontation, on April 5, 1823, a battle was fought in Alto de Ochomogo, with victory for the Republicans, led by Gregorio José Ramírez. After this battle, the city of Cartago ceased to be the capital of Costa Rica, which became the city of San José. [fifteen] At the end of the battle and after the surrender, a new Governing Board was appointed that governed until 1824, the year in which the first Head of State, Juan Mora Fernández (1824-1833), a liberal politician with considerable administrative experience, was appointed. From the use of the printing press and under his rule the first mint was established and the House of Education of Santo Tomás was reorganized, considered the first university in Costa Rica. Also under his government, a new constitution was promulgated in 1825, called the Fundamental Law of the Free State of Costa Rica, as well as the Aprilia Law, which temporarily separated Costa Rica from the Federal Republic. The Virgin of the Angels was declared the patron saint of the nation. during his government. 
As a country located in Central America according to 800zipcodes.com, Costa Rica was the last province to join the Federal Republic in 1824, mainly due to the belief that the political and socio-economic ills suffered by the country were due to the abandonment of the authorities based in Guatemala. By 1825 Costa Rica already had its own Supreme Court of Justice, Legislative Assembly, Head of State, and its own currency. In the period between 1825 and 1833, the country experienced a period of relative political calm. In 1833, José Rafael de Gallegos assumed the presidency, who approved a series of somewhat misguided policies, among which the Ambulance Law stood out, which established a rotation of the capital between the main cities of the Central Valley: San José, Alajuela., Heredia and Cartago.
Braulio Carrillo put all his experience as a businessman, lawyer, president of the Legislative Assembly and prosecutor of the Supreme Court to organize the country, so much so that he is considered the architect of the Costa Rican state. Carrillo was in charge of strengthening public institutions, creating the collection of taxes. He encouraged the cultivation of coffee and enacted the Law Against Vagrancy and respect for the authorities. The repeal under his government of the Ambulance Law caused a second Civil War in Costa Rica that ended with the League War and maintained the hegemony of the city of San José. 
Carrillo was defeated in the 1837 elections by Manuel Aguilar Chacón, a politician in favor of strengthening ties with the federation. In 1838 Aguilar was overthrown by a military coup that put Carrillo back in power. In this second term, Braulio repealed the constitution and promulgated a new one in which he named himself head of state for life.  During this new period, it established a series of codes in criminal, civil and procedural matters.
In 1836 the Republic of New Granada seized the Province of Bocas del Toro, a territory that would later become part of the Republic of Panama. Faced with this flagrant violation, the Federal Republic of Central America did not show any support, which caused general discomfort among the residents of Costa Rica, which caused Carrillo to decide on November 15, 1838, to permanently separate the country from the Federal Republic, proclaiming Costa Rica as a free, sovereign and independent state. The constitution issued by Carrillo caused that in 1842, his political adversaries required the help of Francisco Morazán, former president of the Central American Federal Republic in 1830. Morazán overthrew Carrillo, exiling him to El Salvador, where he was assassinated in 1844. However, Morazán’s interests were directed to the reestablishment of the Federal Republic (which had been dissolved in 1839), using Costa Rica as a military base. This attitude made the Liberals turn against him and led to his fall and execution in San José on September 15, 1842.