Wei Anshi was born in 651, early in the reign of . His family was from the Tang Dynasty capital Chang'an, and his great-grandfather Wei Xiaokuan was a renowned general for Western Wei and Northern Zhou. His grandfather Wei Jin was an official for Northern Zhou's successor and Tang's predecessor Sui Dynasty and, after Sui's destruction in 619, served one of the competing claimants for the throne, Wang Shichong the Emperor of Zheng, before eventually surrendering to and serving Tang's founder . Wei Anshi's father Wei Wan and uncles Wei Kun and Wei Sui also served as officials during Tang.
It is not known when it occurred, but at some point Wei Anshi passed the imperial examination and served as the sheriff of Qianfeng County . The high level official Su Liangsi favored his talent. In 689, by which time Emperor Gaozong's son was emperor , Wei was serving as military recruiter at the capital prefecture Yong Prefecture and Su was serving as . Su told him, "You have great talent and should be used for great things. For you to work in local government is a waste." Su then recommended him to Empress Dowager Wu, and Empress Dowager Wu made him a reserve official at the ministry of rites and magistrate of Yongchang County , as well as military advisor to the prefect of Bing Prefecture . It was said that Empress Dowager Wu personally issued an edict, "I heard that when you are at your post, you are deliberate in your handling of important matters. I also heard that your good governance was greater than even capable officials, and that your kindness and understanding were renowned among the officials. Your capability greatly comforts me." He was soon made the prefect of Bing Prefecture, and he later served as prefect of De Prefecture and then Zheng Prefecture . He was known for his seriousness, and he rarely laughed. He was also said to be honest and strict, and wherever he was, the people and the subordinates respected and feared him.
During Wu Zetian's reign
In 700, by which time Wu Zetian was carrying the title of "emperor" , Wei Anshi was recalled to then-capital Luoyang to serve as ''Wenchang You Cheng'' , one of the secretaries general at the executive bureau of government , but was soon made ''Luantai Shilang'' , the deputy head of the examination bureau and given the designation ''Tong Fengge Luantai Pingzhangshi'' , making him a chancellor ''de facto''. At that time, Wu Zetian's nephew Wu Sansi and lovers Zhang Yizhi and Zhang Changzong were powerful, and Wei Anshi several times publicly tried to rebuke them. There was an occasion when, at an imperial feast that Wei was attending, Zhang Yizhi brought in several merchants , led by one Song Bazi , and began gambling with them. Wei kneeled down to Wu Zetian and stated, "Merchants are low in status and should not be allowed to attend imperial gatherings." He then had the guards expel the merchants, and this shocked the people who attended, who were fearful for Wei. However, Wu Zetian, because Wei was being honest and forthright, praised him.
In 703, while Wu Zetian was at Chang'an, she made Wei the official in charge of Luoyang. In 704, she made Wei acting ''Nayan'' , the head of the examination bureau and a post considered one for a chancellor. In the fall of that year, he filed an indictment against Zhang Yizhi, and Wu Zetian ordered that he and another chancellor, Tang Xiujing, investigate the case, but before their investigation was complete, Wu Zetian was overthrown in a coup in 705, and Zhang Yizhi and Zhang Changzong were killed in the coup, mooting the investigation.
During Emperor Zhongzong's second reign
After Wu Zetian was overthrown, her son the Crown Prince, also a former emperor, was restored to the throne . Soon after Emperor Zhongzong's restoration, Wei Anshi was made the minister of justice , but was no longer a chancellor. Two months later, however, as part of a government reorganization, he was made the minister of civil service affairs and given the designation ''Tong Zhongshu Menxia Sanpin'' , again making him chancellor ''de facto''. A month later, he was made ''Zhongshu Ling'' , the head of the legislative bureau and a post also considered one for a chancellor. He was also created the Duke of Xun and made the secretary general of Emperor Zhongzong's brother Li Dan the Prince of Xiang.
In spring 706, Wei was made the minister of the treasury , no longer a chancellor. In 709, he was made ''Shizhong'' (侍中, the new title for the head of the examination bureau and was therefore chancellor again.
During Emperor Shang's reign and Emperor Ruizong's second reign
In 710, while Wei Anshi served as chancellor, Emperor Zhongzong suddenly died -- a death that traditional historians believed to be a poisoning carried out by Emperor Zhongzong's powerful wife and daughter Li Guo'er the Princess Anle, so that Empress Wei could eventually be emperor like Wu Zetian and Li Guo'er could become crown princess. For the time being, however, Emperor Zhongzong's son by a concubine, the Prince of Wen, was made emperor , with Empress Wei serving as empress dowager and regent. Less than a month later, a coup led by Emperor Zhongzong's sister Princess Taiping and nephew the Prince of Linzi killed Empress Wei and Li Guo'er. In short order, Emperor Shang was deposed, and Li Dan returned to the throne. Shortly after Emperor Ruizong's return to the throne, Wei Anshi was removed from his post as ''Shizhong'' and made an advisor to Li Longji, now crown prince, as well as ''Zuo Pushe'' , one of the heads of the executive bureau .
As of 711, discord began to develop between Li Longji and Princess Taiping, as they vied in their struggle to influence Emperor Ruizong. Traditional historians believed that Princess Taiping was beginning to gather a faction of officials around her, intending to find fault with Li Longji as to remove him from being crown prince. She tried to include Wei in her faction, repeatedly having her son-in-law Tang Jun invite him to her mansion, but he repeatedly declined. On one occasion, when Emperor Ruizong met with Wei alone, he told Wei, "I have heard that the officials are all loyal only to the Eastern Palace . You should investigate this." Wei responded:
Emperor Ruizong was surprised and took no further action. However, Princess Taiping was behind a curtain and heard this, and she spread rumors against Wei, hoping that he would be arrested and tried, but the chancellor Guo Yuanzhen stopped the investigations into Wei. Later that year, after the chancellors Yao Chong and Song Jing were removed for having proposed that Princess Taiping be settled outside the capital and that two princes with potential claims to be crown prince -- Li Chengqi the Prince of Song and Li Shouli the Prince of Bin be sent out of the capital to serve as prefectural prefects, Wei again became ''Shizhong'' and was said to be largely in charge of civil service affairs, along with Li Rizhi. It was said that under Wei's and Li Rizhi's oversight, the civil service affairs, which had fallen into a state of confusion during Emperor Zhongzong's reign and which Song and Yao had tried to streamline, again became unwieldy. Soon thereafter, Wei was again made ''Zhongshu Ling''. Four months later, he was made advisor to Li Longji and ''Zuo Pushe'' , but retained chancellor status with the designation ''Tong Zhongshu Menxia Sanpin''. It was said that, however, his actual powers were limited, as Princess Taiping, who was very influential on Emperor Ruizong's decisions at the time, seeing that Wei would not join her faction, decided to pile honors and titles on him but strip him of actual authority. Three months later, as part of a major governmental reorganization, Wei, along with Guo, Dou Huaizhen, Li Rizhi, and Zhang Shuo, were removed from their chancellor posts, with Wei acting only as ''Zuo Pushe'' and also put in charge of Luoyang .
During Emperor Xuanzong's reign
Sometime after Wei was removed from his chancellor post, his daughter, who had married the junior official Li Yuancheng , died. Wei's wife Lady Xue suspected a servant girl of Li Yuancheng's, with whom Li Yuancheng had a sexual relationship but who had since married another person, had murdered her daughter, and therefore had people seize the servant girl and pound her to death. The imperial censor Yang Maoqian submitted an indictment against Wei, and he was demoted to be the prefect of Pu Prefecture , and then moved to Qing Prefecture . While Wei was at Pu Prefecture, Jiang Jiao , an official who was particularly close to Li Longji, who had become emperor in 712 when Emperor Ruizong decided to retire, made requests to Wei, which Wei rejected, drawing Jiang's ire.
In 714, Jiang Jiao's brother Jiang Hui , then imperial censor, pointing out that at the time that Wei Wen and Zong Chuke altered Emperor Zhongzong's will to remove Emperor Ruizong as coregent, Wei Anshi, Wei Sili, Zhao Yanzhao, and Li Jiao were all chancellors and did nothing to stop them, had his subordinate Guo Zhen file an indictment against the former chancellors. These former chancellors were all demoted, with Wei Anshi reduced to being the secretary general of Mian Prefecture . Once Wei Anshi arrived at Mian Prefecture, Jiang Hui then filed another indictment, accusing Wei Anshi of having embezzled governmental property while being in charge of building Emperor Zhongzong's tomb, and he sent investigators to Mian Prefecture to investigate. Wei Anshi sighed and said, "They just want me to die." He soon died in anger and fear. Later in the reign of Emperor Xuanzong, when his son Wei Zhi had become a powerful official, he was posthumously conferred a number of honors, including the restoration of the title of Duke of Xun.