Background Ankylosing spondylitis (AS) is a progressive rheumatic disease and studies reveal that the immune system is critical for the pathogenesis of AS. In the present study, various bioinformatics analysis methods were comprehensively applied, designed to identify potential key genes and inflammation states of AS. Methods The transcriptome profiles of GSE25101 and GSE73754 obtained from the Gene Expression Omnibus (GEO) database were merged for subsequent analyses. The differentially expressed genes (DEGs) were identified using the Bioconductor package Limma and threshold values. Functional enrichment and pathway enrichment analyses were performed using the clusterProfiler package and Gene Set Enrichment Analysis (GSEA). Next, protein-protein interaction (PPI) network of the identified DEGs was constructed by the online database, the Search Tool for the Retrieval of Interacting Genes (STRING), visualization and analysis were performed through Cytoscape software. Subsequently, we applied CIBERSORT algorithm to identify subpopulation proportions of immune cells in peripheral blood samples. Finally, we validated the hub genes with the GSE18781 dataset. Samples were collected from patients to validate gene and protein expression using qRT-PCR and ELISA. Results A total of 334 DEGs were identified, including 182 upregulated and 152 downregulated DEGs, between AS patients and normal human controls, which were primarily involved in immune response, autophagy, and natural killer cell-mediated cytotoxicity. The most prominent module and candidate biomarkers were identified from the PPI network. Biomarkers were selected for validation and their expressions were significantly decreased in peripheral blood samples which was consistent with transcriptome sequencing results. Nine genes with AUC 0.70 were considered to be AS hub genes for ROC curve analysis, including GZMA, GZMK, PRF1, GNLY, NKG7, KLRB1, KLRD1, IL2RB and CD247. Furthermore, CIBERSORT results suggest that AS contained a higher proportion of CD8+ T cells, naive CD4+ T cells, neutrophils, and lower levels of gamma delta T cells compared with the normal controls. Conclusion In this study, we identified DEGs combined with their closely related biological functions and propose that granule-associated proteins and immune infiltration maybe involved in the progression of ankylosing spondylitis. These validated hub genes may provide new perspectives for understanding the molecular mechanisms of ankylosing spondylitis.