Code and application programming interface (API) recommendation systems are important guarantees for efficient and accurate code reuse to improve the efficiency of software development. Context data plays a key role in code and API recommendation. A large amount of programming onsite data has been generated, but existing code and API recommendation systems rarely consider the context based on programming onsite data, which leads to low efficiency and poor accuracy of code and API recommendation. In this paper, we proposed a context model for code and API recommendation systems. Our context model is based on programming onsite data collected during programming. It includes four aspects: developer, project, time, and environment. Developer data is labeled data abstracted from information according to developers' programming habits and abilities, project data is information about the project, time data is information about temporal aspects of developers interacting with the project, and environment data is all environment elements used by developers during programming. Then, we collected programming onsite data in three ways: explicit collection, implicit collection, and reasoning. Lastly, we built the context model using a coarse-grained abstract model for recommendation. Our context model retains the key information in the code while eliminating redundant information that may affect the accuracy of the recommend task, and it can theoretically improve the efficiency and accuracy of recommendation.