The Transfer of the Portuguese Court to Brazil refers to the escape of the Braganza royal family and its court of nearly 15,000 from Lisbon on November 29, 1807. The Braganza royal family departed for Brazil just days before Napoleonic forces captured Lisbon on December 1. The Portuguese crown remained in Brazil from 1808 until the Liberal Revolution of 1820 led to the return of John VI of Portugal on April 26, 1821.For thirteen years, Rio de Janeiro, Brazil, functioned as the capital of the Kingdom of Portugal in what some historians call a “metropolitan reversal” (i.e., a colony exercising governance over the entirety of the Portuguese empire.
In 1807, at the outset of the Peninsular War, Napoleonic forces invaded Portugal due to the Portuguese alliance with the United Kingdom. The Prince Regent at the time, John VI of Portugal, had governed Portugal on behalf of his mother Maria I of Portugal since 1799. Anticipating the invasion of the French army, John VI ordered the transfer of the Portuguese royal court to Brazil before he could be deposed. Setting sail for Brazil on November 29, John VI navigated under the protection of the British Royal Navy, under the command of admiral Sir Sidney Smith. On December 5, almost halfway between Lisbon and Madeira, Sidney Smith, along with Britain’s envoy to Lisbon, Lord Strangford, returned to Europe with part of the British flotilla. Graham Moore, a British sailor and career officer in the Royal Navy, continued escorting the Portuguese royal family to Brazil with the ships Marlborough, London, Bedford and Monarch.
On January 22, 1808, John VI and his court arrived in Salvador, Brazil. It was in Salvador that John VI signed a law that opened commerce between Brazil and friendly nations (primarily the United Kingdom.) This new law, however, broke the colonial pact that, until then, only permitted Brazil to maintain direct commercial relations with Portugal. Secret negotiations in London in 1807 by Portuguese ambassador [Domingos António de Sousa Coutinho] guaranteed British military protection in exchange for British access to Brazil’s ports and to Madeira as a naval base. Coutinho’s secret negotiations paved the way for John VI’s law to come to fruition in 1808. 
On March 7, 1808, the court arrived in Rio de Janeiro. On December 16, 1815, John VI created the United Kingdom of Portugal, Brazil and the Algarves (Reino Unido de Portugal, Brasil e Algarves), elevating Brazil to the same rank as Portugal and increasing the administrative independence of Brazil. Brazilian representatives were elected to the Portuguese Constitutional Courts (Cortes Constitucionais Portuguesas). In 1816, with the death of Queen Maria, John VI became King of the United Kingdom of Portugal, Brazil and the Algarves. After several delays, the ceremony of his acclamation was held in Rio de Janeiro in 1818.
Among the important measures taken by John VI in his years in Brazil were incentives for commerce and industry; the permission to print newspapers and books; the creation of two medical schools; military academies; and the first Bank of Brazil (Banco do Brasil). In Rio de Janeiro he also created a powder factory, a Botanical Garden, an art academy (Escola Nacional de Belas Artes) and an opera house (Teatro São João). All these measures greatly advanced the independence of Brazil in relation to Portugal.
Due to the absence of the king and the economic independence of Brazil, Portugal entered a severe political crisis that obliged John VI and the royal family to return to Portugal in 1821. The heir of John VI, Prince Pedro, remained in Brazil. The Portuguese Cortes demanded that Brazil return to its former status as a colony and the return of the heir to Portugal. Prince Pedro, influenced by the Rio de Janeiro Municipal Senate (Senado da Câmara), refused to return to Portugal during the famous Dia do Fico (January 9, 1822). Political independence came on September 7, 1822, and the prince was crowned emperor in Rio de Janeiro as Dom Pedro I, ending 322 years of colonial dominance of Portugal over Brazil.