During the first one thousand years of Chinese imperial history, Beijing was a provincial city on the northern periphery of China proper. The Qin Dynasty built a highly centralized state and divided the country into 48 commandaries (jun), two of which are located in present-day Beijing. The City of Ji became the seat of Guangyang Commandary (???/???). To the north, in present-day Miyun County, was Yuyang Commandary.
The Han Dynasty, which followed the short-lived Qin in 206 BC, initially restored some local autonomy. Founding Emperor Liu Bang recognized a number of regional kingdoms including Yan. In 106 BC, under Han Emperor Wudi, the country was reorganized into 13 prefectural-provinces (zhou ?), and the City of Ji served as the prefectural capital for Youzhou (??). The tomb of Liu Jian, the Prince of Guangyang who ruled Youzhou from 73 to 45 BC. was discovered in 1974 in Fengtai District and has been preserved in the Dabaotai Western Han Dynasty Mausoleum. In 1999, another royal tomb was found in Laoshan in Shijingshan District but the prince formerly buried there has not been identified.
During the Three Kingdoms, the Kingdom of Wei controlled ten of the Han Dynasty's prefectures including Youzhou and its capital Ji. Ji was demoted to a mere county-seat in the Western Jin Dynasty (?), which made neighboring Zhuo County, in present day Hebei Province, the prefectural capital of Youzhou. After 304 AD, the Western Jin Dynasty was overthrown by steppe peoples who had settled in northern China and established about sixteen short-lived kingdoms. During this period, Beijing was controlled successively by the Di-led Former Qin, the Jie-led Later Zhao, the Xianbei-led Former Yan and Later Yan. The Tanzhe Temple in the Western Hills of Beijing was built in 307 AD. The Northern Wei, another Xianbei regime, eventually united northern China in 386 AD, and restored Ji as the capital of Youzhou. With the creation of a separate prefecture called Jizhou (??) in present-day Tianjin in 370 AD, however, the name Ji was transplanted from Beijing to Tianjin, where a Ji County (??) still exists today. In Beijing, the City of Ji gradually became known as Youzhou. This designation continued through the Eastern Wei, Northern Qi, Sui and Tang Dynasties.
After the Sui dynasty reunited China in 589 AD, the Emperor Yang of Sui built a network of canals from the Central Plain to Youzhou to carry troops and food for the massive military campaigns against Goguryeo (Korea). Though the campaigns proved to be ruinous, they were continued by the Tang Dynasty. In 645 AD, the Tang Emperor Taizong built the Fayuan Temple 3 km southeast of Youzhou to remember the war dead from the Korean Campaigns. The Fayuan Temple, now within Xicheng District, is one of the oldest temples in urban Beijing. The Tang Dynasty reduced the size of a prefecture as an administrative division from a province to a commandary. Youzhou was one of over 300 Tang Prefectures. In 742, Youzhou was briefly renamed Fanyang Commandary (???), but reverted back to Youzhou in 758. To guard against barbarian invasions, the imperial court arranged six frontier military commands in 711 AD, and Youzhou became the headquarter of the Fanyang Jiedushi, who was tasked to monitor the Khitan and Xi nomads just north of present-day Hebei Province. In 755, the local commander An Lushan, launched a rebellion from Youzhou after losing a power struggle in the imperial court. He declared himself the emperor of the Great Yan Dynasty and went on to conquer Luoyang and Xi'an. The An Shi Rebellion lasted ten years and severely weakened the Tang dynasty. It also paved the way for Khitan expansion into northern China, which prompted the rise of Beijing in Chinese history.
After the demise of the Tang Dynasty fell in 907, China was divided into ten kingdoms, mostly in the south, and five short-lived dynasties in the north. One of these dynasties, Later Jin Dynasty (936-947), a weak regime led by Shatuo Turk Shi Jingtang, ceded a large part of the northern frontier across present-day Hebei and Shanxi Provinces, including Youzhou (modern Beijing) to the Khitan in exchange for military support.