BackgroundAnkylosing spondylitis (AS) is a chronic inflammatory autoimmune disease, and the diagnosis and treatment of AS have been limited because its pathogenesis is still unclear. Pyroptosis is a proinflammatory type of cell death that plays an important role in the immune system. However, the relationship between pyroptosis genes and AS has never been elucidated.MethodsGSE73754, GSE25101, and GSE221786 datasets were collected from the Gene Expression Omnibus (GEO) database. Differentially expressed pyroptosis-related genes (DE-PRGs) were identified by R software. Machine learning and PPI networks were used to screen key genes to construct a diagnostic model of AS. AS patients were clustered into different pyroptosis subtypes according to DE-PRGs using consensus cluster analysis and validated using principal component analysis (PCA). WGCNA was used for screening hub gene modules between two subtypes. Gene Ontology (GO) terms and Kyoto Encyclopedia of Genes and Genomes (KEGG) pathways were used for enrichment analysis to elucidate underlying mechanisms. The ESTIMATE and CIBERSORT algorithms were used to reveal immune signatures. The connectivity map (CMAP) database was used to predict potential drugs for the treatment of AS. Molecular docking was used to calculate the binding affinity between potential drugs and the hub gene.ResultsSixteen DE-PRGs were detected in AS compared to healthy controls, and some of these genes showed a significant correlation with immune cells such as neutrophils, CD8 + T cells, and resting NK cells. Enrichment analysis showed that DE-PRGs were mainly related to pyroptosis, IL-1β, and TNF signaling pathways. The key genes (TNF, NLRC4, and GZMB) screened by machine learning and the protein–protein interaction (PPI) network were used to establish the diagnostic model of AS. ROC analysis showed that the diagnostic model had good diagnostic properties in GSE73754 (AUC: 0.881), GSE25101 (AUC: 0.797), and GSE221786 (AUC: 0.713). Using 16 DE-PRGs, AS patients were divided into C1 and C2 subtypes, and these two subtypes showed significant differences in immune infiltration. A key gene module was identified from the two subtypes using WGCNA, and enrichment analysis suggested that the module was mainly related to immune function. Three potential drugs, including ascorbic acid, RO 90–7501, and celastrol, were selected based on CMAP analysis. Cytoscape showed GZMB as the highest-scoring hub gene. Finally, molecular docking results showed that GZMB and ascorbic acid formed three hydrogen bonds, including ARG-41, LYS-40, and HIS-57 (affinity: -5.3 kcal/mol). GZMB and RO-90–7501 formed one hydrogen bond, including CYS-136 (affinity: -8.8 kcal/mol). GZMB and celastrol formed three hydrogen bonds, including TYR-94, HIS-57, and LYS-40 (affinity: -9.4 kcal/mol).ConclusionsOur research systematically analyzed the relationship between pyroptosis and AS. Pyroptosis may play an essential role in the immune microenvironment of AS. Our findings will contribute to a further understanding of the pathogenesis of AS.Supplementary InformationThe online version contains supplementary material available at 10.1186/s12891-023-06664-8.