ObjectiveThe aim of this study was to search for key genes in ankylosing spondylitis (AS) through comprehensive bioinformatics analysis, thus providing some theoretical support for future diagnosis and treatment of AS and further research.MethodsGene expression profiles were collected from Gene Expression Omnibus (GEO,http://www.ncbi.nlm.nih.gov/geo/) by searching for the term "ankylosing spondylitis". Ultimately, two microarray datasets (GSE73754 and GSE11886) were downloaded from the GEO database. A bioinformatic approach was used to screen differentially expressed genes and perform functional enrichment analysis to obtain biological functions and signalling pathways associated with the disease. Weighted correlation network analysis (WGCNA) was used to further obtain key genes. Immune infiltration analysis was performed using the CIBERSORT algorithm to conduct a correlation analysis of key genes with immune cells. The GWAS data of AS were analysed to identify the pathogenic regions of key genes in AS. Finally, potential therapeutic agents for AS were predicted using these key genes.ResultsA total of 7 potential biomarkers were identified: DYSF, BASP1, PYGL, SPI1, C5AR1, ANPEP and SORL1. ROC curves showed good prediction for each gene. T cell, CD4 naïve cell, and neutrophil levels were significantly higher in the disease group than in the paired normal group, and key gene expression was strongly correlated with immune cells. CMap results showed that the expression profiles of ibuprofen, forskolin, bongkrek-acid, and cimaterol showed the most significant negative correlation with the expression profiles of disease perturbations, suggesting that these drugs may play a role in AS treatment.ConclusionThe potential biomarkers of AS screened in this study are closely related to the level of immune cell infiltration and play an important role in the immune microenvironment. This may provide help in the clinical diagnosis and treatment of AS and provide new ideas for further research.Supplementary InformationThe online version contains supplementary material available at 10.1186/s12891-023-06550-3.