King Solomon wanted to build the Temple from unhewn stone. The Torah forbade iron tools on the altar, and Solomon, meticulous as always, extended the prohibition to the whole sanctuary. But stone will not split without a tool.
The only solution was the Shamir — a tiny worm that could cut stone by gaze alone. The Shamir was said to be in the possession of Ashmedai, the king of the demons.
Solomon sent his general Benayahu ben Yehoyada with a chain engraved with the Shem ha-Meforash — the Ineffable Name — and with Solomon's signet ring, along with wool and skins full of wine. Benayahu located Ashmedai's daily drinking well. He dug a lower well, drained the original, and refilled it with wine. Ashmedai, thirsty, drank without noticing the switch. When he passed out, Benayahu bound him with the Name-engraved chain.
On the road back, Ashmedai behaved strangely. He tore up trees. He bent sideways to avoid a widow's hut. He helped a blind man and redirected a lost child. He wept at a wedding feast. He laughed at a man ordering boots meant to last seven years. He laughed at a fortune-teller performing tricks.
Brought before Solomon, Ashmedai waited three days to be received. Then he hurled a four-cubit cane at Solomon's feet. "When you die," he said, "that is the whole space you will occupy. Yet now the whole world cannot satisfy you."
Solomon asked how to obtain the Shamir. Ashmedai explained it had been entrusted to a certain wild bird. Solomon's men found the nest, covered it with a glass bell, and when the bird returned she dropped the Shamir onto the glass to split it open. Startled by a sudden noise, she flew off without it.
Later Ashmedai explained his behavior on the road. The blind man he helped was a pious man whose reward needed to be delivered in this world. The child would grow into a sinner whose credit needed to be paid out while he was still innocent. He wept at the wedding because the bridegroom would die within three days, and the bride would wait thirteen years for yibum — for her young brother-in-law to come of age. He laughed at the man ordering seven-year boots because the man would not live seven days. He laughed at the fortune-teller because the fortune-teller was sitting on a buried treasure and did not know it.
While the Temple was being built, Solomon grew curious. He removed Ashmedai's chain and handed him the signet ring, just to see some marvelous thing. Ashmedai swallowed the ring, unfurled wings that touched earth and heaven, and flung Solomon four hundred miles away. The king wandered, begging for bread, saying: I, Kohelet, was king in Jerusalem (Ecclesiastes 1:12).
Meanwhile Ashmedai, taking Solomon's shape, sat on the throne. The sages grew suspicious. They asked the queens to examine his feet. He always came to them in shoes — and demons have chicken-feet that cannot be disguised. When the real Solomon finally returned to Jerusalem and was given back the signet ring and the chain, Ashmedai vanished.
Gittin 68 and Gaster's Exempla #114 preserve this long adventure. The wisest king in the world built the Temple only by borrowing tools from the king of the demons, and learned along the way that his own throne could be stolen the moment he forgot who he was.