Washington (CNN) America's top diplomat on Iran was only a few minutes into a speech on Tuesday afternoon when he was handed an urgent note.

Brian Hook, the special representative for Iran, was in Los Angeles to talk about US policy toward Iran. But by the time he stepped to the podium, he was already more than an hour late, having spent much of the day on a secure line speaking to US officials in Washington including his boss, Secretary of State Mike Pompeo.

And now, he had to leave.

"The peoples of Iraq and Lebanon and Iran, they want their country back," Hook said. "And they are tired of Iran being unable to stay within its own borders. Thank you."

With that, Hook walked briskly off stage. America's defense apparatus was spinning into action.

Minutes earlier, US intelligence satellites had picked up signs of a heat signature from Iran, suggesting the country had just launched short range ballistic missiles. The US knew an impending attack was likely, thanks to a tip from the Iraqi government, which had been told by the Iranians an attack was coming and which bases to avoid.

Still, using information from the satellites and US aircraft in the region -- which had intercepted Iranian communications -- US intelligence analysts quickly determined that two bases in Iraq were the targets, al-Asad and Erbil. Within minutes, US troops stationed there were warned. They'd already been on high alert and sought safety in bunkers, according to a source familiar.

Troops took short-term cover the day before out of a concern of a possible attack. Now, the threat was imminent, and the troops were told of incoming missiles.

At 7:30 p.m. ET, the official announcement went out: Iran had launched more than a dozen ballistic missiles against US military and coalition forces in Iraq.

Just five days after killing Iran's top military leader, Qasem Soleimani, in a drone strike in Baghdad, this was the moment that the Trump administration had been preparing for -- a direct retaliatory attack from Iran.

News of the missile strike came as the administration had spent days stuck in a chaotic series of self-inflicted wounds. First, Trump had threatened to target Iranian cultural sites. Then there was the shocking letter announcing the withdrawal of US troops from Iraq that the Pentagon had to walk back as "a mistake."

It all raised questions about the administration's handling of the situation, and even had some Republican allies of the President privately expressing concern about whether the administration was truly prepared to deal with the events it had unleashed with the Soleimani strike.

The moment wasn't lost on Trump's top aides, who convened in the basement Situation Room knowing that the ensuing hours could redeem a chaotic several days -- or cement the impression of a rudderless ship.

On Tuesday evening, aides watched as the situation went from the potential for dramatic conflict to one that seemed to offer Trump a new opportunity to deescalate. Bolstered by incoming messages from Iran sent through back-channels, Trump's aides realized the damage would be limited.

"Iran appears to be standing down, which is a good thing for all parties concerned and a very good thing for the world," Trump said on Wednesday morning.

The following account of the events that unfolded over the past are based on interviews with dozens of Trump administration officials, foreign diplomats, as well as staffers and top lawmakers on the Hill.

Scramble to inform

Within an hour of the strikes, leaders on Capitol Hill were being briefed. In the House, Speaker Nancy Pelosi was discussing the situation in Iran with a group of top Democrats when she was handed a note with news of a rocket attack on a US military base in Iraq. Among those present was Rep. Dan Kildee, who told CNN that Pelosi paused the discussion to alert members of the situation. "Pray," Pelosi told members, according to Rep. Debbie Dingel.

Not long after, Pelosi got on the phone with Vice President Mike Pence, who briefed her on the Iranian attacks. Senate Minority Leader Chuck Schumer also received a call from Pence around the same time and was briefed on the attack.

Meanwhile, GOP leaders were getting briefed directly by the President.

At the Pentagon, Secretary of Defense Mark Esper met with the Joint Chiefs of Staff and senior defense officials as soon as he got word of the attack. Less than an hour after news broke of the Iranian strike, Esper's office reached out to Iraqi Prime Minister Adil Abdul al-Mahdi, who just days earlier had criticized the US drone strike on Soleimani as a "flagrant violation of the conditions for the presence of the American forces in Iraq."

At first Esper's team couldn't get through. It was around 3 a.m. in Baghdad and the number the Pentagon had for Mahdi's office wasn't working, said a source familiar with the outreach. They contacted the ambassador in Washington at home, who managed to connect the two parties.

After making a few calls to senior congressional officials, Esper and Army Gen. Mark Milley, the chairman of the Joint Chiefs of Staff, headed to the White House.

In the Situation Room

Just before 7:30 p.m. ET, Esper, Milley and Pompeo all pulled up to the White House within a few minutes of each other. Pompeo arrived first. As he waited for the others, Pompeo turned the light on in the back seat of his Cadillac sedan and was reading from two cellphones. Once the other two cars arrived, Pompeo, Esper and Milley all entered the West Wing together.

