Ted Cruz once called Trump ‘utterly amoral’ and a ‘sniveling coward.’ Then he worked to save his presidency.

Four years later, one of the nastiest rivalries in recent memory — a brawl both personal and political — has given way to a mutually beneficial partnership forged in the crucible of impeachment.

Working inside the Senate and out, Cruz (R-Tex.) played a unique role in securing Trump’s swift acquittal at the third-ever presidential impeachment trial — simultaneously serving as legal strategist, jury consultant, messaging guru, broadcast surrogate and, unexpectedly, a breakout podcasting star.

Once a bitter foe who went to great lengths to stop Trump, Cruz has long since abandoned his animosity to embrace a president who enjoys sky-high GOP approval and rages against those who dare to break with him. Following his outsized work on impeachment, Cruz can fairly claim to have helped save Trump’s presidency.

“My attitude is, I’ve got a job to do,” Cruz said in an interview. “To do my job, I’ve got to work with the president. And, you know, I could have made the choice to allow my feelings to be hurt, to take my marbles and go home. But I think that would’ve been an irresponsible choice.”

Besides being a particularly stark illustration of the axiom that there are no permanent enemies in politics, Cruz’s decision to enthusiastically don the mantle of presidential guardian also represented, to many Republicans, an instance of a man meeting his moment.

A former Supreme Court clerk, Justice Department official and appellate litigator, Cruz until his 2012 election had spent his career ascending toward the pinnacle of the conservative legal world — a place where he might well have been leading Trump’s defense instead of White House Counsel Pat Cipollone.

“This is his niche,” said Sen. John Cornyn, a fellow Texas Republican who at times has been at odds with Cruz, who wasn’t always considered a Senate GOP team player. “Because of his experience, I think when he talks, people listen, and I think he was one of the people who helped navigate a very difficult path.”

Cruz’s unusually aggressive dual role as juror and de facto auxiliary defense counsel encapsulated everything many Democrats despised about the trial that Senate Majority Leader Mitch McConnell (R-Ky.) and other GOP senators had orchestrated — a betrayal by design of the oath that senators took to do “impartial justice” under the Constitution.

But they were also amazed at the notion that Cruz — who famously went to the floor of the 2016 Republican National Convention and urged the GOP to “vote your conscience,” not for Trump — had become so obeisant.

“When he turned, he really turned, didn’t he?” said Sen. Mazie Hirono (D-Hawaii). “Into a total suck-up — you can quote me on that.”

Cruz has long since forgiven Trump for past grievances, casting his reconciliation as a matter of Christian morality — never mind that Trump never actually sought forgiveness, he said. Politically, Cruz, 49, faces the reality that any Republican harboring presidential ambitions in 2024 or beyond must be in lockstep with the president.

In Cruz’s telling, his most valuable contribution to Trump’s defense came in framing his case before a potentially fickle jury. While Trump’s removal from office was never seriously in doubt in the Republican-controlled Senate, there was a real possibility that a handful of GOP senators would want to hear from witnesses, adding weeks or potentially months to the trial and opening the door to new and damaging revelations.

Throughout the impeachment process, Trump and his allies argued that there was “no quid pro quo” in withholding military aid from Ukraine at the same time Trump pressured Ukraine to investigate former vice president Joe Biden and his family.

That argument, Cruz feared, would not fly in the Senate — especially with former national security adviser John Bolton willing to testify and potentially ready to rebut it.

So Cruz made his view known, starting with an Oct. 30 lunch with fellow Senate Republicans, then at a Nov. 21 meeting with Cipollone and other members of the Trump defense team, and frequently in public after that: Getting hung up on “quid pro quos” was a losing battle. Better, he said, would be to make the case that Trump’s interest in the Bidens was justified.

Meanwhile, Cruz was among the first senators to connect that argument with the key question of whether witnesses would be called.

Along with Sen. Rand Paul (R-Ky.), Cruz began in mid-January to promote the notion that if Democrats wanted to call Bolton, Republicans would demand that Biden’s son Hunter testify about his well-paid service on the board of the Ukrainian gas company Burisma.

Trump and Republicans have accused Biden — without evidence — of ousting the prosecutor because he was investigating Burisma. In fact, former U.S. and Ukrainian officials have said the prosecutor’s investigation of Burisma had been dormant.

“We’re not going to see the one-sided show trial that the House has engaged in for months,” Cruz said on Sean Hannity’s Fox News show on Jan. 15. The message Cruz was trying to send was aimed less at Hannity’s conservative viewers and more at the small handful of undecided GOP senators who were entertaining a vote for witnesses.

“The House managers were saying, ‘Oh, we can do this in a week; it’ll be quick and easy,’ ” he said. “Understanding all that going down the road of additional witnesses entailed was an important factor in bringing them to the decision of: ‘We’ve heard enough; it’s time to reach final judgment.’ ”

Enter Alan Dershowitz, the Harvard Law School professor who taught Cruz in the early 1990s and joined Trump’s defense team just days before the trial. He and Cruz had been in agreement for months, though, in arguing a key point: Even if there was a quid pro quo, it would not be impeachable.

Dershowitz, who praised Cruz as having “one of the best analytic minds that I experienced in my 50 years of teaching,” said the two did not coordinate ahead of the trial but suggested they had approached the facts from a similar perspective.

“We’re both extremely independent. We don’t follow the crowd. And I think we have a lot of self-confidence,” Dershowitz said. “Virtually every academic disagrees with me, and I think they’re wrong, and I’m happy to say that. . . . He wouldn’t care what anybody else in the class thought, and mostly they didn’t like it.”

Meanwhile, Cruz and his advisers saw the impeachment drama as an opportunity to kick-start a project they had been pondering for months — a podcast targeting conservatives who didn’t have hours every day to watch cable news or scan Twitter.

They collaborated with The Daily Wire, a conservative online media enterprise, to quickly launch the podcast, and after each trial day wrapped — in some instances in the early hours of the morning — Cruz left the Senate and headed to a downtown studio to tape “Verdict With Ted Cruz.”

The first episode debuted Jan. 21, and on Jan. 26, “Verdict” began a week-long stint at the very top of Apple’s U.S. charts, with some of the roughly half-hour episodes garnering hundreds of thousands of additional views on YouTube.

While the podcast’s popularity shows Cruz has maintained his esteem among committed conservatives, his status in Trump’s orbit is somewhat more complicated.

When Trump heaped praise on his legions of congressional defenders at a freewheeling East Room celebration the day after his acquittal, Cruz was not among them. He chose instead to fly back to Texas after the trial.

But Cruz said that Trump has been privately grateful, including in a Feb. 2 phone call just days before the verdict, where he took the opportunity to give the president a final bit of advice: With the State of the Union address coming a day ahead of the planned acquittal vote, Cruz warned that a premature victory lap could backfire and counseled him to avoid discussing impeachment.

“He followed that advice,” he said. “I wasn’t the only one giving it to him, but I was glad he did.”

The White House did not respond to a request for comment on Cruz’s role. But at a Jan. 29 event celebrating the ratification of a new North American trade deal, Trump singled Cruz out for praise.

“Thank you, Ted, for everything,” he said. “You’ve been incredible.”