It is narrated from Sayyiduna Abu Sa'id al-Khudri (may Allah be pleased with him): I heard the Messenger of Allah (peace and blessings be upon him) saying: "When one of you intends to do something, let him say: 'Allahumma inni astakhiruka bi-'ilmika, wa astaqdiruka bi-qudratika, wa as'aluka min fadlika al-'azim, fa-innaka taqdiru wa la aqdiru, wa ta'lamu wa la a'lamu, wa anta 'allamu al-ghuyub. Allahumma in kana kadha wa kadha khayran li fi dini wa ma'ishati wa 'aqibati amri, wa illa fasrifhu 'anni wasrifni 'anhu, thumma qaddir liya al-khayra haythu kana, wa la hawla wa la quwwata illa billah' (O Allah, I seek good from You through Your knowledge, and I seek ability from You through Your power, and I ask You of Your immense bounty, for You have power and I have none, You know and I do not know, and You are the Knower of all that is unseen. O Allah, if this matter (here he mentions the thing he intends) is better for me in my religion, my livelihood and the outcome of my affairs (then decree it for me); otherwise turn it away from me and turn me away from it, then decree for me good wherever it may be; and the strength to avoid sin and the power to do good come only through the help of Allah)."
Imam Abu Ya'la, Ibn Hibban in his Sahih, Ahmad bin Hanbal, and al-Tabarani in Kitab al-Du'a recorded it. It also has a supporting narration (shahid) from the hadith of Sayyiduna Jabir bin Abdullah (may Allah be pleased with them both), which Imam al-Bukhari and the authors of the four Sunan recorded; Imam Ahmad and al-Tabarani also recorded it from the hadith of Sayyiduna Abu Ayyub (may Allah be pleased with him), and Ibn Hibban and al-Tabarani from the hadith of Sayyiduna Abu Hurayrah (may Allah be pleased with him).