Sofia first became part of the First Bulgarian Empire during the reign of Khan Krum in 809. Afterwards, it was known by the Bulgarian name Sredets and grew into an important fortress and administrative centre.
After a number of unsuccessful sieges, the city fell again to the Byzantine Empire in 1018. In 1128, Sredets suffered a Magyar raid as part of the Byzantine Empire, but in 1191 was once again was incorporated into the restored Bulgarian Empire at the time of Tsar Ivan Asen I after the Vlach-Bulgarian Rebellion.
From the 12th to the 14th century, Sofia was a thriving centre of trade and crafts. It was renamed Sofia (meaning "wisdom" in Greek) in 1376 after the Church of St Sophia. However, it was called both "Sofia" and "Sredets" until the 16th century, when the new name gradually replaced the old one.
During the whole of the Middle Ages, Sofia remained known for its goldsmithing, particularly aided by the wealth of mineral resources in the neighbouring mountains. This is evidenced by the number of gold treasures excavated from the period and even from Antiquity.