Feudal era
In the 13th and 14th centuries, the territory of the present Shaki district was a part of the state of Shirvanshahs. Management of Shaki was entrusted to the son of Rashid al-Din Hamadani – Jalat In the 30s of the 14th century, the local Oirat tribe took power. After the collapse of the Hulagu Khan's rule in the first half of the 14th century, Shaki gained independence under the rule of Sidi Ahmed Orlat. In 1392, Emir Timur captured Shaki, and the ruler of Shaki, Seyid Ali, was killed. Seyid Ali's son, Seyid Ahmed, who came to power, along with Shirvanshah Ibrahim I Derbendi, accompanied Timur on his third campaign against Azerbaijan in 1399. In 1444, Shaki, then known as Nukhi, was ruled by a Muslim family of Armenian origin whose reign lasted till 1551. In the early 1500s, Safavid king Ismail I (r. 1501–1524) conquered the area, but the town continued to be governed by its hereditary rulers, under Safavid suzerainty.
Ismail's son and successor Shah Tahmasp (r. 1524–1576) put an end to this, and in 1551, he appointed the first Qizilbash governor to rule the town. The territory was annexed to the Safavid dynasty as the independent Sheki beylerbey reigned by Toygun-bey Qajar.
Safavid rule was twice briefly interrupted by the Ottomans between 1578 and 1603 and 1724–1735. In 1734-1735, there was a revolt of poor people against the policy of Nadir Shah in the village of Bilecik (Shaki)
In 1741, there was another uprising against the local ruler, Melik Najaf. Appointed by Nadir Shah, Haji Chelebi, who claimed descent from the hereditary Muslim-Armenian rulers of the province, announced the formation of an independent Sheki khanate in 1743. Upon learning of this, Nadir Shah Afshar sent his army to Shaki. Haji Chelebi took refuge in the fortress of Gelesen-Geresen. In 1746, Haji Celebi was forced to recognize the authority of Nadir Shah. However, new uprisings and the death of Nadir Shah allowed Haji Chelebi to re-declare himself Khan During the existence of Shaki khanate, the local population of the city was engaged in silkworm breeding, craft and trade. As a result of a flood in the river Kish, the city of Shaki was partially ruined and the population was resettled in the present day city.
In alliance with the Shamakhi Khan, in 1748 Haji Chelebi attempted to besiege the Bayat fortress. The defeat in the battle of Bayat, which lasted for a month, had been a serious setback for allies.
The Jaro-Balakan Jamaat, Qabala and Ares sultanates were dependent on the Shaki khanate
In 1751, Haji Chelebi defeated the army of the Kakheti king Heraclius II. At the initiative of Heraclius II, a political conspiracy of the Kakheti Kingdom, the Karabakh, Ganja, Irevan, Nakhichevan, and Karadag khanates against the Shaki khan was arranged. In 1752, in the area of Kyzylgaya, Georgian troops unexpectedly attacked the khans: they were captured. Haji Celebi himself defeated the Georgians in the battle near Ganja and came to the aid of the khans. The army of Shaki khan captured Gazakh and Borchali.
In 1767, the Western part of the Shamakhi khanate was annexed to the Shaki khanate.
In 1785, the Shaki khanate became dependent on the Guba khanate. However, this did not last long: after the death of Fatali khan of Guba, the Shaki khanate regained its independence.
During the reign of Selim khan, the territory of the khanate was conditionally divided into 8 magals, which were ruled by naibs directly appointed by the khan himself.
On May 21, 1805, the treaty of Kyurekchay was signed between Russia and the Shaki khanate, the main condition of which was the annexation of the Shaki khanate to Russia. In 1806, the Russian army moved to Shaki. Selim khan was removed from positions of power. A temporary Board of Pro-Russian beks was created.