A search for charge-parity ($C!P$) violation in $D^0 to K^- K^+$ and $D^0 to pi^- pi^+$ decays is reported, using $pp$ collision data corresponding to an integrated luminosity of 6 $mathrm{fb}^{-1}$ collected at a center-of-mass energy of 13 TeV with the LHCb detector. The flavor of the charm meson is inferred from the charge of the pion in $D^*(2010)^+ to D^0 pi^+$ decays or from the charge of the muon in $overline{B} to D^0 mu^-bar{ u}_mu X$ decays. The difference between the $C!P$ asymmetries in $D^0 to K^- K^+$ and $D^0 to pi^- pi^+$ decays is measured to be $Delta A_{C!P} = [ -18.2 pm 3.2,(rm stat.) pm 0.9,(rm syst.) ] times 10^{-4}$ for $pi$-tagged and $Delta A_{C!P} = [ -9 pm 8,(rm stat.) pm 5,(rm syst.) ] times 10^{-4} $ for $mu$-tagged $D^0$ mesons. Combining these with previous LHCb results leads to $$Delta A_{C!P} = ( -15.4 pm 2.9) times 10^{-4},$$ where the uncertainty includes both statistical and systematic contributions. The measured value differs from zero by more than five standard deviations. This is the first observation of $C!P$ violation in the decay of charm hadrons.