Republican Sen. Ted Cruz has managed to avoid an embarrassing defeat in bright red Texas, beating Democratic Rep. Beto O’Rourke in the widely watched US Senate race.

ABC News projected Cruz would win the race at 10:09 p.m. based on exit polls. Cruz led O’Rourke 51.2 percent to 48.1 percent with 83 percent of precincts reporting.

“Texas saw something this year that we’ve never seen before. This election wasn’t about me and it wasn’t about Beto O’Rourke, this election was a battle of ideas,” Cruz told his fans. “It was a contest of who we are and what we believe.”

Texas, Cruz said, “came together behind a common sense agenda.”

“I’m so f***ing proud of you guys,” O’Rourke told his spirited supporters gathered in his hometown of El Paso.

“Tonight’s loss does nothing to diminish how I feel about Texas and this country,” he said, adding that he had called Cruz and congratulated the senator, pledging to help him. “And we will see you out there down the road,” O’Rourke said, keeping the door open for another political run.

O’Rourke’s run against Cruz gained him fame bigger than Texas. He was cool enough to get a last-minute endorsement from Beyonce, a Houston native. Two hours before most of the state’s polls closed, she posted a series of photos on Instagram posing in a black and white “Beto for Senate” ballcap.

The Democratic congressman from El Paso raised a whopping $69 million and growing – the most of any US Senate candidate in history. Hours before Election Day’s end, his campaign was still sending out pleas. In contrast, Cruz raised $40 million. Not counting outside money, the Cruz-O’Rourke contest was 2018’s most expensive Senate race.

The Democrat got the endorsement of Texan Willie Nelson, who performed at a rally. Hollywood money came pouring in from Kyra Sedgwick, Bradley Whitford, Zooey Deschanel, John Goodman, Wanda Sykes, Zach Braff, John Slattery and more.

Polling showed Cruz – vying for his second term – consistently ahead, but by single digits. Republicans typically have a double-digit advantage over their Democratic counterparts in the deep red state.

The tightness of the race meant Cruz had to lean on his former political rival – President Trump, whom he competed against in the 2016 Republican presidential primary. Trump traveled to Houston in late October on Cruz’s behalf. The president’s campaign hosted an all-day tailgate and a rally for supporters, while Trump gave Cruz a new nickname: “Beautiful Ted.”

O’Rourke had reminded voters of Cruz’s original Trump moniker — “Lyin’ Ted” — when the candidates last faced off against each other in late October. “He’s dishonest,” O’Rourke said then. “It’s why the nickname stuck – because it’s true.”

Cruz used the opportunity to go after O’Rourke’s out-of-state supporters: “left-wing national activists and left-wing national donors.” He suggested an O’Rourke win would lead to “two years of a partisan circus and a witch hunt on the president.”

“Really interesting to hear you talk about a partisan circus after your six years in the US Senate,” O’Rourke shot back, alluding to Cruz’s role in leading the Republicans in 2013 to shutting down the government.

The Texas GOP had also played up O’Rourke’s punk rock past – the Democrat had been in the band Foss – and two alcohol-related arrests from his 20s.

The waning moments of the campaign provided Democrats with hope – a Change Research poll had the candidates tied at 49 percent.

Meanwhile, a comedy bit put the candidates in the national spotlight yet again. Broadcast Monday night on the “Late Show with Stephen Colbert,” Triumph the Insult Comic Dog – a cigar-smoking hound puppet – mocked O’Rourke, Cruz and their supporters.

Cruz tried to counter the dog puppet by saying Democrats were responsible for him being snipped. “I support spaying and neutering,” Triumph shot back. “Just like Trump did to you.”