Before Rudolph W. Giuliani was ordered to pay $148 million to two Georgia election workers he defamed, and before he owed his own lawyers several million dollars more, federal prosecutors were scrutinizing whether he pursued dubious business dealings in Ukraine to shore up his dwindling fortune, according to court records unsealed late Tuesday.

The documents lifted the veil on a criminal investigation that federal prosecutors spent three years conducting into the dealings of Mr. Giuliani, the former New York mayor who had reinvented himself as Donald J. Trump’s personal lawyer and attack dog.

The investigation, which did not result in charges for Mr. Giuliani, centered on whether he illegally lobbied the Trump administration in 2019 on behalf of Ukrainian officials. Those same Ukrainians helped Mr. Giuliani dig for dirt on Joseph R. Biden Jr., who was then on his way to becoming the Democratic presidential nominee and who would ultimately defeat Mr. Trump in 2020.

The prosecutors had assembled enough evidence to persuade a judge in April 2021 to authorize the seizure of Mr. Giuliani’s phones and computers, an extraordinary step to take against any lawyer, let alone one who had represented a sitting president. And for a time, it appeared as if the prosecutors, working in the same Manhattan U.S. attorney’s office that Mr. Giuliani had presided over decades earlier, might seek to indict him.

But when they failed to find a smoking gun in Mr. Giuliani’s electronic records, the prosecutors notified the judge overseeing the matter that they had ended the long-running investigation.

A spokesman for Mr. Giuliani did not immediately respond to a request for comment late Tuesday.

The judge, J. Paul Oetken, recently ordered the prosecutors to release the search warrant materials in response to a request from The New York Times. Mr. Giuliani consented to the newspaper’s request, as did the government, with certain redactions to protect privacy interests.

While much of the evidence that underpinned the search warrant had already come to light in the media and through Mr. Trump’s first impeachment proceedings in late 2019, the search warrant materials represent the government’s most comprehensive catalog yet of Mr. Giuliani’s ties to Ukraine.

And for the first time, the records explicitly linked Mr. Giuliani’s recent financial troubles to his dealings in Ukraine, suggesting that he did not just want Ukrainian officials’ help in attacking Mr. Biden but also their money.

Although Mr. Giuliani’s money woes have hardly been a secret, the records offer a previously unreported snapshot of his financial condition and show that those problems were undergirding the federal investigation into the former mayor.

In May 2018, as Mr. Giuliani was leaving his law firm to work for Mr. Trump, he had approximately $1.2 million in cash, and some $40,000 in credit card debt, according to the search warrant records. By early 2019, just as he was building ties to the Ukrainian officials, he was down to approximately $400,000 in cash and up to $110,000 in credit card debt.

A month later, according to the search warrant records, “his account balances had dropped to approximately $288,000,” a decline that coincided with an expensive divorce from his third wife, Judith Nathan.

Although the Manhattan U.S. attorney’s inquiry once appeared to pose the gravest legal danger to Mr. Giuliani — and its closure represented a significant victory for him — the reprieve was short lived.

In Georgia this year, Mr. Giuliani was indicted in a sweeping racketeering case in connection with his effort to keep Mr. Trump in office after the 2020 election. In New York, his own lawyer sued him for failing to pay legal bills from a wide array of work, ranging from the investigation into his ties to Ukraine to the expansive litigation stemming from the 2020 election fight. And in Washington last week, a federal jury ordered him to pay the $148 million to the two Georgia election workers whom he defamed by spreading baseless claims that they tried to steal votes from Mr. Trump. That blow will most likely prompt him to file for bankruptcy.

Together, the cascade of legal woes has marked a remarkable reversal of fortune for Mr. Giuliani, once known as “America’s mayor” in the aftermath of the Sept. 11, 2001, attacks.

Just a few years ago, he was worth millions, thanks to lucrative contracts and law firm partnerships he gained after leaving City Hall. At age 79, Mr. Giuliani is now facing financial ruin, and, potentially, years in prison.

The federal investigation into Mr. Giuliani culminated in the F.B.I. seizure of electronic devices from his Manhattan apartment and office. The seizure marked a dramatic escalation of an investigation that grew out of a criminal case against two Soviet-born men who helped Mr. Giuliani pressure Ukrainian officials to investigate Mr. Trump’s rivals, including Mr. Biden and his son, Hunter, who had served on the board of a Ukrainian energy company.

One of the Soviet-born men, Lev Parnas, turned on Mr. Giuliani and offered evidence against him. He was convicted in October 2021 and sentenced to 20 months in prison, while the other man, Igor Fruman, pleaded guilty and received a one-year sentence.

A main focus of the investigation into Mr. Giuliani was his role in lobbying the Trump administration to remove the U.S. ambassador to Ukraine, Marie L. Yovanovitch. Some of the same Ukrainian officials whom Mr. Giuliani wanted to implicate the Bidens — including a high-ranking prosecutor, Yuriy Lutsenko — had butted heads with Ms. Yovanovitch over her condemnation of political corruption, and they wanted her gone.

Mr. Trump ultimately recalled her in May 2019 — a move that was scrutinized during his first impeachment trial — but it took time and several false starts.

In May 2019, Mr. Giuliani texted Mr. Parnas, seemingly in a sarcastic tone, “Boy I’m so powerful I can intimidate the entire Ukrainian government,” adding, “Please don’t tell anyone I can’t get the crooked Ambassador fired or I did three times and she’s still there.”

The search warrant materials underscore how extensively the Manhattan prosecutors examined whether Mr. Giuliani had struck a quid pro quo with Mr. Lutsenko: pushing for Ms. Yovanovitch’s removal in exchange for information about the Biden family that would benefit Mr. Trump.

The affidavit that an F.B.I. agent submitted to Judge Oetken in support of the search warrant detailed the array of evidence linking Mr. Giuliani to Mr. Lutsenko, whose name was largely redacted.

In particular, according to the affidavit, the authorities scrutinized Mr. Giuliani’s pursuit of a $200,000 contact with Mr. Lutsenko to retain Mr. Giuliani’s firm to help recover funds believed to have been stolen from Ukrainian government coffers.

And with Mr. Giuliani’s finances worsening, he “had a financial interest in receiving such a retainer agreement from” Mr. Lutsenko, the affidavit said.

Mr. Parnas, the affidavit said, had also stated that Mr. Lutsenko would only implicate the Bidens if the ambassador was going to be removed. Mr. Lutsenko wanted her gone for “his own career reasons,” according to Mr. Parnas, the affidavit said.

“It appears that Giuliani took steps to cause the firing of the ambassador to prove to” Mr. Lutsenko “what he could achieve in order to, among other things, secure the legal representation,” according to the affidavit.

It is against federal law to try to influence the U.S. government on behalf of a foreign official without registering as a foreign agent.

Yet Mr. Giuliani argued that he was acting as Mr. Trump’s lawyer, not as a lobbyist for Ukrainian officials. He wanted Ms. Yovanovitch gone, he contended, because he believed she was disloyal to Mr. Trump and hostile to his attempt to uncover information about Mr. Biden.

Facing this defense, the Manhattan prosecutors decided to close the investigation.

In a letter to Judge Oetken last year, the prosecutors wrote that “based on information currently available to the government, criminal charges are not forthcoming.”

Kenneth P. Vogel contributed reporting.