A city police officer was taken a hospital Friday night after he accidently shot himself, the NYPD said.

It happened around 8:15 p.m. near Third Avenue and Baltic Street in Boerum Hill, according to police.

Police say the officer accidentally pulled the trigger of a gun and shot himself in the torso. 

Sources say the NYPD officer was working undercover during a drug buy-and-bust operation at the Wyckoff Garden Houses. 

The NYPD said the officer was transported to Brooklyn Methodist Hospital and was expected to be OK.