The short answer: Samguk Yusa Book 1, the Dohwanyeo and Bihyeongnang entry
Search the written record for dokkaebi and the first thing you find is the entry on Dohwanyeo and Bihyeongnang in Book 1 of the Samguk Yusa (三國遺事). The Encyclopedia of Korean Folk Culture, published by the National Folk Museum of Korea, calls this the first dokkaebi origin tale (bonpuri) set down in writing. The Encyclopedia of Korean Culture, published by the Academy of Korean Studies, says much the same, introducing the Bihyeong legend (鼻荊說話) of the Samguk Yusa as the first dokkaebi story on record. The tale concerns King Jinji, the 25th ruler of Silla, and survives in Book 1 of the Samguk Yusa. Every evening Bihyeong gathered dokkaebi around him to play, and in the space of a single night he built a bridge.
A custom outlived the story and has been handed down with it. People wrote out a text about Bihyeong, pasted it up, and so drove off malign spirits. The National Folk Museum's entry on Dohwanyeo and Bihyeongnang describes the Gyeongju custom this way: "In Gyeongju it is still the custom to drive off spirits by pasting on the door a verse that reads, 'the house of Bihyeongnang, the son borne by the soul of the holy emperor (聖帝),' and this is said to be the beginning of the 'Donggyeong Duduri.'" The bridge-building and the text pasted on the door survive side by side in a single entry.
Taken together, the two encyclopedias give two reasons for treating this record as an early witness to dokkaebi belief. First, it describes the superhuman feat of Bihyeong commanding dokkaebi to build a bridge. Second, it describes the religious act of writing Bihyeong's name and pasting it on the door. The Encyclopedia of Korean Culture surmises from the various written records that dokkaebi belief already existed in the Three Kingdoms period. Arguably the telling point is that story and belief survive here side by side, in one and the same place.
The reason for inclusion set out in the preface to the Giyi chapter
The preface to the Giyi chapter of the Samguk Yusa first draws a line. It opens: "Now, when the sages of old (聖人) were raising up their states through rites and music (禮樂) and dispensing their teachings through benevolence and righteousness (仁義), they did not speak of the strange, the forceful, the disorderly or the spiritual (怪力亂神)." The preface then turns its argument around, listing the birth legends of ancient China as precedents: "Jiandi (簡狄) swallowed an egg and gave birth to Xie (契); Jiang Yuan (姜嫄) trod in a footprint and gave birth to Qi (弃)."
The preface then asks: "How, then, can it be called strange that the founders of the Three Kingdoms all came forth from the miraculous?" It also states its editorial policy in a single sentence: "That such marvels and miracles are placed at the head of every chapter (篇) — this is precisely the reason why." These lines can be read directly in the translated Samguk Yusa Book 1 on Wikisource. There is no confirmation, however, that the preface points specifically to the Dohwanyeo and Bihyeongnang entry.
Dohwanyeo, King Jinji, the seven-day stay, and the birth of Bihyeong
The first half of the tale begins with the king and a woman. The Encyclopedia of Korean Culture summarizes the passage as follows. Before he was driven from the throne, King Jinji, the 25th ruler of Silla, tried to force himself on Dohwarang, a married woman. The king could not break her fidelity, however, and sent her home. Dohwarang told him she would grant his request if she had no husband.
Some ten days later the king, now dead, appeared. He entered Dohwarang's room and held her to what she had said. He stayed seven days and then left. When her term was full, Dohwarang gave birth to a boy. The child's name was Bihyeong.
King Jinpyeong heard of this marvel and had Bihyeong brought into the palace to be raised there. At the age of 15 Bihyeong received the post of jipsa. After that he would go out beyond the palace walls every evening, gather the dokkaebi together, and play with them. That is as far as the record goes on Bihyeong's birth and office.
The Gwigyo built in a single night and Gyeongju's worship of Duduri
King Jinpyeong confirmed what was happening and put the dokkaebi to work. He had them build the Gwigyo (鬼橋) in the space of a single night. He also brought in a dokkaebi named Gildal (吉達) to help with affairs of state. The Encyclopedia of Korean Culture records that Bihyeong, as chief of the dokkaebi, built a large bridge over the ditch north of Sinwonsa Temple (神元寺). The National Folk Museum's entry likewise says that Bihyeong commanded his band to build a bridge overnight and named it Gwigyo.
The National Folk Museum's entry also explains what kind of act this was. It reads Bihyeong's commanding the Duduri dokkaebi to build bridges and raise fortress gates as the function of a building deity. And it notes that the act matches what dokkaebi characteristically do in oral tradition.
Traces of the story's afterlife remain in records from Gyeongju. The dokkaebi came to flee in fear at the mere mention of Bihyeong's name. The National Folk Museum's entry quotes the record in the Gyeongju section of the Dongguk Yeoji Seungnam (東國輿地勝覽), which states that after Bihyeong the people of Silla's Gyeongju worshipped Duduri (豆豆里) with great fervor. The same entry identifies that Bihyeongnang as a dokkaebi. The Goryeosa (高麗史), in its entry on Yi Ui-min, records that the people of Gyeongju had a tree spirit (木魅) they called Dudueul (豆豆乙), and that Yi Ui-min built a shrine and enshrined it in his house.
Korean and Chinese records side by side
Korean sources record the dokkaebi's names and nature along several lines. The Encyclopedia of Korean Culture lists such names as dochaebi, dokgakgwi (獨脚鬼), heoju (虛主), mangnyang (魍魎) and, on Jeju Island, yeonggam. The same encyclopedia distinguishes dokkaebi from ghosts, which are souls transformed after a person's death. Dokkaebi come instead from natural objects such as trees and stones, and they commonly appear in the hills and fields. Jang Gye-i's (張繼弛) Haedong Jamnok (海東雜錄) describes the dokkaebi as the shadowy, yin energy (陰靈) of mountain and sea, formed from the transformed essence of grass, trees, earth and stone. On Jeju Island dokkaebi were venerated as household guardians, as tutelary spirits of fishing boats, as gods of the smithy and as village deities.
Chinese sources carry related records. The Baopuzi (抱朴子) records that the dokkaebi has only one leg. Citing this, the Encyclopedia of Korean Culture takes the one-legged motif to have been widespread in East Asia since antiquity. The same encyclopedia notes that the necrophilia legend type also appears in the Chinese Soushenji (搜神記) and Fayuan Zhulin (法苑珠林), which is why its connection to Dohwanyeo and Bihyeongnang has drawn attention.
Laying the two bodies of record out item by item brings the differences into view. In the names, the Korean sources give Bihyeong, Duduri and Dudueul. In the texts, the Korean side consists of Samguk Yusa Book 1, the Gyeongju section of the Dongguk Yeoji Seungnam, the Yi Ui-min entry in the Goryeosa, and the Haedong Jamnok; the Chinese side consists of the Baopuzi, the Soushenji and the Fayuan Zhulin. In content, the Korean sources record the building of a bridge, the text pasted on the door, and the enshrining of a tree spirit in the home. The Baopuzi records the single-leg trait, while the Soushenji and the Fayuan Zhulin record the necrophilia legend.
Wrapping up
The earliest written record of a dokkaebi story is the entry on Dohwanyeo and Bihyeongnang in Book 1 of the Samguk Yusa. The National Folk Museum of Korea calls it the first dokkaebi origin tale set down in writing. The Encyclopedia of Korean Culture calls the Bihyeong legend the first dokkaebi story on record. The same encyclopedia surmises from the various written records that dokkaebi belief already existed in the Three Kingdoms period. The accounts in the two reference works can be seen as showing how this entry is assessed.
As a tale type, the classification runs several layers deep. The Encyclopedia of Korean Culture places the story within the category of legends of union with a strange being (異物交媾說話), specifically marriage with the dead (死者交婚). It also notes that the second of the categories of necrophilia legend (屍愛說話) proposed by Jang Deok-sun (張德順) designates this group of tales. The National Folk Museum states that the legend is interrelated with the night-visitor (夜來子) legend, the legend of marriage with a strange being, and the necrophilia legend. The original text can be consulted in the translated Samguk Yusa Book 1 on Wikisource and in the entries of the two encyclopedias.