Electrocatalytic ammonia (NH3) synthesis under ambient temperature and pressure is an emerging sustainable method for dinitrogen (N2) fixation, providing a potential environmentally benign pathway for N2 fixation using renewable power of electricity. However, this strategy is subjected to the low activity of electrocatalysts. In this work, a Co–Nx–C hybrid derived from the metal–organic framework with built-in Co–Nx active sites was fabricated. A high NH3 yield (37.6 μg mg–1 h–1 at −0.9 V vs a reversible hydrogen electrode) and favorable faradaic efficiency (17.6% at −0.3 V vs a reversible hydrogen electrode) were achieved in a 0.05 M H2SO4 electrolyte. The dominating coordination environment of Co–Nx was finally determined by combining X-ray absorption fine structure spectroscopy and theoretical calculation. Co–N3 demonstrated pivotal active centers that facilitated N2 adsorption, lowered the free energy of the rate-determining step, inhibited hydrogen evolution reaction, and promoted the N2 reduction reaction (N2RR). The hierarchical pore structure of catalysts also promoted N2 adsorption, and the produced high pressure contributed to the further reaction of N2 fixation. This work also provides a new strategy for developing cost-effective electrocatalytic materials for N2RR.