A built-in electric field generated at the p-n heterojunction will enhance charge transfer at the interface, which brings a new strategy for improving electrochemical energy storage. Herein, we construct a novel p-n heterojunction in a three-dimensional (3D) urchin-like CoNi x S y /g-C 3 N 4 (3D–2D) junction microsphere based on a one-step solvothermal method. The forming built-in electric field at the heterointerface of p-type semiconductor (CoNi x S y ) and n-type semiconductor (g-C 3 N 4 ) ensure its high-efficiency charge transfer. Besides, the porous 3D urchin-like microsphere could facilitate the diffusion of electrolytes and enhance the stability and volumetric energy density. Benefiting from the synergistic advantages of the p-n heterojunction and the 3D urchin-like structure, the CoNi x S y /g-C 3 N 4 electrode displayed an ultrahigh battery-type specific capacity (1029 C g −1 ) in a three-electrode system. Furthermore, an asymmetric supercapacitor formed by positive electrode of CoNi x S y /g-C 3 N 4 and negative electrode of activated carbon (AC) obtains a high energy density of 71.9 Wh kg −1 and a retention of 72.2% after 5000 cycles. • 3D CoNi x S y /g-C 3 N 4 microsphere with a p-n heterojunction was synthesized by solvothermal method. • Built-in electric field at the heterointerface of CoNi x S y /g-C 3 N 4 ensure its high charge transfer. • The CoNi x S y /g-C 3 N 4 shows a significantly enhanced capacity of 1029 C g −1 at 1A g −1 . • Excellent energy density of 71.9 W·h kg −1 at 0. 23 kW kg −1 .