How to get when student last accessed course via API?

Jump to solution
Community Explorer


We are trying to create reports that include when a student last accessed their course. I know this can be done with the data portal by getting the latest request timestamp in the requests table but that is a few days behind - we need it in real time. I am not seeing a quick way to do this with the API. I only see going through every request the user has made between the start of the term and current day then loop through until I hit the course ID.

Is there a better way I'm not seeing or are these two ways my only options?

Thank you,


Labels (2)
1 Solution
Community Champion


GraphQL is where Canvas is pushing developers and have said they will switch everything at some point away from the long-standing REST API. They haven't given a definitive timeline that I've seen and other things that they've promised have been in the works for over five years, so people continue to use the REST API.

Their implementation of GraphQL is better for (1) getting just the information you want instead of all of the information and (2) combining multiple objects together as opposed to having to making multiple API calls. When it works, it is typically a lot faster than using the REST API.

One major problem with GraphQL, though, is the lack of filtering. Another is that not all of the information available in the REST API is available through GraphQL.

With the Enrollments API, you can List enrollments by course, section, or user. In that, you can filter by enrollment_term_id when you use the user query (not a course or section). There are a lot of other parameters you can specify with the list enrollments endpoint. The enrollments object includes a last_activity_at and total_activity_time. The total_activity_time is a pretty worthless number used in isolation (the values aren't reliable), but GraphQL doesn't give it at all.

In the case of the GraphQL query that @jerry_nguyen shared, there are only two options for filtering: courseId and currentOnly. If you specify currentOnly: true then you will get a list of the current enrollments. The default is false, which returns all of the courses. Note that currentOnly: true may not be the same as current semester depending on how you handle things. In my case, I get my courses for next semester and all of my enrollments in those non-academic courses like training courses. Still, that doesn't include all the courses I've been teaching since 2012, so it is better than having to fetch everything. If you're doing this for students and don't have them enrolled in the next term already, it may work for you.

REST API allows you to specify the term in the query. To get that same functionality with GraphQL, you would need to include the term information as part of the query and then filter it after it has downloaded. The term information is found under the course object, so you could add term { name }  inside the course { } block that Jerry gave. Personally, I would use _id or one of the SIS IDs for the term instead of the name, but it's up to you.

All of this is about finding the last dates of activity for a single student. If you want it for the entire institution, then you would need to do this for every student or for every course. There was a similar thread a week ago that dealt with that: Best Endpoint for when a user was "last active". In it, we tried to force GraphQL to do something that is easier with the REST API (plus you get the total activity time with the REST API).

View solution in original post