American troops with Afghan support have fought off an attack on the United States consulate in the western Afghan city of Herat. International forces in Afghanistan say the consulate is now safe.
The Taliban told the BBC a suicide bomber detonated explosives outside the building before dawn on Friday. Other fighters then opened fire on the consulate.
Two Afghan policemen and a consulate guard were killed in the fighting, and 17 civilians were injured, including women and children.
An Afghan army spokesperson told the BBC the initial explosion damaged the consulate's outer defences, allowing the attackers to breach the perimeter and shoot at the consulate buildings.
"A truck carrying attackers drove to the front gate, and attackers, possibly firing rocket propelled grenades and assault rifles, commenced attacking Afghan Protective forces on the exterior of the gates," US State Department spokesperson Marie Harf told reporters.
"Shortly after, the entire truck exploded, extensively damaging the front gate." The gunfire continued for some time, reporters at the scene said.
The Nato-led International Security Assistance Force tweeted at 0400 GMT that the consulate was secure "with all enemy forces being killed". The Taliban has claimed responsibility.
The attack is the latest in a series ahead of the withdrawal of foreign combat troops from Afghanistan in 2014.
The BBC's correspondent in Kabul says the attack - carried out in the shadow of the anniversary of the 9/11 attacks on the US - is a demonstration of the ability of insurgents still to disrupt Afghanistan 12 years on.