Shingon Buddhist doctrine and teachings arose during the Heian period (794-1185) after a Buddhist monk named Kūkai traveled to China in 804 to study Esoteric Buddhist practices in the city of Xi'an (西安), then called Chang-an, at Azure Dragon Temple (青龍寺) under Huiguo, a favorite student of the legendary Amoghavajra. Kūkai returned to Japan as Huiguo's lineage- and Dharma-successor. Shingon followers usually refer to Kūkai as Kōbō-Daishi (弘法大師 Great Master of the Propagation of Dharma) or Odaishi-sama (お大師様 The Great Master), the posthumous name given to him years after his death by Emperor Daigo.
Before he went to China, Kūkai had been an independent monk in Japan for over a decade. He was extremely well versed in Chinese literature, calligraphy and Buddhist texts. Esoteric Buddhism was not considered to be a different sect or school yet at that time. Huiguo was the first person to gather the still scattered elements of Indian and Chinese Esoteric Buddhism into a cohesive system. A Japanese monk named Gonsō (勤操) had brought back to Japan from China an esoteric mantra of the bodhisattva Ākāśagarbha, the Kokūzō-gumonjihō (虚空蔵求聞持法 "Ākāśagarbha Memory-Retention Practice") that had been translated from Sanskrit into Chinese by Śubhakarasiṃha (善無畏三蔵 Zenmui-Sanzō). When Kūkai was 22, he learned this mantra from Gonsō and regularly would go into the forests of Shikoku to practice it for long periods of time. He persevered in this mantra practice for seven years and mastered it. According to tradition, this practice brought him siddhis of superhuman memory retention and learning ability. Kūkai would later praise the power and efficacy of Kokuzō-Gumonjiho practice, crediting it with enabling him to remember all of Huiguo's teachings in only three months. Kūkai's respect for Ākāśagarbha was so great that he regarded him as his honzon (本尊) for the rest of his life.
It was also during this period of intense mantra practice that Kūkai dreamt of a man telling him to seek out the Mahavairocana Tantra for the doctrine that he sought. The Mahavairocana Tantra had only recently been made available in Japan. He was able to obtain a copy in Chinese but large portions were in Sanskrit in the Siddhaṃ script, which he did not know, and even the Chinese portions were too arcane for him to understand. He believed that this teaching was a door to the truth he sought, but he was unable to fully comprehend it and no one in Japan could help him. Thus, Kūkai resolved to travel to China to spend the time necessary to fully understand the Mahavairocana Tantra.
The main building of Shinsenen
, a Shingon temple in Kyoto
founded by Kūkai in 824
When Kūkai reached China and first met Huiguo on the fifth month of 805, Huiguo was age sixty and on the verge of death from a long spate of illness. Huiguo exclaimed to Kūkai in Chinese (in paraphrase), "At last, you have come! I have been waiting for you! Quickly, prepare yourself for initiation into the mandalas!" Huiguo had foreseen that Esoteric Buddhism would not survive in India and China in the near future and that it was Kukai's destiny to see it continue in Japan. In the short space of three months, Huiguo initiated and taught Kūkai everything he knew on the doctrines and practices of the Mandala of the Two Realms as well as mastery of Sanskrit and (presumably to be able to communicate with Master Huiguo) Chinese. Huiguo declared Kūkai to be his final disciple and proclaimed him a Dharma successor, giving the lineage name Henjō-Kongō (traditional Chinese: 遍照金剛; ; pinyin: Biànzhào Jīngāng) "All-Illuminating Vajra".
In the twelfth month of the same year, Huiguo died and was buried next to his master, Amoghavajra. More than one thousand of his disciples gathered for his funeral. The honor of writing his funerary inscription on their behalf was given to Kūkai.
Kukai returned to Japan after Huiguo's death. If he had not, Esoteric Buddhism might not have survived; 35 years after Huiguo's death in the year 840, Emperor Wuzong of Tang assumed the throne. An avid Daoist, Wuzong despised Buddhism and considered the sangha useless tax-evaders. In 845, he ordered the destruction of 4600 vihara and 40,000 temples. Around 250,000 Buddhist monks and nuns had to give up their monastic lives. Wuzong stated that Buddhism was an alien religion and promoted Daoism zealously as the ethnic religion of the Han Chinese. Although Wuzong was soon assassinated by his own inner circle, the damage had been done. Chinese Buddhism, especially Esoteric practices, never fully recovered from the persecution, and esoteric elements were infused into other Buddhist sects and traditions.
After returning to Japan, Kūkai collated and systematized all that he had learned from Huiguo into a cohesive doctrine of pure esoteric Buddhism that would become the basis for his school. Kūkai did not establish his teachings as a separate school; it was Emperor Junna, who favored Kūkai and Esoteric Buddhism, who coined the term Shingon-Shū (真言宗 Mantra School) in an imperial decree which officially declared Tō-ji (東寺) in Kyoto an Esoteric temple that would perform official rites for the state. Kūkai actively took on disciples and offered transmission until his death in 835 at the age of 61.
Kūkai's first established monastery was in Mount Kōya (高野山), which has since become the base and a place of spiritual retreat for Shingon practitioners. Shingon enjoyed immense popularity during the Heian period (平安時代), particularly among the nobility, and contributed greatly to the art and literature of the time, influencing other communities such as the Tendai (天台宗) on Mount Hiei (比叡山).
Shingon's emphasis on ritual found support in the Kyoto nobility, particularly the Fujiwara clan (藤原氏). This favor allotted Shingon several politically powerful temples in the capital, where rituals for the Imperial Family and nation were regularly performed. Many of these temples – Tō-ji and Daigo-ji (醍醐寺) in the south of Kyōto and Jingo-ji (神護寺) and Ninna-ji (仁和寺) in the northwest – became ritual centers establishing their own particular ritual lineages.