Abstract Background Immune checkpoint inhibitors (ICIs) are widely used in cancer treatment; however, the emergence of ICI-associated myocarditis (ICI-MC) presents a severe and potentially fatal complication with poorly understood pathophysiological mechanisms. This study aimed to identify crucial immune-related genes in ICI-MC and uncover potential therapeutic targets using bioinformatics. Methods Using the GSE180045 dataset, which includes three groups—Group A: ICI patients without immune adverse events, Group B: ICI patients with non-myocarditis immune adverse events, and Group C: ICI patients with myocarditis—we analyzed differentially expressed genes (DEGs) between ICI-MC samples (Group C) and non-myocarditis controls (Groups A and B). These DEGs were then cross-referenced with 1796 immune-related genes from the immPort database to identify immune-related DEGs. We conducted functional enrichment analyses (Gene Ontology, Kyoto Encyclopedia of Genes and Genomes, gene set enrichment analysis), constructed a protein–protein interaction network, and identified hub genes. Validation using the GSE4172 dataset led to the identification of optimal feature genes from the overlap between hub genes and DEGs. Predictions of target MicroRNAs (miRNAs) were made, and a competing endogenous RNA (ceRNA) network was constructed. Target drugs for hub genes were predicted using the Connectivity Map database. Results We identified 58 DEGs between ICI-MC and controls, which led to the identification of 32 immune-related DEGs after intersection with 1796 immune-related genes. Functional analyses revealed enrichment in cell lysis, CD8+ T-cell receptor, natural killer cell–mediated cytotoxicity, and RAGE signaling. Notably upregulated hub genes included IL7R, PRF1, GNLY, CD3G, NKG7, GZMH, GZMB, KLRB1, KLRK1, and CD247. In the validation dataset, 407 DEGs were uncovered, resulting in the identification of 3 optimal feature genes (KLRB1, NKG7, GZMH). The predicted target miRNAs, lincRNAs, and circRNAs constituted a comprehensive ceRNA network. Among the top 10 drugs with elevated connectivity scores was acetohydroxamic acid, indicating a need for caution in ICI treatment. Conclusion KG7, GZMH, and KLRB1 were identified as pivotal immune-related genes in ICI-MC. Biological enrichments included pathways involved in cell lysis, the CD8+ T-cell receptor pathway, natural killer cell–mediated cytotoxicity, RAGE signaling, and proinflammatory responses. The ceRNA network illuminated the role of critical molecules and underscored the importance of avoiding drugs such as acetohydroxamic acid in ICI treatment. Key message What is already known on this topic Myocarditis is recognized as a serious ICI-associated toxicity, seemingly infrequent yet often fulminant and lethal. The underlying mechanisms of ICI-associated myocarditis remain not fully understood. Although the significance of T cells and cytotoxic T lymphocyte–associated protein 4 (CTLA-4) is evident, the inciting antigens, the reasons for their recognition, and the mechanisms causing cardiac cell injury are not well characterized. An improved understanding of ICI-associated myocarditis will provide insights into the equilibrium between the immune and cardiovascular systems. What this study adds Our study further validates the significance of T cells and CTLA-4 in ICI-associated myocarditis. More importantly, we identified three genes—NKG7, GZMH, and KLRB1—essential for the development of ICI-MC and proposed ceRNA networks involving these three key genes. How this study might affect research, practice or policy The newly discovered key genes and their intricate molecular interactions offer a comprehensive perspective on the mechanisms underlying ICI-MC. Furthermore, our findings advise caution regarding the use of drugs like acetohydroxamic acid during ICI treatment. As our understanding of these regulatory networks deepens, our study provides valuable insights that could inform future therapeutic strategies for ICI-MC.