Abstract Magma volatile budgets and storage depths play a key role in controlling the eruptive styles of volcanoes. Volatile concentrations in the melt can be inferred from analyses of glass inclusions, which however may not be present in the investigated rocks or may have experienced post-entrapment processes that modify their volatile records. Apatite is becoming an alternative robust tool for unraveling the information of magmatic volatiles. Here we report a comprehensive dataset for the concentrations of volatiles and major elements in apatite crystals in the rocks from two eruptions with contrasting eruptive styles: the 2006 (dome-forming) and 2010 (explosive) eruptive events at Merapi volcano (Java, Indonesia). We obtained two-dimensional compositional distributions and in situ concentrations of H2O, CO2, F, Cl and S in 50 apatite crystals occurring at various textural positions. The CO2 concentrations we report are probably the first ones from natural volcanic apatite. Using the volatile concentrations in apatite and existing thermodynamic models and geothermobarometers, we have calculated the volatile abundances of the pre-eruptive melts of the two eruptions. We find that the apatite from the 2006 and 2010 deposits have a similar compositional range of volatiles, with a bimodal distribution of F–H2O–CO2 contents. The apatite included in amphibole has higher H2O (0·9–1·0 wt %) and CO2 (≥2400 ppm), but lower F (0·9–1·4 wt %), compared to crystals included in plagioclase, clinopyroxene, or in the groundmass (H2O: 0·4–0·7 wt %; CO2: 40–900 ppm; F: 1·7–2·3 wt %). Using these volatile concentrations and apatite-melt exchange coefficients we obtained two distinct ranges of H2O–CO2–S–F–Cl concentrations in the melt. Melts in equilibrium with apatite included in amphibole had 3–8 wt % H2O, ≥8000 ppm CO2, 340–2000 ppm S, whereas melts in equilibrium with apatite included in anhydrous minerals and in the groundmass had lower H2O (1·5–4 wt %), CO2 (60–2500 ppm), and S (10–130 ppm). We calculated the melt H2O–CO2 saturation pressures and found that they correspond to two main magma storage depths. The shallow reservoir with melts stored at ≤10 km below the crater agrees with the depths constrained by melt inclusions, as well as the geodetic, geophysical and seismic tomography studies from the literature. We have also found a significantly deeper melt storage zone at ≥25–30 km recorded by the C- and H2O-rich apatite in amphibole and barometry calculations using amphibole and high-Al clinopyroxene, which matches with the depths reported in seismic tomography studies. The high CO2/H2O and CO2/SO2 concentrations of the deep melt can help to explain the sharp increase in these ratios in fumarolic gas that were sampled just before the eruption in 2010. Supply of deep volatiles to the shallower magma column before the eruption in 2010 could have increased the magma buoyancy, and thus led to higher magma ascent rates and associated eruption explosivity. Evidence for the faster pre-eruptive magma ascent in 2010 than 2006 is also found on the diffusion distance of Cl in apatite microlites.