Gov. Charlie Baker and Mayor Martin J. Walsh led an unprecedented taxpayer-funded effort to salvage the doomed IndyCar race, motivated to prove government wouldn’t get blamed for failing to land another big sporting event, newly released emails show.

Top Walsh aides even organized a quiet celebration with coffee and pastries a day after the Grand Prix of Boston was abruptly canceled, saying they wanted to honor the “amazing work” of the team and mark a “proper end to this incredible process,” according to emails.

“Early in this process we assured all involved that if the race did not happen it would not be because of government,” state Transportation chief Tom Tinlin wrote to dozens of city and state staffers. “We should all be pleased to know that in fact, was the truth.”

The emails also show Baker and his top aides were heavily involved in trying to save the race after organizers hit environmental snags. The governor led at least two meetings in March and April, the last one just days after the Boston Conservation Commission made a devastating vote to force race organizers to get another state environmental permit.

The vote apparently escalated concerns that the race wouldn’t come off and prompted a high-level meeting of top Baker and Walsh aides to map out a plan to save it.

“As the Governor said on last night’s call, it would be terrible to get this far down the road and then have the worse case scenario have to happen at the 11th hour,” Baker’s deputy chief of staff Mindy D’Arbeloff wrote on April 25, days after the city commission vote that put the fate of the race in the hands of state environmental regulators.

A Baker spokeswoman said she couldn’t comment on the email because she hadn’t seen it.

The emails, released under a public records request by a group opposing the Grand Prix and obtained by the Herald, show that despite promises of not spending taxpayer money on the race, at least 150 government officials were involved in planning the event and negotiating permits.

City and state leaders seemed to be motivated to avoid another failure in the wake of the collapse of the Boston 2024 Olympic bid.

In the end, the Walsh administration did in fact get blamed by some race fans and Grand Prix CEO John Casey for the race’s demise.

But the emails, along with others released by the city to the Herald, show that the Walsh and Baker administrations were acting as virtual partners with the promoters and pressured regulators to approve the needed permits.

Walsh’s chief of operations, Pat Brophy, contacted state Secretary of Energy and Environmental ?Affairs Matthew Beaton to organize a meeting with state environmental affairs chief Deirdre Buckley. Brophy was concerned her office hadn’t approved needed permits for the Grand Prix.

Beaton wrote back that he had contacted Buckley and hoped to “expedite this issue as quickly as possible.”

Buckley, in fact, never did issue any permits for the race before it was canceled.

The emails also show Brophy enlisted city governmental affairs chief Timothy Sullivan to contact Buckley and schedule a meeting. Sullivan was indicted a few months later on corruption charges related to alleged pressuring of a music festival to hire union stage hands.