Hi @AtticusMcNulty,
Yes, this is something you can do. It's not exactly simple, but the good news is that you can work toward this goal in a step-by-step fashion. If you can get through this process, you will have learned a lot and have a nice project to show a professor or prospective employer demonstrating your knowledge of REST API.
Here's an outline of the steps you'd need to do.
- Become familiar with the Canvas API
- Create an access token so your program can talk to Canvas
- Test your token
- Get a list of your courses
- For each course, get a list of your assignments
- For each assignment, get a list of your submissions, which will include the grade you earned
Ready? Get some caffeine, because we'll need it!
Become Familiar with the Canvas API
Before diving into programming, I recommend becoming familiar with the Canvas API. Here's a simple way to get started.
- Launch Mozilla Firefox. (Other browsers might do this too, but I know Firefox makes these steps easy.)
- Log into Canvas and go to your Dashboard. Your address bar will probably say something like
https://myschool.instructure.com. This is the base URL of your Canvas instance. Copy this address to your clipboard.
- Open a new tab. Paste in the base URL, then append
/api/v1/users/self, making the address bar read something like https://myschool.instructure.com/api/v1/users/self. Hit Enter. You should see a JSON response with some basic information about your Canvas user account, formatted in pretty blue, pink, and green colors. If so, you've successfully made a call using your browser to the Canvas API. In this case, because you were logged into Canvas in one tab, your browser included this authorization when it made the API request in another tab, so Canvas was able to allow and respond to the request.
Now, we can start writing a program to do get the same information...
Create an Access Token
First, Canvas will need to see that your program is authorized to request information on your behalf. By far, the easiest way to do this is manually generate an access token.
- Log into Canvas, click Account, then click the Settings link on the pop-out panel.
- Under the Approved Integrations section, click the + New Access Token button.
- Enter a Purpose ("testing" is fine), enter an expiration date if desired, and click Generate Token.
- The system will show your new token. Record this value immediately, because once you leave this screen, you won't be able to retrieve the full token anymore, and you'd have to regenerate it to get a different value. This token is essentially equivalent to your Canvas username and password combined together in a single value, so safeguard it accordingly!! I recommend NOT hard-coding this information into your program Instead, learn about using environment variables with your language/platform of choice to store this information. These will let you save your top-secret credential in the user profile on your computer or perhaps in a separate file in the project folder on your hard drive. (This file is one you would NEVER share with anyone else, nor commit to a Git repository of your code, for example.)
Test Your Access Token
Second, you can then use your access token to connect to Canvas. Each programming language will have a way to make HTTP requests of different types, with GET, POST, PUT, UPDATE, and DELETE being the most common. You''ll use your language's request function to authenticate to Canvas with your access token and make a request. I recommend making a basic GET request to the same endpoint you used above, /api/v1/users/self, and printing to the screen the JSON data you get back. Once you're able to retrieve your user information, you can move on to something more advanced.
Get a List of Courses
First, from your Dashboard, go visit some of your courses, and note the URLs in your browser address bar. You'll see that each address ends in /courses/<some_number>. These numbers are course ID numbers and are unique to each course.
Now use the API endpoint /api/v1/users/self/courses to get a list of your courses. Try this first in the browser (adding it after your base URL, of course) to see what information you get back. Then modify your program to call this endpoint and produce a list of your courses. There are a few things you'll want to note here:
- Each course listed in the API response has an
id parameter that corresponds to the course ID in the address bar when you browse to the course.
- By default, the API will only return 10 courses, even if you're enrolled in more. This is the result of pagination, a process whereby Canvas breaks its response into a number of pages. What do you do if you are enrolled in more courses and want a list of all of them? When you get a Canvas response, it will include a header to the next page of results (if there is another page). So the way to handle this is to retrieve the first page, see if it has a next header link and, if so, follow that link to the second page. You repeat this process until Canvas gives you a response that doesn't have a next page, at which point you know you've gotten all the results.
- The API will return past, present, and future courses in its response. Presumably, you want to get your grades for current courses and don't care (much) about reporting assignment grades for past courses. Or do you? Figuring out whether and how to filter your reporting to current courses based on the API data is left as an exercise to the reader.
Get a List of Assignments from Each Course
Once you have a list of your course IDs, you can use these to get a list of assignments in each course using this endpoint: /api/v1/users/self/courses/<course_id>/assignments. These responses will include the following valuable information:
- The
id for each assignment.
- The
grading_type, which is typically 'points' but can also be 'pass_fail', 'percent', 'letter_grade', or 'gpa_scale'.
- The
points_possible maximum score for the assignment, if the grading_type is points.
Onward to the last step...
Get the Grade for Each Assignment Submission
Finally, you can get the grade for your submission to each assignment using api/v1/courses/<course_id>/assignments/<assignment_id>/submissions/self. These will include the actual grade for the assignment (if one has been assigned).
Conclusion
If you made it this far, you should be very proud of yourself. This is not a trivial project, especially when you're just starting out. Give these steps a try and feel free to ask more questions!
Steven Endres (he/him/his)
Director of Learning Technologies
Information Technology (Lewis 001A)
Dominican University | 7900 Division Street | River Forest, IL 60305
Mobile: (708) 466-9452 | sendres@dom.edu | Book a meeting with me
.................................
We are Dominican. We Go First.