Cellular functions are strongly dependent on surrounding cells and environmental factors. Current technologies are limited in their ability to characterize the spatial location and gene programs of cells in poorly structured and dynamic niches. We developed a method, NICHE-seq, that combines photoactivatable fluorescent reporters, two-photon microscopy, and single-cell RNA sequencing (scRNA-seq) to infer the cellular and molecular composition of niches. We applied NICHE-seq to examine the high-order assembly of immune cell networks. NICHE-seq is highly reproducible in spatial tissue reconstruction, enabling identification of rare niche-specific immune subpopulations and gene programs, including natural killer cells within infected B cell follicles and distinct myeloid states in the spleen and tumor. This study establishes NICHE-seq as a broadly applicable method for elucidating high-order spatial organization of cell types and their molecular pathways.