The original wording of this ḥadīth is found in the Musnad, as well as in the Sunan of Abū Dāwūd, al-Tirmidhī, and Ibn Mājah (1).
The full, extended version appears in Dalāʾil al-Nubuwwah (2) by al-Bayhaqī.
Al-Bayhaqī (3) said:
Chapter: What became manifest in the well that was at Qubāʾ through his ﷺ blessing
Abū’l-Ḥasan Muḥammad ibn al-Ḥusayn al-ʿAlawī narrated to us; Abū Ḥāmid ibn al-Sharqī related to us; Aḥmad ibn Ḥafṣ ibn ʿAbd Allāh related to us; my father related to me; Ibrāhīm ibn Ṭahmān narrated to us, from Yaḥyā ibn Saʿīd, who said that he was told:
Anas ibn Mālik came to them at Qubāʾ and asked about a certain well there. He continued, “I showed him the well, and he said, ‘Yes, this is it. A man used to draw water for his donkey and the well would run dry. Then the Messenger of Allah ﷺ came, ordered that a bucketful be taken, and it was brought to him. He either performed ablution with it or spat into it, after which he commanded that the water be poured back into the well. From that day on it never ran dry.’ He added, ‘I saw him relieve himself, then he returned, performed ablution, wiped over his leather socks, and prayed.’”
Abū Bakr al-Bazzār said:
Walīd ibn ʿAmr ibn Miskīn narrated to us; Muḥammad ibn ʿAbd Allāh ibn Mathnā narrated to us, from his father, from Thumāmah, from Anas, who said:
“The Messenger of Allah ﷺ once came to us and we served him from a well in our courtyard that had been called al-Nuzūr in the pre-Islamic period. He spat into it, and it never ran dry thereafter (4).”
He then remarked, “We do not know this ḥadīth to be transmitted except through this chain.”
[Chapter: His ﷺ multiplication of foodstuffs]—because the community was often in need of such miracles, as we shall set forth in detail (5)—and his multiplication of milk on several occasions as well.
Imām Aḥmad (6) said: Rūḥ narrated to us; ʿUmar ibn Dharr narrated to us, from Mujāhid, that Abū Hurayrah used to say:
“By Allah, I would press my liver against the ground out of hunger, and I would bind a stone to my stomach because of hunger. One day I sat on the path along which they would pass. Abū Bakr came by, and I asked him about a verse from the Book of Allah, Mighty and Majestic—asking only in hopes that he would invite me along—but he did not …”