A federal appeals court in the United States has ruled the National Security Agency's collection of data about millions of phone calls exceeded the scope authorised by Congress.
The secret programme was first disclosed by the former NSA contractor Edward Snowden, who has since fled to Russia.
In overturning a 2013 ruling, the judges did not halt the programme, but urged Congress to take action.
The NSA has collected data about numbers called and times, but not the content of conversations.
Among individuals targeted was German Chancellor Angela Merkel, and it has allegedly spied on European firms.
The latest verdict came after New York District Judge William Pauley had dismissed a legal challenge by the American Civil Liberties Union, which argued that the way the NSA tracked million of calls contravened the US constitution.
The 97-page ruling by the 2nd US Circuit Court of Appeals in New York said "a provision of the USA Patriot Act permitting the Federal Bureau of Investigation to collect business records deemed relevant to a counter-terrorism investigation cannot be legitimately interpreted to permit the systematic bulk collection of domestic calling records".
However, the appeals court stopped short of ruling on the constitutionality of the programme, launched after the 11 September 2001 attacks on the US.
The measures - repeatedly approved in secret by a national security court since 2006 - are set to expire on 1 June.
Leaders of the lower US House of Representatives would prefer to pass a bill to end the government's bulk collection of phone records and replace it with legislation that supporters say protects civil liberties. But Republican Senate Majority Leader Mitch McConnell has indicated he wants to extend the Patriot Act and retain the bulk collection programme.
The White House supports an alternative mechanism to preserve the programme's "essential capabilities without the government holding the bulk data", a spokesman for the White House National Security Council said.
The ACLU's deputy legal director Jameel Jaffer said the appeal court ruling should end any debate about whether the NSA's phone-records programme was lawful."