The history of Peru between 1821 and 1842 is the period considered by the country's official historiography as the first stage of its republican history, formally receiving the name of Foundational Period of the Republic () by historian Jorge Basadre. During this era, what became known as the First Militarism (), a period where several military figures held control of the country, started in 1827 with José de La Mar's presidency, ending in 1844.

The twenty-year period begins on July 28, 1821, when General José de San Martín of the Liberating Expedition of Peru declared the Independence of Peru to a crowd gathered under the balcony of the Casa del Oidor, located at the main square of Lima, until then the capital of the Viceroyalty of Peru. However, Basadre claims that the period only begins, sensu stricto, with the installation of the first Constituent Congress on September 20, 1822.

During this period, the newly formed Peruvian State not only established itself as a proper country, but notably attempted to both expand its territory and preserve its territorial integrity due to the overlapping disputes that arose from the end of the Spanish American wars of independence. To the north, Colombia (which already claimed territory located south of the Caquetá River) declared war on Peru after the latter's military occupation of Bolivia, then under Bolivarian influence. The war concluded without a clear victor through the signing of the Treaty of Guayaquil. After the dissolution of the Greater Colombian state, Ecuador regained its independence and pursued a dispute of its own.

Meanwhile, to the south, another war saw the division of Peru through the establishment of North Peru and South Peru, which shortly after joined Bolivia as the Peru–Bolivian Confederation that was ultimately dissolved in 1839 after a three-year war. With a reunited Peruvian state now reestablished, now president Agustín Gamarra unsuccessfully sought to annex its southern neighbour, which cost him his life during the Battle of Ingavi on November 18, 1841. Gamarra was succeeded by interim President Manuel Menéndez, but his death led to political instability and ultimately served as the beginning of a period of anarchy where multiple generals declared themselves the head of state, beginning with Juan Crisóstomo Torrico on August 16, 1842.

Background

After the Independence of Chile, General José de San Martín, on August 20, 1820, at the head of the Liberating Expedition of Peru, set sail from Valparaíso and landed in the Bay of Paracas in September of that year. He then settled in Pisco from where he sent delegates to convince viceroy Joaquín de la Pezuela to collaborate with independence, which he refused.

Given this response, San Martín moved to Huaura, near Lima, and sent his lieutenant Juan Antonio Álvarez de Arenales to the mountains where he defeated the royalists in the . Later, Viceroy Pezuela was removed from the government and José de la Serna was appointed as the new Viceroy of Peru. He decided to meet with San Martín in Punchauca, but they did not reach any agreement, due to the problems that arose due to the advance of San Martín and his troops.

Viceroy La Serna and his troops ultimately left for the mountains. Consequently, San Martín arrived in Lima and proclaimed the independence of Peru on July 28, 1821, while standing on the balcony of the Casa del Oidor, located at the main square:

Government of San Martín (1821–1822)

San Martín had assumed the military and political command of the so-called free departments of Peru under the title of Protector, as stated in the decree of August 3, 1821. For all practical purposes, Peru was divided militarily and administratively into two parts: that of Lima, the northern coast and the central highlands (under the control of the Patriots) and that of the central and southern parts, headquartered in Cuzco and controlled by the Royal Army of Peru.

Later, the title of Protector was changed to Protector of the Freedom of Peru. During the Protectorate, which lasted just one year and 17 days, the following administrative policies were implemented: The beginning of an autonomous administrative regime after three centuries of Spanish rule. Possibility for the people to choose the system that best suits national interests. The symbols of the country: the first flag and the national anthem. The national currency, fiduciary sign of free economic power. Basic regulation of its commercial system to initiate economic relations with other countries in the world. The creation of the Peruvian Navy and the acquisition of the first ships for its national squadron in order to defend the acquired sovereignty. The basic organization of its military force, to protect internal and external security. The determination of its own educational performance with the founding of the Normal School, as well as the first public schools of free Peru. The first attempt to rescue, value and disseminate national culture through the creation of the National Library of Peru.

The Protectorate was a dictatorship that was based on a Statute, which had the following characteristics: The Statute of Government was a provisional and emergency norm, corresponding to a revolutionary situation for an emerging state, which had achieved its partial independence and was trying to complete it. In its declarative principles it was liberal in nature, because it included the defence of human rights, which had inspired the French Revolution and the American Revolutionary War. The territorial organization of the independent State was based on the departmental system. The Upper Chamber of Justice replaced the Royal Court of Lima and assumed the legal and political functions of the country. It was proposed to create a Council of State, which would support the Protector in his government, made up of several members, among whom would be 3 Creole counts and an Inca marquis.

