With hours left before polls closed in her race to defeat Republican Rep. Mark Sanford, Katie Arrington got a shocking text message from a friend: President Donald Trump had just endorsed her on Twitter.

"I was stunned and grateful," she told NBC News, hours after squeaking out a surprise win in the GOP primary for South Carolina's 1st Congressional District. "That tweet kept it from a runoff, without a doubt. We needed the 50 percent plus one, and I'm sure that that tweet put us in that realm."

South Carolina Rep. Katie Arrington campaigns after voting in the primary election on June 12, 2018 at Bethany United Methodist Church in Summerville, South Carolina. Kathryn Ziesig / The Post And Courier via AP

Arrington, 47, won with 50.6 percent of the vote, according to The Associated Press, going from underdog primary challenger up against an incumbent who had never lost an election to November favorite in the safe GOP district.

She is the latest candidate to benefit from a wholehearted embrace of the president, a stance that made Trump's endorsement particularly effective because it confirmed her message to voters all along: She wasn't just any Republican. She was a Trump Republican.

"We are the party of President Donald J. Trump," she said in her victory speech Tuesday night.

A first-term state representative who works for military contractors, Arrington campaigned on traditional conservative bonafides, like being for lower taxes and against abortion, but most of her television ads focused on pitching herself as the ally Trump needs in Congress.

Sanford has been critical of the president — Trump "fanned the flames of intolerance" and was unprepared for the presidency, he said previously — and it gave his opponent ample television soundbites to splice together and paint him as disloyal.

"Mark Sanford and the career politicians cheated on us," she said in one ad that alludes to Sanford's infamous extra-marital affair. "Bless his heart, but it's time for Mark Sanford to take a hike. For real this time."

With just days left in the race, Sanford rolled out an ad of his own insisting that he'd "overwhelmingly" voted with the president. But Arrington still won out.

Like Trump, she pledged to "drain the swamp." She said she would donate her congressional salary to charity (Trump donates his salary as president) and that she would limit herself to eight years in Washington (Trump has called for congressional term limits).

"I'm only going to go, if they'll have me, four terms," she told NBC News. After that, she wants to take a year to tour the country in an RV with her husband.

"Career goals!" she said.

Trump congratulated Arrington in a tweet on Wednesday, suggesting that he had been discouraged from making an endorsement in a primary challenge to a sitting Republican representative, but couldn't resist. The pair also spoke by phone on Wednesday as well, Arrington said in a Facebook post.

My political representatives didn’t want me to get involved in the Mark Sanford primary thinking that Sanford would easily win - but with a few hours left I felt that Katie was such a good candidate, and Sanford was so bad, I had to give it a shot. Congrats to Katie Arrington! — Donald J. Trump (@realDonaldTrump) June 13, 2018

If she wins in November, Arrington would be South Carolina's first Republican woman to be elected to federal office.

It's a first that may help boost her party in a midterm year that has seen Democratic women run for office in droves, responding in part to a president who has been accused by more than a dozen women of sexual misconduct and who was recorded in 2005 boasting about grabbing women without their consent.

The GOP has struggled to recruit as many women, and Arrington's past experiences — living paycheck-to-paycheck, working the night shift at Denny's, and starting a day care so she could care for her own special-needs child while working — distinguished her in the race against Sanford, a millionaire. At 47, she's more than a decade younger than the average House member, too.

State Rep. Katie Arrington with supporters in North Charleston, South Carolina, after defeating U.S. Rep. Mark Sanford in the GOP primary. Andrew Whitaker / The Post And Courier via AP

"I'm a hard worker and I keep my word," she said in a phone interview after her primary victory. "That goes from the graveyard shift to Denny's to the briefing rooms of the Pentagon and hopefully to the halls of Congress."

Asked if there was any daylight between her and the president on the issues, Arrington said it was impossible for two people to agree on everything ("I've been married," she quipped), but said that she would not criticize the president in the press.

"I’m not going to go on CNN to bash him," she said. "Now I have the rapport to go in and speak to him and have a conversation."