This study aimed to construct immune-related predictors to identify responders to anti-PD1 therapy of melanoma through CIBERSORT algorithm. Using the least absolute shrinkage and selection operator (LASSO) logistic regression, we constructed an immunoscore consisting of 8 immune subsets to predict the anti-PD1 response. This score achieved an overall accuracy of AUC = 0.77, 0.80 and 0.73 in the training cohort, validation cohort and on-anti-PD1 cohort, respectively. Patients with high immunoscores had significantly higher objective response rates (ORRs) than did those with low immunoscores (ORR: 53.8% vs 17.7%, P < 0.001 for entire pre-anti-PD1 cohort; 42.1% vs 15.1%, P = 0.022 for on-anti-PD1 cohort; 66.7% vs 16.7%, P = 0.038 for neoadjuvant anti-PD1 cohort). Prolonged survival trends were observed in high-immunoscore group (1-year PFS: 42.4% vs 14.3%, P = 0.059; 3-year OS: 41.5% vs 31.6%, P = 0.057). Furthermore, we found that high-immunoscore group exhibited higher fractions of tumor-infiltrating lymphocytes and an increased IFN-γ response. Analysis of the results of the GSEA indicated a significant enrichment of antitumor immunity pathways in the high-immunoscore group. Therefore, this study indicated that we constructed a robust immunoscore model to predict the anti-PD1 response of metastatic melanoma and the neoadjuvant anti-PD1 response of resectable melanoma.