Orlin Wagner / AP

Republicans narrowly avoided an embarrassing defeat in the first congressional election of Donald Trump’s presidency on Tuesday, as the party retained a House seat in a deeply-red Kansas district that became surprisingly competitive.

Ron Estes, the Kansas state treasurer, held off Democrat James Thompson to win a special election to fill the seat vacated in January when conservative former Representative Mike Pompeo became CIA director. But the close result in a district Trump dominated in November was sure to jolt Republicans and buoy Democrats who will need a wave election to retake the House majority next year. With all precincts reporting, Estes had won, 53-46 percent.

The race had drawn little attention before the last few days because as Democrats were the first to acknowledge, they really had no business competing in Kansas’s 4th district. Reliably red for the last 22 years, the district went for Trump by 27 points in November, and in his three House campaigns, Pompeo never won less than 60 percent of the vote.

Both parties instead had looked to Georgia to wage the first competitive congressional battle of the Trump era. In a race to replace Tom Price, the former conservative lawmaker who became secretary of health and human services, Democrat Jon Ossoff has raised more than $8 million from energized party donors and jumped out to a wide lead over a crowded field of Republicans. He hopes to win more than 50 percent of the vote on April 18 and avoid a runoff election in which a Republican would be favored.

But while money and volunteers flooded into Georgia, the combination of enthused Democratic voters, an unpopular new president, and a toxic conservative governor presented an even more urgent threat to Republicans in Kansas. With less than a week to go before the vote, the National Republican Congressional Campaign sent an infusion of cash for attack ads against Thompson, and both President Trump and Vice President Mike Pence recorded automated calls to the Republican rank-and-file in support of Estes. Senator Ted Cruz flew in for a last-minute airport rally on Monday.

The national Democratic Party stayed away from Kansas, even as it became clear Republicans were growing nervous. The Democratic Congressional Campaign Committee and the party’s House super PAC both opted against spending money in the district, and strategists said in the closing days the race was simply not winnable. Democrats appeared to be playing an expectations game common in special elections, betting they could generate momentum merely from a respectable showing in defeat without making an investment that risked setting party activists and donors up for disappointment. On the other hand, the narrow margin quickly prompted criticism from some on the left at the national party’s decision to write off the Kansas race as a lost cause.

In a statement after the vote, the DCCC said the district “should never have been on the table.” “This massive swing from the 2016 presidential results should set off alarm bells for House Republicans,” spokeswoman Meredith Kelly said.

Trump’s record low approval rating so early in his term has energized progressive activists hoping to pick up seats in a series of special elections for the House this spring. But in Kansas, the anchor weighing down Estes was less the president than the state’s governor, Sam Brownback, whose standing locally has plummeted amid an ongoing budget crisis. Democrats and moderate Republicans made gains in the state legislature last year, and Thompson attacked Brownback much more often than Trump during the campaign. The Wichita civil rights attorney and Army veteran might also have benefitted from being a political newcomer compared to Estes, who was twice elected to statewide office. Thompson told voters that he had been homeless as a child and only got involved in politics for the first time when he caucused for Bernie Sanders during the presidential campaign last year.

But whether it was because of the GOP’s late rescue effort or simply the partisan tilt of Kansas’s 4th district, Thompson could not pull off the upset. Estes’s win offers momentary relief for Republicans who feared an nearly unthinkable loss, but it portends a difficult road ahead for a party newly entrusted with the full reins of government in Washington.

We want to hear what you think about this article. Submit a letter to the editor or write to letters@theatlantic.com.