Magnesium alloys usually have poor corrosion resistance, which inhibits their use in the automotive and biomedical industries. Here, the authors use an environmental TEM to carbonate the natural corrosion products at the surface of magnesium alloys and form a compact and protective surface layer.