Covalent Organic Frameworks (COFs) have emerged as promising platforms for photocatalytic synthesis of hydrogen peroxide (H2O2) due to their tunable chemical compositions and efficient catalytic functionalities. Inspired by the role of the microenvironment in enzyme catalysis, this study introduces various N-heterocyclic species into β-ketoenamine COFs (Nx-COFs, where Nx represents the number of nitrogen atoms in the N-heterocycle) to regulate the microenvironment around catalytic sites on acceptor-donor-acceptor (A-D-A) COFs foroverall H2O2 photosynthesis in pure water. The Nx-COFs exhibit distinct H2O2 photosynthetic rates following the number of nitrogen atoms sequence of N3-COF > N2-COF > N1-COF > N0-COF, with N3-COF with triazine structure showing the highest H2O2 generation rate (4881 µmol h-1 g-1) and the decent solar-to-chemical conversion (SCC) efficiency (0.413%), surpassing many existing COF-based catalysts. In situ characterization and theoretical calculations support the experimental results, revealing that N-heterocyclic species promote the photosynthesis of H2O2 through both an indirect stepwise single-electron oxygen reduction reaction (1e- ORR) mechanism and a direct two-electron water oxidation (2e- WOR) pathway. This study advances the design paradigm of photocatalysts by modulating the microenvironment within A-D-A COFs, paving the way for the development of more efficient and robust photocatalytic systems for the overall photosynthesis of H2O2.