Sufi literature—comprised of sayings of local figures and teachers, which were then collected by their students—originated in the 9th century C.E. Eventually, as the practices and dogma of Sufism developed to incorporate methods of training and disciplining the soul, thematic works gave way to more elaborate treatises on the "Science of the Hidden" or "'ilm al-batin." An early hub of mysticism developed in Baghdad, the capital of the Islamic empire under the Abbasid dynasty from the 8th century onward. Antecedents of a famous teacher named Al-Junayd ibn Muhammad (d. 910 C.E.) included such luminaries as Al-Hasan al-Basri, a major figure who is often cited as an intellectual forefather by later practitioners of Sufism.
With the consolidation of orthodox schools of law and what is now known as the shariah in the 9th century C.E. came a flourishing of literary and philosophical scholarship in the Islamic world. "Religious Sciences" is a term used for everything from Quranic exegesis to Sufi interpretations of scripture. According to medieval Sufi texts, knowledge of how to relate to the world in a way that acknowledged and fostered love for God was the "Science of the Hidden" or "'ilm al-batin." The language of the "hidden versus the visible" or the "batin versus the zahir" is still in use by Sufi practitioners today. This also referred to knowledge of the self, a term which had both negative and positive implications. One's lower self, or base self, was concerned with earthly necessities and desires. To transcend this lower self successfully was to achieve union with God, the ultimate goal for which Sufis strived.
In contrast to other types of scholarly endeavor, the knowledge resulting from Sufi exploration of knowledge of the inner, hidden, or esoteric (all elements of al-batin) was deemed by its practitioners to be a more inwardly-looking mode of worship. Sufism did not attempt to divorce itself from mainstream Islam, but it did supplement it with this esoteric perspective. It is difficult, partly for this reason, to discern the exact beginnings of Sufism chronologically. There was no foundational moment, event, or single person. Sufis have long claimed that their original model was Muhammad himself, a claim rejected by their mainstream opponents. Early Sufis who flourished in the later 7th and early 8th centuries C.E., such as Al-Hasan al-Basri, are not uniformly recognized as having been Sufis. Still, by the 9th century C.E., the label "Sufi" became accepted in Islamic society, after which point the movement flourished enormously.