The United States House of Representatives has passed a bill to renew the National Security Agency's warrantless internet surveillance programme, overridng objections from privacy advocates.
Congress passed the legislation with 256 votes for and 164 against, split along party lines.
It is the culmination of a years-long debate on the proper scope of US intelligence collection - one fuelled by the 2013 disclosures of classified surveillance secrets by former NSA contractor Edward Snowden.
Senior Democrats had urged cancellation of the vote after Trump appeared to cast doubt on the merits of the program via Twitter, but Republicans forged ahead.
Trump initially said on Twitter that the surveillance programme, first created in secret after the September 11, 2001, attacks and later legally authorised by Section 702 of the Foreign Intelligence Surveillance Act, had been used against him but later said it was needed.
Before the vote a tweet from Trump had contradicted the official White House position and renewed unsubstantiated allegations that the previous administration of Barack Obama improperly surveilled his campaign during the 2016 election.
"This is the Act that may have been used, with the help of the discredited and phony Dossier, to so badly surveil and abuse the Trump campaign by the previous administration and others?" the president said in a tweet.
Some conservative, libertarian-leaning Republicans and liberal Democrats attempted to persuade colleagues to include more privacy protections.
However, they failed to pass an amendment to include a warrant requirement before the NSA or other intelligence agencies could scrutinise communications belonging to Americans whose data is incidentally collected.
Thursday's vote was a major blow to privacy and civil liberties advocates, who just two years ago celebrated passage of a law effectively ending the NSA's bulk collection of US call records, another top-secret program exposed by Snowden.
The bill as passed by the House would extend the NSA's spying program for six years with minimal changes.
Privacy advocates said it would actually expand the NSA's surveillance powers.
Most lawmakers expect it to become law, although it still would require Senate approval and Trump's signature.
Republican Senator Rand Paul and Democratic Senator Ron Wyden immediately vowed to filibuster the measure but it was unclear whether they could convince enough colleagues to force changes.
The White House, US intelligence agencies and Republican leaders in Congress have said they consider the surveillance program indispensable and in need of little or no revision.
"We need it!": Trump
Trump posted a follow-up tweet less than two hours later after his original one, after speaking on the phone with House Republican leader Paul Ryan.
"With that being said, I have personally directed the fix to the unmasking process since taking office and today’s vote is about foreign surveillance of foreign bad guys on foreign land. We need it! Get smart!" Trump tweeted.
Unmasking refers to the largely separate issue of how Americans' names kept secret in intelligence reports can be revealed.
After the vote Thursday, Ryan, asked about his conversation with the president, said Trump's concerns regarded other parts of the law.
"It's well known that he has concerns about the domestic FISA law. That's not what we're doing today. Today was 702, which is a different part of that law ... He knows that and he, I think, put out something that clarifies that," Ryan told reporters.
Asked by Reuters at a conference in New York about Trump's tweets, Rob Joyce, the top White House cyber official, said there was no confusion within Oval Office about the value of the surveillance programme and that there have been no cases of it being used improperly for political purposes.
Without congressional action, legal support for Section 702 will expire next week, although intelligence officials say it could continue through April.
Section 702 allows the NSA to eavesdrop on vast amounts of digital communications from foreigners living outside the United States through US companies such as Facebook, Verizon and Google.
The spying program also incidentally scoops up communications of Americans if they communicate with a foreign target living overseas, and can search those messages without a warrant.