Militants have rammed at least one car packed with explosives into a wall surrounding the German consulate in the northern Afghan city of Mazar-i-Sharif, killing several civilians and wounding scores of others.
The Taliban has claimed responsibility for the attack, saying it was in retaliation for NATO air strikes against a village near the northern city of Kunduz last week in which more than 30 people were killed.
Witnesses reported sporadic gunfire from around the consulate and said the huge blast had shattered windows in a wide area around the compound.
A NATO spokesman said the explosion had caused "massive damage" to the building, where around 30 people normally worked and heavily armed gunmen had followed up the blast.
Sayed Kamal Sadat, police chief of Balkh province said several civilians had been killed and dozens wounded by flying glass from the explosion but consular staff were unharmed.
A German foreign ministry spokesman said the attack had been suppressed by Afghan and German security personnel as well as NATO special forces.
"All German employees of the Consulate General are safe and uninjured," a spokesman from the German foreign ministry said, adding that it was not yet known how many Afghan civilians and security personnel were killed or injured.
The attack highlighted the security problems spreading across Afghanistan in recent months, with heavy fighting in areas from the volatile southern province of Helmand to Kunduz in the far north.
Last week, more than 30 people, many of them children, died when US aircraft carried out air strikes in support of Afghan and US special forces who came under attack during a raid against suspected Taliban militants threatening the city.
The explosion occurred at around 2305 local time, a spokesman for the German military joint forces command in Potsdam said.
By the early hours of the morning, Afghan special forces were still conducting search operations but were not encountering any more resistance, the local police chief Sadat said. The area would be locked down until morning when the search would continue after daybreak, he said.
The NATO spokesman said at least one car packed with explosives had been rammed into the high outer wall surrounding the consulate, but authorities were investigating if a second car had been involved.
At least two people were killed and 87 were wounded in the attack - some critically - a journalist, Bilal Sarwary, said on his Twitter feed, quoting a local doctor.
An Afghan police official at the scene said a car bomb had been detonated at the gate of the compound, allowing a number of attackers to enter.
The heavily protected consulate is located in a large building close to the Blue Mosque in the centre of Mazar-i-Sharif, where earlier this year, the Indian consulate was also attacked by militant gunmen.
NATO forces have a large base on the edge of the city.
In Berlin, a crisis task force had been set up, and Foreign Minister Frank-Walter Steinmeier was receiving continual updates, the ministry spokesman said.
Germany, which is responsible for the NATO presence in northern Afghanistan, has about 850 soldiers at the base, with another 1,000 troops coming from 20 partner countries.