TiN and NbN nanoparticles were synthesized from mesoporous graphitic C3N4 (mpg-C3N4) as a reactive template and used as the catalyst for ammonia synthesis. The obtained TiN and NbN nanoparticles possess high surface areas of 299 and 275 m2 g-1, respectively, making them attractive in the use of catalysis and support. Although most of the TiN and NbN particles show no measurable activity for ammonia formation, the nanoparticles enabled an ammonia synthesis rate of 31 μmol h-1 g-cat-1 at 673 K and 0.1 MPa of synthesis gas (N2 + 3H2) for both TiN and NbN catalysts. It is evident that the formation of nanoparticles with high nitride surface area is essential for the materials to function as catalysts in ammonia synthesis. The addition of Fe to TiN enhanced the ammonia synthesis activity, whereas it had detrimental effects on the catalytic activity of NbN. The properties of these catalysts in ammonia synthesis are discussed.