Context. Cyanamide (NH 2 CN) and its tautomer carbodiimide (NHCHN) are believed to have been key precursors of purines and pyrimidines during abiogenesis on primitive Earth. The detection of guanine and cytosine in meteorites and comets provides evidence of their nonterrestrial formation. Although NH 2 CN has been found in several molecular clouds, NHCHN has only been detected in Sgr B2(N). Their possible molecular formation mechanisms in the gas phase and therefore their respective molecular precursors remain an open subject of investigation. Aims. The main objective of this paper is to determine which reactions can produce NH 2 CN and HNCNH in the amounts observed under the astrophysical conditions of Sgr B2(N). The determination of their most likely precursors could serve to provide new insights into possible routes to purine and pyrimidine synthesis, and by extension to nucleosides, under the astrophysical conditions of dense molecular clouds. Methods. Initially, we proposed 120 reaction mechanisms, 60 being dedicated to NH 2 CN formation and the remaining 60 to HNCNH. These mechanisms were constructed using 25 chemical species that were identified in outer space. We calculated the molecular energies of reactants and products at the CCSD(T)-F12/cc-pVTZ-F12 and MP2/aug-cc-pVDZ levels of theory, and defined the values of thermodynamic functions using the Maxwell-Boltzmann statistical quantum theory. Via an extensive literature review on the abundances of reactants and products in Sgr B2(N), in addition to a detailed kinetic study for a range of 20-300 K, we identify the most likely reaction mechanisms for both cyanamides of those proposed previously and presently.Results. From the 120 analyzed reactions, only nine for NH 2 CN and four for HNCNH could thermodynamically account for their synthesis in Sgr B2(N). The kinetic portion of our study shows that Ra60 (CH 3 NH 2 + •CN → NH 2 CN + •CH 3 ), with a modified Arrhenius expression of k T = 1.22 x 10 −9 ( T 300 ) −0.038 exp − ( −147.34 T ) cm 3 mol −1 s −1 , is the most efficient reaction at low temperatures (<60 K). Above 60 K, no reaction with known reagents in Sgr B2(N) is efficient enough. In this way, Ra37-2 (•HNCN + •NH 2 → NH 2 CN + 3 NH ) appears to be the most likely candidate, showing a modified Arrhenius constant of k T = 2.51 x 10 −11 ( T 300 ) −32.18 exp − ( −1.332 T ) cm 3 mol −1 s −1 . In the case of carbodiimide production, Rb18 (•H 2 NC + •NH 2 → HNCNH + •H) is the most efficient reaction, fitting a rate constant of k T = 4.70 x 10 −13 ( 300 T ) −3.24 exp − ( 36.28 T ) cm 3 mol −1 s −1 in Sgr B2(N). Conclusions. The detected gas-phase abundances of cyanamide (NH 2 CN) in Sgr B2(N) can be explained as: Ra60 (•CN + •CH 3 NH 2 ) from 20-60 K; Ra5: (•CN + •NH 2 ) from 60-120 K; and Ra37-2 (•HNCN + •NH 2 ) from 120-300 K. The carbodiimide (HNCNH) synthesis could proceed via Rb18 (•H 2 NC + •NH 2 ). Moreover, the presence of •HNCN and •H 2 NC in Sgr B2(N) are predicted here, making them viable candidates for future astronomical observations. The fores...