Carbonaceous materials present the promising anode candidates for sodium-ion batteries (SIBs) and potassium-ion batteries (PIBs), however, the poor rate performance and low capacity due to limited interlayer spacing greatly impact their practical application. Herein, we report soybean roots-derived mesoporous hard carbon constructed by N, P co-doping (NPDCs) with enlarged interlayer distance and more active sites for high-performance SIBs and PIBs. As a result, the optimized NPDC achieves an excellent cyclic stability (197 mA g−1 after 2000 cycles at 2.0 A g−1) for SIBs, superior rate capability (a high reversible capacity of 179 mAh g−1 at 5.0 A g−1) for PIBs. Kinetic and first-principles calculation studies further demonstrate the dual-doping improves the Na (K) ions diffusion and adsorption as well as electrons transport. This work provides a facile and sustainable strategy to realize advanced carbon-based materials with heteratoms doping in application for energy storage devices.