Realtime traffic flow forecasting is a core issue of intelligent traffic control and management. To make accurate traffic flow forecasting, it is necessary to take into account both the temporal and spatial characteristics of traffic flow in the flow forecasting methods. In this paper, we build a spatial temporal graph-based transformer model (STGT) for traffic flow forecasting. In the proposed model, we introduce the distance correction matrix, step correction matrix and attention mechanism to improve the traditional graph convolutional networks (GCN). The improved GCN is used to extract the spatial information. We further introduce the Transformer model to exploit the temporal information, so as to improve the accuracy of forecasting results. The proposed STG T model is tested using the real-world dataset from the Caltrans Performance Measurement System (PeMS). In the comparison experiment, the accuracy of STGT on MAE and RMSE reached 22.08 and 33.44 respectively, which is better than all selected baseline models.