Electrochemical reduction of naturally abundant nitrogen (N2) under ambient conditions is a promising method for ammonia (NH3) synthesis, while the development of a highly active, stable and low-cost catalyst remains a challenge. Herein, the N2 reduction reaction of TM@g-BC3N4 in electrochemical nitrogen reduction has been systematically investigated using density functional theory (DFT) calculations and compared with that of TM@g-C3N4. It was found that TM atoms are more stably anchored to g-BC3N4 than to g-C3N4. The adsorption free energy of N2 molecules on Fe@g-BC3N4 has the greatest change compared with that on Fe@g-C3N4, decreasing by 1.08 eV. The spin charge density around the Fe atom in Fe@g-BC3N4 increases significantly compared with that in Fe@g-C3N4, and the total magnetic moment of the system increases by 3.26μB. The limiting potential (-0.57 V) of Fe@g-BC3N4 in nitrogen reduction is decreased by 0.06 V compared with that of Fe@g-C3N4 (-0.63 V), and the desorption free energy of ammonia molecules decreases from 1.72 eV to 0.46 eV. The Fe atom has higher catalytic activity, the ammonia molecule is easier for desorption, and nitrogen reduction performance is better. This provides an important reference for the application of g-C3N4-based single atom catalysts in the field of nitrogen reduction.