Other provisions that occurred in Peru, during the Protectorate, were: In a conservative measure, San Martín respected all the titles of the viceregal nobility, changing the name of Titles of Castile to Titles of Peru. The Patriotic Society of Lima () was founded, with the intention of defending the establishment of a Peruvian monarchical regime, of which San Martín was a supporter; but, in practice, its members advocated the republican system. The Order of the Sun of Peru was created to recognize the work of the most distinguished Peruvians and give them a status similar to that of the Titles of Peru. A special commission, made up of Juan García del Río and , traveled to Europe by order of San Martín to look for a prince to come to Peru as king. They left Peru in December 1821 and arrived in London in September 1822, the time when the Protectorate was coming to an end. Although they were replaced by Ortiz de Zevallos and John Parish Robertson, the idea of the republican system had been consolidated in Peru, therefore, the commissioners failed in their attempts. The first members of San Martín's cabinet were: Juan García del Río, Minister of Foreign Affairs; Bernardo de Monteagudo, Minister of War and Navy; and Hipólito Unanue, Minister of Finance. The first was a native of Cartagena de Indias, the second from the province of Tucumán and the third from Arica. José de la Riva Agüero, a young and rich aristocrat from Lima who had collaborated intensely for the cause of freedom, was appointed Prefect of Lima.

Disaster at Macacona

San Martín's main issue was that of the war against the royalists, with some reproaching him for not undertaking a total offensive against them as he had done in Chile. However, he was aware of the numerical inferiority of his forces, compared to that of the viceregals who dominated the interior of the country from Jauja to Upper Peru, and numbered a total of 23,000 soldiers, mostly Andean and mestizo recruits. San Martín only had 4,000 troops. An important triumph for the patriots was the surrender of the fortresses of Callao on September 19, 1821, whose leader, Marshal José de La Mar, joined the patriot cause. Meanwhile, Viceroy La Serna reorganized his forces in the central and southern mountains of Peru and in Upper Peru, from where he carried out raids on the coast, destroying a patriot army in the battle of Ica (or La Macacona), on April 7. of 1822.

Independence of Quito

On May 24, 1822, Peruvian–Colombian troops defeated the royalists in the Battle of Pichincha and occupied Quito on May 25. The Peruvian contingent that participated in this battle was made up of 1,600 troops under the command of Colonel Andrés de Santa Cruz and joined the Colombian patriot troop in Saraguro, on February 9, 1822. This event is memorable, because for the first time they converged the two liberating currents, that of the North and that of the South. Later, General Simón Bolívar invaded Guayaquil, with the desire to annex it to Colombia, of which he was its undisputed leader. Both Bolívar and San Martín were convinced that the definition of American independence had to take place on the still-disputed Peruvian soil.

Guayaquil Conference

San Martín could not finish the war against the Spanish. Although the entire north of Peru had voluntarily joined the patriot cause, its central and southern parts remained under the control of viceregal troops. San Martín considered external military aid necessary and in pursuit of it he went to meet with Bolívar in Guayaquil. In what became known as the Guayaquil Conference, held between July 26 and 27, 1822, they discussed three important questions: The fate of Guayaquil, which, being Peruvian territory, was annexed by Bolívar to Colombia. The help that Bolívar had to provide for the common goal of the independence of Peru. The form of government that the nascent republics had to adopt.

The interview did not produce any concrete results. Regarding the first point, Bolívar had already decided that Guayaquil belonged to Colombia and did not admit any discussion on the matter. Regarding the second point, Bolívar offered to send a Colombian auxiliary force of 2,000 men to Peru, which San Martín considered insufficient. And regarding the third point, Bolívar was decidedly republican, thus opposing San Martín's monarchism. Disillusioned, San Martín returned to Peru, already convinced that he should withdraw to give way to Bolívar.

First Constituent Congress

Before the events of Guayaquil, San Martín had convened the First Constituent Congress of Peru, on May 1, 1822. 80 deputies were elected, and this legislature was solemnly installed on September 20, 1822. It was presided over by the clergyman Francisco Xavier de Luna Pizarro. As soon as this First Constituent Congress was installed, it approved a proposition that said: "...that the Constituent Congress of Peru is solemnly constituted and installed, sovereignty resides in the nation, and its exercise in the Congress that legitimately represents it."

After the installation and on the same date, this Congress offered General San Martín dictatorial powers, which he refused. The offer was changed to that of "Founder of the Freedom of Peru and Generalissimo of Arms" (), a title that was accepted by San Martín, although in an honorary manner. His decision to retire was final.