Hassān'l-Aʿjam Afzāl al-dīn Badil ibn Ali Khāqānī Haqaiʿqi Shirvani was born in 520 AH in Tabriz. His father was a carpenter and Khaqani's mother was originally a slave-girl of Nestorian Christian faith who has been taken from Rome to Shirvan and had converted to Islam. Khāqānī was a Sunni Muslim.
In the beginning, his title was Haqaiʿqi, and after serving to Khāghān Akbar Manuchehr Shirvanshah, he adopted the pen-name of Khaqani ("regal"). His other title was Hassān'l-Aʿjam ("The Persian Hassān") which according to his Mathnawi, it was his uncle who has given him that title. Khaqani has been to Shirvanshah, Khwarazmian, and Seljuq’s court and also the cities in Iraq and Khorasan, and wrote the eulogy lyrics for more than 20 courtiers. He traveled to Mecca two times and in addition to Tuḥfat al-ʿIrāqayn (“Gift of the Two Iraqs”), he wrote some odes about these journeys. Visiting Al-Mada'in, he wrote the well-known Qasidas of Iwan of Mada'in. Khaqani can be recognized from the verses of Tuḥfat al-ʿIrāqayn. He wrote that Mathnawi in 40 days and it is the first Persian travel literature in the form of Mathnawi. Although Khaqani is famous for his eulogy lyrics, he has a free will, and he was a high-minded and honorable man. Being related to the reputable courts, he avoided the jobs which prevented him from the spiritual world. Being one of the unique poets in Persian literature, he is incomparable in respecting form in poetry. Regarding religious beliefs, he is not like other poets such as Sanai, Attar of Nishapur, or any other one who saw goodness all over the world. Contrary to these poets, Khaqani believed that the universe is constructed by evil and decadence. He has lots of complaints about people, and he expressed his dissatisfaction about the unfaithfulness of the world and human beings. Because of the chaos after the death of Shirvanshah in 554 AH, he was imprisoned and he complained about this event in his poems. All his efforts and ups and downs in his life are eloquently represented in Qasidas he had sent to different rulers in order to make some money at various times. One of the characteristics of his diwan is the abundance of references and documents about his own biography.
Khaqani passed away in 595 AH.