\begin{abstract} In this paper we state the following weighted Hardy type inequality for any functions φ in a weighted Sobolev space and for weight functions μ of a quite general type \begin{equation*} c_{N,\mu} \int_{\R^N}V\,\varphi^2\mu(x)dx\le \int_{\R^N}|\nabla \varphi|^2\mu(x)dx +C_\mu \int_{\R^N}W \varphi^2\mu(x)dx, \end{equation*} where V is a multipolar potential and W is a bounded function from above depending on μ. The method to get the result is based on the introduction of a suitable vector value function and on an integral identity that we state in the paper. We prove that the constant cN,μ in the estimate is optimal by building a suitable sequence of functions. \end{abstract}