Sodium-ion batteries (SIBs) and potassium-ion batteries (PIBs) are potential cost-effective electrochemical energy storage devices for future grid-scale energy storage. However, the limited capacities of carbonaceous anodes hamper their commercial development. Edge-nitrogen doping has been demonstrated as an effective strategy to enhance the reversible capacities of carbonaceous anodes. In this work, we demonstrate a general strategy to synthesize three-dimensional high edge-nitrogen doped turbostratic carbons (3D-ENTC) through catalytic pyrolysis of graphitic carbon nitride, which is enabled by metal cyanamides. The 3D-ENTC exhibits a three-dimensional carbon nanosheet framework with a high edge-nitrogen doping level of 18.9 at% and a total nitrogen doping level of 21.2 at%. Further, 3D-ENTC displays high capacities of 420 and 403 mA h g−1 at a current density of 50 mA g−1, high rate capabilities, and superior cycling stability when used as the anodes of PIBs and SIBs, respectively. The different charge storage mechanisms of 3D-ENTC anodes in PIBs and SIBs are elucidated by in situ electrochemical impedance spectroscopy. We find that 3D-ENTC stores Na+ ions mainly by adsorption, while 3D-ENTC stores K+ ions by adsorption and intercalation. This work opens a new avenue for designing high edge-nitrogen doped carbon anodes for SIBs and PIBs.