The first major legislative challenge to the National Security Agency's bulk collection of phone records from millions of Americans was defeated by only a narrow margin on Wednesday, sending a clear signal to the Obama administration that congressional anger about the extent of domestic surveillance is growing.

Despite a concerted lobbying effort by the White House and senior intelligence figures, the attempt to rein in the NSA failed by only 12 votes in the House of Representatives.

The final vote was 205 in favor and 217 against, exposing deep restiveness in Congress over the wisdom and constitutionality of the bulk surveillance on Americans less than two months after the Guardian exposed it, thanks to leaks from whistleblower Edward Snowden. A shift of seven votes would have changed the outcome.

Civil libertarians disappointed by the vote promised not to relent in opposing what they consider an unnecessary and unconstitutional violation of Americans' privacy.

The principal author of the effort, Michigan Republican Justin Amash, said he introduced his amendment to the annual Defense Department appropriations bill to "defend the fourth amendment, to defend the privacy of each and every American."

In opposition, the chairman of the House intelligence committee, Mike Rogers of Michigan, asked: "Have we forgotten what happened on September 11?" Swiping at Amash, who was supported by an online campaign, he asked: "Are we so small we can only look at how many Facebook likes we have?"

Congressman Mac Thornberry, a Texas Republican on the intelligence committee, called the abridgment of the NSA's power "foolhardy," saying it was an "overreaction that increases the danger" from terrorism.

There were some unlikely alliances: the Democratic minority leader, Nancy Pelosi, voted against the amendment with Michele Bachmann, a Tea Party Republican. John Boehner, the House speaker, found himself in the rare position of being on the same side as President Obama.

The measure, known as the Amash amendment, sought to end the NSA's years-long secret practice of collecting the phone records of millions of Americans unsuspected of any crime or foreign intelligence threat. Senator Ron Wyden, an Oregon Democrat, said the effort dangerously provided the NSA with a "human relationship database," something as or potentially more intrusive than the contents of Americans' phone calls.

Members of Congress of both parties opposed to the bulk NSA surveillance compared it to general warrants issued by the British colonists. The raucous and passionate debate exposed deep divisions in Congress over the propriety of the surveillance, contrary to assertions by the Obama administration and its allies that Congress had already granted its approval for the effort before it became public.

Campaigners said the narrow margin of defeat sent a clear signal about the strength of opposition to the measures. Michelle Richardson, the surveillance lobbyist at the American Civil Liberties Union, described the vote as a "sea change" in how Congress views bulk surveillance. "This is a great first step. It's the best vote we've ever had on the Patriot Act," said the ACLU's surveillance lobbyist.

Richardson cited a pledge made during the debate by Rogers, the intelligence committee chairman, to add privacy protections on the program into the forthcoming intelligence authorization bill. "I think he has to do that now," she said. "It also puts pressure on the administration to release more information on the program, because Congress isn't buying what they're selling."

David Segal, executive director of Demand Progress, which helped organize an online campaign to support the amendment, said: "Today's vote shows that the tide is turning, that the American people, when they are aware of these programs, overwhelmingly reject them, and the expiration date on these programs is coming due."

Leading members of the House intelligence committee welcomed the vote. Rogers, the chairman, and ranking Democrat Dutch Ruppersberger, said the amendment would have "eliminated a crucial counterterrorism tool" and rejected privacy concerns. In a joint statement, they said: "The charge that the program tramples on the privacy of citizens is simply wrong. This program balances our duty to protect the privacy of our fellow Americans with the equal duty to protect the nation."

They promised to take measures to address public confidence in the progrmas, but did not give details.

The Obama administration, the intelligence agencies and their allies in Congress had made an all-out push to quash the amendment after it unexpectedly made it past the House rules committee late on Monday. For four hours on Tuesday, General Keith Alexander, the director of the NSA, implored legislators that preventing his agency from collecting the phone records on millions of Americans would have dire consequences for national security.

The White House entered the fray on Tuesday night, taking the unusual step of publicly objecting to a proposed amendment to a bill. Hours before the House began consideration of the Amash amendment, the US director of national intelligence, James Clapper, warned legislators that "acting in haste to defund the Fisa business records program risks dismantling an important intelligence tool."

While most contentious House votes in recent years have been marked by partisanship, the Amash amendment crossed party lines. Obama was joined in opposing Amash by seven Republican committee chairmen in the House, the Wall Street Journal, the conservative thinktank the Heritage Foundation, and an array of Bush-era national security officials, some of whom helped design the phone-records collection program.

For his part, Amash, a Republican, was joined by a coalition of libertarian Republicans and progressive Democrats. His amendment's principal Democratic ally was longtime Michigan representative John Conyers, the ranking member of the House judiciary committee. Applause broke out from both sides of the party aisle for speakers for and against the Amash amendment.

Joe Barton, a Texas Republican, said it was "simply wrong" for the NSA, which he called well-intentioned, to "collect the data in the first place of every phone call of every American every day."

Meanwhile, an announcement of the Amash amendment, issued by the House Democratic leadership, described the NSA's bulk phone records collection program as harvesting data from people "not already subject to an investigation".

In opposition to the Amash amendment, Tom Cotton, an Arkansas Republican and Iraq war veteran, said, "Folks, we are at war. You might not like that truth. I wish we were not at war. But it is the truth."

Before the vote, Mike Pompeo, a Kansas Republican, offered what appeared to be alternative to Amash's amendment, albeit one that "clarified" NSA could collect no content from Americans. It succeeded by a wide margin, having 15 minutes for members to vote. Legislators had only two minutes to vote for the Amash amendment.