Carboxylic acids, upon treatment with diazomethane, undergo alkylation to yield methyl esters. Diazomethane, a yellow-colored gas generated from precursors such as N-methyl-N-nitrosourea and a base, is a resonance hybrid of three dipolar structures. Mechanistically, diazomethane is protonated by carboxylic acid to generate a carboxylate anion and a methyldiazonium cation. The methyldiazonium cation is an extremely unstable species bearing dinitrogen–an excellent leaving group. Next, the nucleophilic carboxylate anion attacks the methyldiazonium cation via an SN2 mechanism to yield methyl esters with the simultaneous release of nitrogen gas. Notice that the esterification proceeds via the nucleophilic carboxylate oxygen of the carboxylic acid. In contrast, acid-catalyzed Fischer esterification involves an electrophilic carbonyl carbon that eventually reacts with the nucleophilic alkoxy group of alcohol. One of the applications of alkylation by diazomethane is to detect cocaine in a urine sample. Here, benzoylecgonine –a urinary metabolite of cocaine, is converted to a more suitable methyl ester to be detected by mass spectrometry analysis.