Zhangye's history begins in 111 BC when Emperor Wu of the Han Dynasty established it as a prefecture during his campaigns to control the Hexi Corridor. The city's strategic location made it a vital military garrison and trading post on the Silk Road.
During the Western Jin Dynasty, the Buddhist monk Zhu Tan built the first temple here. The Giant Buddha Temple was constructed in 1098 during the Western Xia Dynasty, marking Zhangye's importance as a Buddhist center. The Mati Temple grottoes were begun even earlier, during the Northern Liang period (397-439 AD).
The Hexi Corridor was under the control of various regimes over the centuries — the Tang Dynasty, the Tibetan Empire, the Tangut Western Xia, the Mongol Yuan Dynasty, and finally the Ming and Qing dynasties. Each left their mark on Zhangye's culture and architecture.
During the Ming Dynasty, Zhangye served as the headquarters of the Gansu military command, controlling the entire Hexi defense line. The city declined somewhat after the Silk Road's importance waned with the rise of maritime trade.
In modern times, Zhangye has developed into an agricultural and tourism hub. The discovery and promotion of the Danxia landform in the early 2000s transformed it into a major tourist destination.