Soon, a group of senior administration officials were gathered in the Situation Room. Along with Pence, Pompeo, Esper and Milley, the group included national security adviser Robert O'Brien, acting Director of National Intelligence Joseph Maguire and acting chief of staff Mick Mulvaney. White House counsel Pat Cippolone and press secretary Stephanie Grisham were there, while CIA Director Gina Haspel joined by teleconference.

The first objective was to determine whether any Americans had died in the strike. A senator who spoke with Trump told CNN the President appeared ready to attack Iranian facilities had there been even one American casualty. Though it took well into the night to confirm, the early evidence suggested there had not been any American deaths.

That lack of casualties fed a sense of restraint in the room, according to sources. While some consideration was given to striking back at Iran that night, according to one White House official, the decision was made to hold off until more information came in regarding Iran's intentions and the conditions on the ground.

One of the initial reactions in the room was one of surprise that the Iranians fired so few missiles out of their arsenal of thousands, this source said. That, along with the expectation that Iran was always going to strike back, created a sense of calm. Though there was an obvious level of tension given the high stakes, part of the reason the group was more relaxed was the initial view among many administration officials that Iranians were more bent on sending a message than in killing Americans.

One source pointed to how accurate Iranians have been in the past with its ballistic missiles, such as the attack a Saudi oil refinery, suggesting the attack could have been lethal.

Within hours of the strike, the President had made clear he wanted to make a public address and began dictating an outline of what it should look like, according to a person familiar. As Trump and his advisers continued to meet in the Situation Room, aides began making urgent plans for an address to the nation, including prepping the Oval Office. The framework of a speech started coming together with aide Stephen Miller at the helm and senior advisers weighing in.

Over the past few days, top White House officials expressed regret that Trump hadn't addressed the nation sooner after the strike that killed Soleimani, and worried he'd missed a chance to shape the narrative in his favor. Right after the strike, White House aide and Trump son-in-law Jared Kushner was among those pushing Trump to give a speech, but the decision was made to hold off.

On the Hill, Republican leaders were getting constant updates from the White House, including Senate Majority Leader Mitch McConnell and Senate Armed Services Chairman Sen. James Inhofe. The overall message to the White House from Republicans, according to a number of sources, was one of restraint, and that this was a moment for de-escalation.

In the end, White House officials said Trump would not emerge on Tuesday night to make an address. The news was met with a collective sigh of relief on the Hill, according to several GOP aides who spoke to CNN.

Around 9 p.m. ET, Trump began making calls to several GOP lawmakers, including Inhofe, who told reporters Wednesday the President was in a "very, very positive" mood and said he was willing to negotiate with Iran. Inhofe agreed, telling the President this was an opportunity to not just de-escalate but start negotiations.

At 9:45 p.m. ET, Trump tweeted, "All is well! Missiles launched from Iran at two military bases located in Iraq. Assessment of casualties & damages taking place now. So far, so good! We have the most powerful and well equipped military anywhere in the world, by far! I will be making a statement tomorrow morning."

But the night was far from over.

Iran Back Channels

Starting late Tuesday night and into Wednesday morning, Iran initiated contact with the administration through at least three back channels, including Switzerland and other countries, according to a senior administration official. The message from Iran was clear: This would be their only response. They would now wait to see what the US would do.

As part of its response back, the US conveyed that it was fully aware that Iran controls its proxies in the region, including Hezbollah. The source told CNN that Iran tried to "squeak out of it," saying they are not responsible for those proxies, but the US made clear it didn't buy that argument.

By around 1 a.m. ET, the battleground assessment came in, confirming no US casualties . Working through the night without sleep in secure rooms at the White House, national security teams put response options together, including plans to sanction Iran.

By early Wednesday morning, teams met again with the President, who was given an update on the latest assessment. That's when Trump made the final decision that the US response would be sanctions, signaling to advisers that the threat of escalation was off the table.

"They've stepped back -- now we've taken a bit of a step back," said a senior administration official.

Trump then began making his own edits on the draft of the speech, as did a handful of other national security advisers including Esper, Pompeo and Milley. As the White House prepared for an 11 a.m. ET address to the nation, advisers kept weighing in on the draft of the President's remarks, delaying Trump's address by nearly half an hour.

As junior staffers and reporters crowded into the red-carpet lined White House foyer, the President's top national security officials -- many wearing their military uniforms -- lined up on either side of the podium.

The wooden doors opened behind them and Trump -- silhouetted by late-morning sun -- emerged.