Of three buildings at this Orthodox patriarchal basilica site (first dedicated in 360 by Emperor Constantius II), two burned during riots of 404 and 532. The current structure was ordered built by Justinian I, larger and more majestic than its predecessors. Materials came from around the empire: Hellenistic columns from the Temple of Artemis at Ephesus, Egyptian stone, green marble from Thessaly, Bosporus black stone, and Syrian yellow stone. Over 10,000 men labored here. After the inauguration, on December 27, 537, it was the largest cathedral for 1,000 years. Justinian proclaimed "Solomon, I have outdone thee!" Mosaics were completed under Justin II (565–578). Seat of the Orthodox patriarch of Constantinople, it was a sanctuary for persecuted outlaws, and the principal Byzantine imperial ceremonial site.
Frequent earthquakes caused partial collapses in 558, 869, and 989. Each time the building was restored. Pictures and statues were removed during the Byzantine Iconoclasm. Between 726 and 842, under emperors Leo the Isaurian and Theophilus, the veneration of imagery was forbidden, as it would be during centuries under Islam.
The church was ransacked and desecrated by Fourth Crusade Latin Christians. Relics sent to churches in the West may now be seen in various museums. During the 1204-1261 occupation the church was a Roman Catholic cathedral. In 1261 the Byzantines returned to find it dilapidated. Emperor Andronicus II had new buttresses built in 1317, but cracks appeared after an 1344 earthquake, and parts of the building collapsed in 1346, closing it until 1354.
In 1453 Ottoman troops of Mehmed II made the Hagia Sophia a focal point of their pillage, desecrating and looting it. Byzantines taking sanctuary were slaughtered or enslaved. Converted to the Aya Sofya Mosque, it was to be the principal mosque of Istanbul for almost 500 years; a model for others such as the Sultan Ahmed Mosque, and the Şehzade Mosque.
Another earthquake in 1509 collapsed a minaret, restored under Selim II (1566–1574) by the great architect Mimar Sinan, one of the first earthquake engineers. He added four great minarets seen today, along with Selim's mausoleum. Sultan Ahmed III (1703–1730), had plaster renovated, helping to preserve mosaics that would otherwise have been sold as talismans to visitors. Restorations by Mahmud I in 1739 added a medrese (now the museum library), a soup kitchen for the poor, a library, and an ablutions fountain.
The most famous Aya Sofya restoration, by Sultan Abdülmecid, was completed in 1849 by eight hundred workers under the Swiss-Italian architect brothers, Gaspare and Giuseppe Fossati. Mosaics were also cleaned, old chandeliers replaced, and gigantic medallions hung, inscribed by calligrapher Kazasker Mustafa İzzed Effendi with the names of Allah, the Prophet Muhammad, caliphs Abu Bakr, Umar, Uthman and Ali, and Mohammed's grandsons, Hassan and Hussain. With ceremonial pomp the mosque re-opened in July, 1849.
In 1935 the building became a museum with the establishment of the Republic of Turkey under President Mustafa Kemal Atatürk. With carpets removed marble floor decorations such as the Omphalion appeared for the first time in centuries, and plaster-covered mosaics were revealed.
For Tuesday-Sunday tours see the website: