As the Pali commentaries tell it
The commentaries on the Sutta Nipata and the Samyutta Nikaya · Alavi · 3 min read
The king of Alavi went hunting every seventh day, to keep his soldiers sharp. The rule of the hunt was strict: if the deer broke out at a man's post, that man had to catch it. One day the deer broke out at the king's own post.
He chased it alone for three leagues, killed it, slung it over his shoulder and turned for home. On the way he stopped to rest under a great banyan tree. It was the home of the yaksha Alavaka, and the king of the yakshas had given Alavaka a gift: anyone who walked into the shade of that tree was his to eat.
A huge hand closed on the king.
"Let me go," said the king, "and I will send you a man to eat, with a bowl of rice, at the set time."
The yaksha agreed. The king went home and told his ministers, and at first they sent prisoners from the jail. When the jails were empty, each house in the town had to give a child. Women expecting babies left Alavi for other towns. Twelve years went by in this way, until there was only one child left in the city: the king's own son. The king had him bathed and dressed in his finest clothes, to be taken to the tree at dawn.
That night, the Buddha saw it all. He walked thirty leagues in a day and came to the yaksha's house by the banyan. Alavaka was away, at a gathering of yakshas in the Himalaya. His doorkeeper, Gadrabha, looked at the visitor.
"My master is rough," he warned. "He does not care who you are."
"Let me in all the same," said the Buddha. Gadrabha let him in, and he sat down on the yaksha's own seat and taught the women of the house.
Two yakshas, Satagira and Hemavata, were flying to the same gathering over Alavi that night, and found they could not fly over him. They came down, bowed, and went on to the mountains with the news. When Alavaka heard it, he raced home in a fury.

He threw every terror he had at the Buddha, and none of it touched him. He threw his last weapon, a cloth that could stop the rain for twelve years, and it did nothing to him at all.
"Get out, ascetic!" he shouted.
The Buddha rose and went out.
"Come in!"
The Buddha came in. Three times the yaksha ordered him out and in again, and three times the Buddha did as he was told, to cool his temper. The fourth time he stayed where he was.
"Then answer my questions," said Alavaka, "or I will drive you out of your mind."
His questions were old ones. His parents had learned them, with their answers, from Kassapa, a Buddha of long ago, and Alavaka meant to wear his guest out with them. He asked what wealth was best, and what taste was sweetest, and how a man crosses the flood. The Buddha answered every one. By the end Alavaka was shouting again, but now for joy, and he had entered the stream that leads to freedom.
At dawn the king's men came up the road with the boy, and stopped in wonder at the sound of the yaksha's happy cries.
"Here is the child," they said. "He is for you."
Alavaka took the boy in his great hands. Then, ashamed before his guest, he held him out to the Buddha. The Buddha blessed him and gave him back to the king's men, and the boy went home. He grew up to be known as Hatthaka of Alavi.
The king and his people built the yaksha a new home, and brought him flowers and perfumes instead.
As the Sutta Nipata tells it
Sutta Nipata 1.10 and Samyutta Nikaya 10.12 · the oldest texts · 2 min read
In the oldest Buddhist books there is no king, no hunt and no child. There is only a spirit, his house near Alavi, and a guest he does not want.
The Buddha was staying in the haunt of the yaksha Alavaka. The yaksha came to him and told him to get out, and he went out. He told him to come in, and he came in. After the fourth "Get out!" the Buddha said no. "I will not go. Do what you have to do."
"Then I will ask you a question," said the yaksha. "If you cannot answer, I will drive you mad, or burst your heart, or catch you by the feet and fling you over the Ganga."
"There is no one in the world, among gods or men, who could do that to me," the Buddha said. "But ask what you like."
So the yaksha asked in verse, and the Buddha answered in verse. A man's best wealth is faith. Truth is the sweetest of all tastes. The life lived with understanding is the best life. The flood is crossed by faith and the sea by care; sorrow is passed by effort, and a person is made pure by wisdom. Wealth comes from hard and fitting work, good name from truthfulness, and friends are kept by giving. A householder who has truth, right conduct, steadiness and generosity does not grieve when he leaves this world.
"Ask the other teachers too," the Buddha added, "and see if they have anything better."
"Why should I ask anyone else?" said Alavaka. "Today I know what is good for the life to come. It was for my good that you came to Alavi." And he said he would go from village to village and town to town, praising the Buddha and his teaching.

The verses became one of the parittas, the protective chants that Buddhist monks still recite.


