New Zealand's GCSB bill has been passed into law after Prime Minister John Key told parliament it was vital to the country's national security.
The legislation, arguably the most contentious since National came to power, gives the Government Communications Security Bureau authority to spy on New Zealanders on behalf of the police and the Security Intelligence Service.
It passed its third reading on Wednesday by 61 votes to 59 against fierce opposition from Labour, the Greens and NZ First.
Launching the third reading debate, Mr Key said that since he became prime minister four-and-a-half years ago he had been frequently briefed by the intelligence agencies on issues which deeply concerned him.
"If I could reveal them, they would cut dead some of those who oppose this bill," he said.
"I can't, all I can do is assure New Zealanders the GCSB is essential to our national security."
Mr Key again gave an assurance he would never allow "wholesale spying" on citizens and residents, saying there would be very tight warrant processes to protect individual privacy.
Labour leader David Shearer said New Zealand could have been a world leader by holding a comprehensive review of all its intelligence agencies and then drafting legislation to cover them.
"Instead of that the government simply decided to get across the line, a quick remedy for a political hangover," he said.
Green Party co-leader Russel Norman said the bill was a fundamental constraint on freedom.
"It reduces our freedom to live free from state surveillance, and what we have seen around this debate is the government trying to actively confuse people."
Government MPs followed Mr Key's lead, backing the bill and saying it fixed a flaw in legislation passed by the previous Labour government.
The GCSB spied on New Zealanders for decades on behalf of the other agencies under an ambiguous clause in its legislation which said it could - but there was another clause forbidding it to spy on citizens and residents.
National, United Future and ACT supported the bill.
Labour, the Greens, NZ First, the Maori Party, the Mana Party and independent Brendan Horan opposed it, but only two of the three Maori Party MPs voted on Wednesday night.