SAO PAULO, Brazil — His political career hung by a thread, but Brazilian President Michel Temer spent Wednesday afternoon working on a victory speech. Just a few miles away, 500 lawmakers in Brazil’s Chamber of Deputies would hold a vote to decide whether to suspend his mandate for six months and approve a corruption trial in the Supreme Court.

At the end of the day, the decision was clear. By a vote of 263 to 227, Brazil’s lawmakers shrugged off the charges and stuck with Temer, whose approval ratings are at a dismal 5 percent. The results were a testament to Temer’s skill as a politician but also revealed the lengths to which he would go to stay in power.

Two months ago, analysts declared Temer’s government dead after leaked recordings appeared to show the president condoning the payment of hush money to protect a wealthy meatpacking executive. Next, a videotape surfaced of a close aide receiving a suitcase full of cash — part of a $12 million bribe from the executive allegedly intended for Temer, a charge he denies.

Calls for Temer's resignation abounded, and his coalition worried that he wouldn’t last a week. Still, Temer vowed to hold on until he was kicked out. Over the next two months, he released more than $1 billion of federal funds for congressional projects, according to Open Records, a transparency nonprofit group. He openly stacked the congressional justice committee with allies and shuffled personnel to reward supporters with coveted positions in his government. He attended dinners and birthday parties. He met with lobbyists and interest groups, asking for their support in return for stability, a sacred concept in Brazil’s volatile, commodities-driven economy. Now, he was ready to celebrate.

“We always knew administrations exchange favors, but this was very ostentatious,” said Alexandre Bandeira, a strategic consultant in Brasilia. “It was an open purchase of support and showed the public that the quest to stay in power was personal for the president.”

Temer would have been the second Brazilian president to be suspended in 15 months. He took office after his predecessor, Dilma Rousseff, was impeached on extremely technical charges that most Brazilians struggled to understand. But at the time, with the economy in tatters, throngs of Brazilians choked the country’s avenues to call for her resignation. The mood could not have been more different on Wednesday night, as Temer faced a similar fate.

Inside the Chamber of Deputies, Temer’s opponents called for early elections and showered his supporters with fake bills as they cast their votes. But outside the outrage was muted. While more than 80 percent of Brazilians wanted Temer to go to trial, according to polls, three years of economic recession aggravated by a behemoth political crisis tapped their will to fight.

As politicians cast their votes in support of the president, the most common refrain was concern about the economy.

“At this time, it is not reasonable to suspend President Temer for six months, to send the country down the path of political instability and economic uncertainty,” said Rep. Luiz Felipe Baleia Rossi, a member of Temer’s Democratic Movement Party, as he cast his vote in support of the president.

But the fight to stay on may have drained the president of the political capital needed to govern. Temer has promised to use his victory to try to pass flagship reform programs he believes are key to resuscitating the economy. The unpopular measures require a two-thirds majority in congress, a level of support he has yet to achieve.

He will also likely face obstruction of justice and racketeering charges in connection with the same corruption case in the coming months, setbacks that would require similar levels of political maneuvering to preserve his mandate.

Yet on the podium in the aftermath of the vote, Temer said he would fight to modernize Brazil until the last day of his administration.

“We are pulling Brazil from the gravest economic crisis in our history,” he said. “ I will not rest until Dec. 31, 2018.”