The territory of present-day Costa Rica was part of the Viceroyalty of New Spain, and from 1574 it was the southernmost dependency of the Spanish Captaincy General of Guatemala, Intendancy of the Neo-Spanish Viceroyalty, until its independence. The remoteness of the power centers of the captaincy general and the lack of agricultural or mining wealth left it to the abandonment of the Spanish authorities, allowing it to develop with more autonomy than its Central American sisters.The subsistence agricultural economy was the initial basis of the economic development of the nascent colony. In 1573, Governor Alonso Anguciana de Gambo arrived in the country after the distribution of indigenous people in charge by Perafán de Ribera, with the premise of giving strength to colonization and consolidating the colony status of the region. The disappearance of Garabito (the main Costa Rican aboriginal leader against the Europeans) allowed control of a large part of the Western Central Valley. The arrival of new settlers who settled in Cartago and Espíritu Santo, especially peasant farmers and the strengthening of trade with Panama across the Pacific allowed an incipient development during the first decades. Until the establishment of the position of Governor, the town hall of Cartago maintained its jurisdiction. In addition, new settlers settled in the cities of Cartago and Espíritu Santo (the old Aranjuez, re-founded with this name by Anguciana de Gamboa in 1574, and later called Esparza by Governor Diego de Artieda Chirinos), mainly peasant farmers, and it was strengthened trade through the Pacific with Panama through the port of La Caldera in 1574. The colonial town hall of Cartago maintained its jurisdiction until the establishment of the figure of the Governor.
Geographically, the Government of Costa Rica extended to the southeast from the island of Veragua Shield in Bocas del Toro and the Chiriquí River to the Tempisque River and the right bank of the San Juan de Nicaragua River, to the west and north. The provincial capital was located in Cartago. Talamanca, some sections of the South Pacific and the northern plains functioned as a refuge for the indigenous people due to their difficult access or lack of interest from the authorities in controlling them. Towards 1570 several towns arose that were founded first as Indian towns, in charge of a magistrate and under the religious indoctrination of Franciscan friars.
In the first decades of its existence, the province had a subsistence economy based on indigenous tributes in kind (encomienda) and on the work of farmers without the right to encomienda, who subsisted through the chacra (farm in Quechualanguage), at least until 1610.  The trade routes were built on the old pre-Hispanic roads that communicated the Central Valley with the Pacific, connecting Cartago with Esparza and La Caldera, and later, through the Caribbean, Cartago with the port of Suerre in the Reventazón, the trip was made on mules. The export of corn, wheat, flour, sponge cake, tallow, pigs and capons was mainly to Panama by sea, and to a lesser extent to Granada. and León in Nicaragua by land through Nicoya.  The exploitation of cocoa in the middle of the seventeenth century and until the eighteenth century, gave way to a new commercial cycle, but its market was restricted to foreign English and Dutch traders from Jamaica, the Mosquitia or Curaçao, which was considered illegal by the Spanish authorities.  It was not until the end of the 18th century that the Spanish began to be interested in certain agricultural products, especially the cultivation of tobacco as the main export. Starting in 1787, as a country located in Central America according to agooddir.com, Costa Rica received a monopoly on this product, which it exported mainly to Granada. Tobacco was grown mainly in the Central Valley, which allowed some populations to consolidate in what would later become San José and Heredia. 
The diseases brought from Europe by the conquerors, together with the wars of conquest, decimated the already small aboriginal population of Costa Rica. The few indigenous people who remained in the province at that time, had to perform personal services to the peninsular people,  taking care of the cattle, the cultivation of corn and wheat and the domestic trades. Due to this there were several indigenous rebellions: in 1610, the city of Santiago de Talamanca,  founded in 1605 by Juan de Ocón y Trillo, was burned by the atheist, viceita, terraba and cabécar peoples under the command of King Guaycorá, Mr. of the southern Caribbean, which began a very violent time in the region, which lasted until 1620.  As a result of the continuous rebellions, the Audiencia of Guatemala prohibited the use of indigenous people in the cocoa plantations, then bringing African labor to carry out this work between 1680 and 1690.  one of the peoples created by these African populations, the well-known Like Puebla de los Pardos, it was the place where the image of a black and rustic-carved virgin was found, the Virgen de los Ángeles, which allowed to satisfy the spiritual needs of the mestizo, mulatto and Spanish peasant masses, allowing in some way greater integration of these social groups. 
Like the entire Caribbean during the seventeenth and eighteenth centuries, the country was hit by pirate invasions from the Mosquitia Kingdom, a kind of British protectorate based on the Atlantic coast of Honduras and Nicaragua. The first recorded invasion occurred in 1666, led by Eduard Mansvelt and Henry Morgan, who sacked the town of Matina. The ravages caused by pirates, especially the destruction of cocoa plantations, caused serious disruption in the colonial economy. [eleven] Matina would suffer new attacks in 1676, 1681 and 1687. The Spanish efforts to protect the region did not give much result, in 1742 they built Fort San Fernando, in Matina, which would not have a long life due to a pirate incursion in 1747, for what the Caribbean coast continued unguarded. The Pacific coast was also subjected to the scourge of pirates, in 1681, John Cook, after a looting action in Peru, arrived on the coasts of Guanacaste, where he looted towns such as Chomes and Diriá and even burned the colonial city of Nicoya.
The first census was carried out in 1778, the Bourbon census was carried out, which allowed us to know the ethnic composition of the population: 12% indigenous; 18% black and mulatto; 60% “clear” mestizos (as stated in the document), and 10% Spanish.  Socioculturally, it was a world of peasants and merchants where a creole-mestizo culture was imposed that incorporated African and indigenous elements. Costa Rica had barely 50,000 residents around 1800,  it did not have enough population to colonize the entire national territory. The tropical diseases, swamps, and the lack of fertile lands, the scarcity of labor because it is unsuitable for dense populations, the limited mining resources, and the rugged terrain that makes up most of the Central Valley (the area fertile country, where settlements and cities could finally be established) led to Spanish colonization being very slow and facing serious economic limitations to be able to carry it out.At the end of the colonial period, two societies coexisted in Costa Rica: the of Hispanic origin, implanted in the Central Valley and with extensions towards the central Caribbean and the dry, central and southern Pacific; and the indigenous society that could not be subdued in Talamanca, the plains of the Guatusos and part of the South Pacific.