During the first war, the Vanir had reduced the walls of Ásgarð to rubble.
After the gods had established the nine worlds, a master builder came to them in Ásgarð. He offered to build a fortification around Ásgarð that would be secure against the giants, even if they should come from the direction of Miðgarð. The builder promised to complete the job in three seasons, and as payment, he asked for Freyja to be his bride, and to receive the sun and the moon.
The gods assembled in council and came up with a counteroffer. They proposed that the builder would receive his payment only if he completed the job in one season and did so without assistance from any man. The builder's payment would be forfeit if the job were not completed on the first day of summer.
The builder agreed to the offer if he could use his stallion, Svaðilfari, to help him on the job. Loki convinced the gods to accept this offer to make their world safe from attacks by giants. Oaths were made and witnesses invoked to bind the agreement. On the first day of winter, the builder started work. By night, he hauled the stones with Svaðilfari, and by day, he set the stones in place. All the Æsir were amazed at how quickly the work progressed. At the end of winter, the walls were so high that they were impregnable. Three days before the beginning of summer, only the entrance to the walled fortification remained to be done. The gods assembled in council to decide what was to be done. It was clear by this time that the builder must be a giant. No one else could have completed the work so quickly. The thought of losing not only Freyja to the giants, but also the sun and the moon, was too much to bear. Something had to be done. The gods agreed that the person most responsible for this decision was Loki. They declared that Loki would face death if he did not devise some scheme to cause the builder to forfeit his payment.That evening, as the builder drove Svaðilfari out for more stone, Loki, in the guise of a mare, ran out from the woods towards the stallion and neighed at him. When Svaðilfari realized what sort of horse the mare was, he went wild with desire. He tore apart his harness and chased after the mare. The mare ran into woods with the stallion close behind, and the builder chased after the two of them, trying to catch the stallion. All night long, the three tore through the woods. The next day, not much work was completed. When the builder realized that the job was not going to be completed on time, he fell into a giant rage, verbally abusing the gods for their trickery. The Æsir could see now that the builder was a giant in disguise, and they revoked their oaths and called upon Þór to pay the giant his wages. But instead of Freyja, the sun, and the moon, Þór paid off the giant with a blow from his hammer Mjóllnir, shattering the giant's skull into fragments and sending him down to Niflhel.A number of months passed before Loki was seen again. His romp with Svaðilfari had resulted in him giving birth to a foal. He returned to Ásgarð leading the colt. The colt was gray and had eight legs, and was called Sleipnir. He was the best horse among gods and men. Loki gave the colt to Óðin. Óðin will ride Sleipnir into battle at Ragnarök.