A US appeals court has ruled that the National Security Agency's massive collection of phone records of Americans is illegal, saying it exceeds the scope of what Congress authorised.

The ruling adds pressure on politicians to decide quickly whether to end or replace the programme.

The Second US Circuit Court of Appeals in Manhattan stopped short of declaring the programme unconstitutional.

The court said a lower court judge erred in dismissing a lawsuit by the American Civil Liberties Union that challenged the constitutionality of the surveillance on the ground it violated people's privacy.

At issue was the NSA's collection of "bulk telephony metadata", a programme the existence of which was first disclosed by former NSA contractor Edward Snowden.

In December 2013, US District Judge William Pauley dismissed the ACLU lawsuit, saying the NSA programme was a "counter-punch" by the US government to aid its efforts to fight terrorism.

Writing for a three-judge appeals court panel, however, Circuit Judge Gerard Lynch said Congress did not authorise the NSA programme under a section of the Patriot Act governing how investigators may collect information to fight terrorism.

"Such expansive development of government repositories of formerly private records would be an unprecedented contraction of the privacy expectations of all Americans," Judge Lynch wrote in a 97-page decision.

"Perhaps such a contraction is required by national security needs in the face of the dangers of contemporary domestic and international terrorism," he added.

"But we would expect such a momentous decision to be preceded by substantial debate, and expressed in unmistakable language. There is no evidence of such a debate."

Today's decision did not resolve the issue of whether the NSA programme violated the bar against warrantless searches under the Fourth Amendment to the US constitution.

The Second Circuit returned the case to Judge Pauley for further proceedings. 

It upheld his denial of a preliminary injunction to stop the collection of records.

The White House said it was evaluating the ruling.

"Without commenting on the ruling today, the President has been clear that he believes we should end the Section 215 bulk telephony metadata program as it currently exists by creating an alternative mechanism to preserve the program's essential capabilities without the government holding the bulk data," said Ned Price, a spokesman for the White House National Security Council.

"We continue to work closely with members of Congress from both parties to do just that and we have been encouraged by good progress on bipartisan, bicameral legislation that would implement these important reforms," Mr Price said in a statement.