The House narrowly passed legislation on Thursday to partially repeal and replace Obamacare, giving GOP leadership and the White House a major legislative win after weeks of negotiations with conservative lawmakers who pushed for a tougher bill.

The House voted 217-213 to pass the amended American Health Care Act, after deciding late Wednesday that the votes were there thanks to last-minute changes that appealed to centrists.

When the vote was called, 20 Republicans voted against it, just under the threshold of 22 GOP lawmakers that could vote against it and still have it pass.

The vote is a win not just for Trump, who has been pushing the House to vote for weeks, but for Republicans who have been looking for a way to dismantle Obamacare since it was passed in 2010.

It's also an important victory for House Speaker Paul Ryan, R-Wis., who's leadership has been questioned among conservatives as the effort failed to materialize in a vote over the last few months. Ryan spoke just before the vote to thank President Trump for helping to lead the House on healthcare, and reminded GOP lawmakers that they promised their constituents to get rid of the controversial law.

"A lot of us have been waiting seven years to cast this vote. Many of us are here because we pledged to cast this very vote, to repeal and replace Obamacare," Ryan said. "To rescue people from this collapsing law."

House Whip Steve Scalise said the victory was also important to show that the Republican majority can govern. "It unified a conference that needed to find its footing in this unique environment where we have the House, Senate and White House and have this opportunity to do some big things to get this country back on track," he said after the successful vote.

Scalise also conceded that if they didn't get healthcare done, it was "going to impede the rest of our agenda."

Republicans are hoping to use savings from the Obamacare repeal bill for tax reform.

But still the vote was perilously close, and Republicans who voted "no" said they worry about the bill. Rep. Michael Turner, R-Ohio, said that he was concerned about the impact of the bill on the Medicaid expansion, a potential sticking point in the Senate too.

"There are 47,000 people in my community who are on Medicaid expansion alone, that would have negatively impacted them," he said.

Democrats predicted the vote would permanently hurt Republicans among voters who are relying on Obamacare for healthcare.

"You will glow in the dark on this one," said House Minority Leader Nancy Pelosi, D-Calif., repeating her warning from this week that voters would not forget.

Rep. Jan Schakowsky, D-Ill., added that Republicans on Thursday they "politically signed a death notice for themselves."

The bill faces an uncertain fate in the Senate, where lawmakers are already eyeing changes to the controversial bill. But for now, the leading Republican plan repeals Obamacare's individual and employer mandates and rolls back the law's taxes.

It keeps in place the Medicaid expansion until 2020, but then gives states the option to adopt a block grant or per capita cap scheme for getting Medicaid funding. It also defunds Planned Parenthood, fulfilling a long-time GOP goal.

Just before the vote, the House easily passed a separate bill that ensures congressional lawmakers and their staffs aren't exempt from any changes to Obamacare.

The main vote was made possible by an amendment from Rep. Tom MacArthur, R-N.J., that would let states opt out of key insurance mandates, which the conservative House Freedom Caucus believes are the drivers of high premiums. The amendment was key to getting caucus members on board.

That idea initially repelled centrists worried about losing protections for people with pre-existing conditions. But enough of them came back when $8 billion was added to help states fund coverage for those people.

Now that the bill has gotten through the House, it faces an uncertain fate in the Senate, which has a much smaller margin of error.

Under reconciliation rules, the bill can pass in the Senate by a simple majority. But there are just 52 Republican senators, which means the GOP can only lose two votes.

If the Senate dramatically changes the bill, that could imperil support among key conservatives that got major concessions.

Rep. Mark Meadows, R-N.C., the caucus chairman, said that he has been in touch with numerous senators over the course of the bill's path through the House. He said he's optimistic so far.

"I have talked to over 14 different senators on the subject and we are very optimistic we will find some common ground," Meadows said. "Obviously, the upper chamber has its own personalities and own agendas."

Still, senators expect to make some changes to the bill to get to 51 votes. Sen. Orrin Hatch, R-Utah, said that passing the bill in its current form "would be difficult to do."

Another possible hurdle is whether the bill conforms to the rules for reconciliation, which allows a simple majority vote. To use reconciliation, a bill has to only focus on spending and budget levels and not add to the deficit. Some experts have questioned whether adding the opt-out amendment will pass the reconciliation test.

For all of these reasons, Sen. John Cornyn, R-Texas, the number two in Senate GOP leadership, said it could take weeks to pass the health bill through the Senate.

Al Weaver contributed to this report