The state of Yanqi 焉耆 (Türkic name Karašahr), also called Wuyi 烏夷, Yanyi 傿夷, Wuxi 烏耆, Yini 億尼, Yini 憶尼 or Axini 阿耆尼, was located in the modern region of Yanqi, Xinjiang. The native name might have been Argi and was changed to Solmi 唆里米 in the 7th century. After the occupation by Muslim invaders it was called Čališ, and in the 18th century, after the occupation by the Qing dynasty 清 (1644-1911), was given the Türkic name of Qarašahr (Karashar, Chinese transliteration 喀喇沙爾).
The inhabitants spoke a language belonging to the Indo-European family called Tokharian A. From the 7th century on the population was mixed with Türkic immigrants (mainly Uyghurs), in the 18th century with Mongolian (Oirats) and Chinese immigrants.
The ancient capital was Yuanqu 員渠, also called Henan 河南城 (modern ancient city of Bogedabi 博格達泌). The population during the Former Han period 前漢 (206 BCE-8 CE) was 4,000 (or 30,000?) and rose to 15,000 (52,000) during the Later Han period 後漢 (25-220 CE). The inhabitants lived of farming and pasturing, but to a large extent also from the silk trade.
In the early 1st century BCE Yanqi was controlled by the mighty steppe federation of the Xiongnu 匈奴 that had appointed a Commander-in-chief of the Tributary Slaves (tongpu duwei 僮僕都尉) who resided in Yanqi and the neighbouring cities of Weixu 危須 and Weili 尉犁 and collected tributes from the states of the Western Territories (xiyu 西域).
After the armies of the Han empire had conquered the Western Territories, they set up military agro-colonies (tuntian 屯田) in Quli 渠犁 near Yanqi. In 60 BCE the Protectorate of the Western Territories (xihu duhufu 西域都護府) was established whose offices were located in Wulei 烏壘 west of Yanqi. When Wang Mang 王莽 (r. 8-23 CE) usurped the throne of the Han, the Xiongnu used the chance to regain control over the Western Territories. The king of Yanqi joined the rebellion and killed the envoys of Wang Mang, general Wang Jun 王駿 and Protector-general Li Chong 李崇.
Yanqi again became a subject to the Han in 45 CE, when a large envoy of 18 states was sent to the Han court to ask for a reestablishment of the protectorate. Emperor Guangwu 漢光武帝 (r. 25-57 CE) declined, and only in 73 CE a Han army arrived in Yanqi to expell the Xiongnu and brought a new Protector-general, Chen Mu 陳睦. The latter and his whole staff were killed two years later by Yanqi and
After the downfall of the Han, Yanqi became one of the powerful states of the region and conquered its rival Qiuci. King Long An 龍安 had once been insulted by King Bai-shan 白山 of Qiuci. Long An's son Long Hui 龍會 took revenge for his father and occupied Qiuci, while his own son Long Xi 龍熙 ruled over Yanqi. Yanqi suffered defeat against the armies of the Former Liang empire 前涼 (314-376) in 345 and accepted that a Chief Clerk in Command of Troops (zhangshi 長史) was sent as a governor to Yanqi. The kings of Yanqi also accepted the Former Qin 前秦 (351-395) and the Northern Wei 北魏 (386-534) dynasties as their overlords.
In 448 the Northern Wei court accused Yanqi having plundered their embassadors and sent out general Wan Dugui 萬度歸 to conquer Yanqi and Cheshi 車師. King Jiu-shi-bi-na 鳩尸卑那 fled to Qiuci. This defeat brought an end to the glorious phase of Yanqi that was eclipsed by the steppe federations of the Rouran 柔然 and the Gaoche 高車. The Long dynasty nevertheless continued to rule Yanqi for a short interception in which a son of the Qu dynasty 麴 of Gaochang 高昌 was ruling.
In the 6th century, Yanqi sent tributes to the Western Türks (Xi Tujue 西突厥) and the Türkic federation of the Tölöš 鐵勒, but also to the Sui dynasty 隋 (581-618).
After the Tang dynasty 唐 (618-907) was founded, Yanqi declared its submission to the Tang and so enjoyed the profits from the merchandise that now again was transported along the old northern route and not via the new northern route of Gaochang. Critically disturbed by this economic loss, Gaochang joined an alliance with the Western Türks and attacked Yanqi. Emperor Taizong 唐太宗 (r. 626-649) therefore sent out an army to destroy the state of Gaochang in 640. In the same year the king of Yanqi married his daughter to a younger son of a high noble of the Western Türks, Qu-li-chuo 屈利啜. Four years later the Protector-general of the Pacified West (Anxi duhu 安西都護), Guo Xiaoke 郭孝恪, conquered Yanqi, captured King Tu-qi-zhi 突騎支, sent him to the Tang capital Chang'an 長安 (modern Xi'an 西安, Shaanxi) and appointed Li-po-zhun 栗婆準 as regent of Yanqi.
As soon as the Tang army withdrew, the Western Türks occupied Yanqi and captured Li-po-zhun. The latter was killed by people from Qiuci, and the Türks enthroned his older brother Xue-po-a-na-zhi 薛婆阿那支 as the new king. In 648 the Tang general Ashina She'er 阿史那社爾 conquered Qiuci and enthroned Xian-na-zhun 先那準 (or Po-ka-li 婆咖利?) as king of Yanqi. Tu-qi-zhi was sent back to Yanqi shortly before his death and was succeeded by King Nen-tu 嫩突. In 658 the Türkish khan A-shi-na He-lu 阿史那賀魯 rebelled against the Tang, but after his defeat, Yanqi became one of the four defense commands (sizhen 四鎮) of the Protectorate of the Pacified West.
Between 674 and 676 Yanqi was itself for a short time an own area command (dudufu 都督府). In 679 Yanqi was deprived the status of a defense command in favour to Suiye 碎葉 that was strategically more advantageous to cut off the communication lines betwen Tubo 吐蕃 (Tibet) and the Western Türks. In 710 the military commissioner (jiedushi 節度使) Tang Jiahui 湯嘉惠 gave Yanqi back the status of a defense command and established military colonies nearby.
In 788 Yanqi was lost again to the armies of Tubo, but soon, the Uyghurs 回鶻 expelled them and controlled the region for more than half a century. The Uyghur khanate disintegrated in 840, and Yanqi (now called Karašahr) was able to fill the power vacuum. Still ruled under the Long dynasty, its population now consisted to a great part of Uyghurs. Yanqi expanded to the east and occupied the former Tang prefecture of Xizhou 西州 (modern Yelufan 葉魯番). The Uyghurs living in that region were therefore called the Uyghurs from Xizhou 西州回鶻. The city of Yanqi at that time was called Solmi. It had lost its status as an independant state and became subject to the various masters of that region, from the Mongols 蒙古 to the Dzungars 準噶爾 and the Manchus 滿洲.
Yanqi was an important centre of Buddhism. Countless monasteries were to be found on its soil, like the famous "Thousand Buddha Grottoes" of Mingwu 明屋千佛洞. In the 20th century, quite a few Buddhist writings were discovered in Yanqi, written in Sanskrit and also in the local language, Tokharian A. A very important example is the Tokharian translation of a Buddhist play by Aryacandra that is in Chinese known as Mile huijian ji 彌勒會見記. These writings influenced not only the genre of bianwen 變文 that flourished during the Tang period, but also the Buddhist Uyghur literature. Yanqi also harboured adherents of Zoroastrianism. Zoroastrian writings in Persian and Uyghur language have been detected in Turfan 吐魯番 that is located not far from ancient Yanqi.
Yanqi was famous for its distinct culture and its own language and script. Males shaved their heads, and women wore trousers. The death were buried after cremation. The inhabitants of Yanqi loved grape wine and were good musicians. Silkworm breeding was known, but not the production of silk textiles. Instead, silk was only used as wads. Yanqi produced a wide range of fieldcrops and a lot of different cattle.