The metabolism of abnormal bile acids (BAs) is implicated in the initiation and development of gastrointestinal (GI) cancer. However, there was a lack of research on the molecular mechanisms of BAs metabolism in GI. Genes involved in BAs metabolism were excavated from public databases of The Cancer Genome Atlas (TCGA) database, Gene Expression Omnibus (GEO) database, and Molecular Signatures Database (MSigDB). ConsensusClusterPlus was used to classify molecular subtypes for GI. To develop a RiskScore model for predicting GI prognosis, univariate Cox analysis was performed on the genes in protein-protein interaction (PPI) network, followed by using Lasso regression and stepwise regression to refine the model and to determine the key prognostic genes. Tumor immune microenvironment in GI patients from different risk groups was assessed using the ESTIMATE algorithm and enrichment analysis. Reverse transcription-quantitative real-time PCR (RT-qPCR), Transwell assay, and wound healing assay were carried out to validate the expression and functions of the model genes. This study defined three molecular subtypes (C1, C2, and C3). Specifically, C1 had the best prognosis, while C3 had the worst prognosis with high immune checkpoint gene expression levels and TIDE scores. We selected nine key genes (AXIN2, ATOH1, CHST13, PNMA2, GYG2, MAGEA3, SNCG, HEYL, and RASSF10) that significantly affected the prognosis of GI and used them to develop a RiskScore model accordingly. Combining the verification results from a nomogram, the prediction of the model was proven to be accurate. The high RiskScore group was significantly enriched in tumor and immune-related pathways. Compared with normal gastric mucosal epithelial cells, the mRNA levels of the nine genes were differential in the gastric cancer cells. Inhibition of PNMA2 suppressed migration and invasion of the cancer cells. We distinguished three GI molecular subtypes with different prognosis based on the genes related to BAs metabolism and developed a RiskScore model, contributing to the diagnosis and treatment of patients with GI.