Metabolism is involved in the pathogenesis of hypersensitivity pneumonitis. To identify diagnostic feature biomarkers based on metabolism-related genes (MRGs) and determine the correlation between MRGs and M2 macrophages in patients with hypersensitivity pneumonitis (HP). We retrieved the gene expression matrix from the Gene Expression Omnibus database. The differentially expressed MRGs (DE-MRGs) between healthy control (HC) and patients with HP were identified using the “DESeq2” R package. The “clusterProfiler” R package was used to perform “Gene Ontology and Kyoto Encyclopedia of Genes and Genomes pathway enrichment analyses” on DE-MRGs. We used machine learning algorithms for screening diagnostic feature biomarkers for HP. The “receiver operating characteristic curve” was used to evaluate diagnostic feature biomarkers’ discriminating ability. Next, we used the “Cell-type Identification by Estimating Relative Subsets of RNA Transcripts” algorithm to determine the infiltration status of 22 types of immune cells in the HC and HP groups. Single-cell sequencing and qRT-PCR were used to validate the diagnostic feature biomarkers. Furthermore, the status of macrophage polarization in the peripheral blood of patients with HP was determined using flow cytometry. Finally, the correlation between the proportion of M2 macrophages in peripheral blood and the diagnostic biomarker expression profile in HP patients was determined using Spearman analysis. We identified a total of 311 DE-MRGs. Gene Ontology and Kyoto Encyclopedia of Genes and Genomes pathway enrichment analysis showed that DE-MRGs were primarily enriched in processes like steroid hormone biosynthesis, drug metabolism, retinol metabolism, etc. Finally, we identified NPR3, GPX3, and SULF1 as diagnostic feature biomarkers for HP using machine learning algorithms. The bioinformatic results were validated using the experimental results. The CIERSORT algorithm and flow cytometry showed a significant difference in the proportion of M2 macrophages in the HC and HP groups. The expression of SULF1 was positively correlated with the proportion of M2-type macrophages. In addition, a positive correlation was observed between SULF1 expression and M2 macrophage proportion. Finally, we identified NPR3, GPX3, and SULF1 as diagnostic feature biomarkers for HP. Further, a correlation between SULF1 and M2 macrophages was observed, providing a novel perspective for treating patients with HP and future studies.