Canvas Developers,
I am an absolute beginner in the Canvas development world and would like to look at any resources that would introduce me to the Canvas API. Any suggestions out there?
Logan
A great place to start your API journey is here: https://community.canvaslms.com/t5/Canvas-Developers-Group/Canvas-APIs-Getting-started-the-practical-ins-and-outs-gotchas/ba-p/263685
Hi Logan,
The main page for API documentation is here. I suggest skimming through the "Basics" and "Oauth 2" sections. Then head on over to the Live API page and play with some of the available fields to see what the data looks like. You'll spend a lot of time in the /courses, /enrollments, and /users sections, so I'd look there first. If you are brand new to RESTful apis, O'Reilly book on the topic is a good crash course.
I'll add a recommendation for the Postman app as a great tool to use when experimenting with RESTful APIs:
Postman | Supercharge your API workflow
...and @garth has a great post on how to use Postman with the Canvas API: API Testing: Postman
--Colin
As a new API user myself, I found this site to be useful for converting the curl examples to Python.
http://curl.trillworks.com
You are able to choose between python, PHP and Node.js
In Canvas API docs, under 'Courses - Recently logged in students' you'll find this curl example listed.
<SPAN class="" style="color: #000000;">curl </SPAN><SPAN class="" style="color: #666600;">-</SPAN><SPAN class="" style="color: #000000;">H </SPAN><SPAN class="" style="color: #008800;">'Authorization: Bearer <token>'</SPAN><SPAN class="" style="color: #000000;"> \ https</SPAN><SPAN class="" style="color: #666600;">://<</SPAN><SPAN class="" style="color: #000000;">canvas</SPAN><SPAN class="" style="color: #666600;">>/</SPAN><SPAN class="" style="color: #000000;">api</SPAN><SPAN class="" style="color: #666600;">/</SPAN><SPAN class="" style="color: #000000;">v1</SPAN><SPAN class="" style="color: #666600;">/</SPAN><SPAN class="" style="color: #000000;">courses</SPAN><SPAN class="" style="color: #666600;">/<</SPAN><SPAN class="" style="color: #000000;">course_id</SPAN><SPAN class="" style="color: #666600;">>/</SPAN><SPAN class="" style="color: #000000;">recent_users</SPAN>
Pasting into the page above converts to this:
import requests
headers = { 'Authorization': 'Bearer ',}
requests.get('https:///%3Ccanvas%3E/api/v1/courses/%3Ccourse_id%3E/recent_users', headers=headers)
Hope this helps!
~ Dave
I have a number of examples of using the API via python programs at GitHub - gqmaguirejr/Canvas-tools: Some tools for use with the Canvas LMS. Additionally, there are examples of using the API via an external LTI tool (written in Ruby) at GitHub - gqmaguirejr/E-learning: E-learning project for using Canvas . There are series of examples at Course Modules: Chip sandbox .
Just as an addition, if you prefer a classic terminal experience the combination of curl and jq is pretty sweet, especially for GET (but not too bad for the others)
for GETs it's as easy as setting up an alias:
alias canvapi="curl -H \"Authorization: Bearer {secretlongtokenstring}\""<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
and then adding the api url and a quick pipe to jq, like this
canvapi https://uni.instructure.com/api/v1/courses/:course_id | jq <SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
which gets you e.g.
{
"id": 20512,
"name": " Dank Memes and Dark Dreams",
"account_id": 5,
"etc": "etc.",
...
}
all nice in your terminal.
I have postman and some other similar apps, but I end up just using bash terminal & curl/jq most of the time (at least until something needs to be automated and turned into a tool for users)
jq has rich set of abilities beyond just pretty printing -- you can transform the data you get from the api and bend it to your needs. For example, if you just needed the id and name in the command above (try playing around with this, it's fun):
canvapi <A href="https://uni.instructure.com/api/v1/courses/:course_id" target="test_blank" rel="nofollow noopener noreferrer">https://uni.instructure.com/api/v1/courses/:course_id</A> \<BR />| jq {id: .id, name: .name}<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
Of course all normal shell rules apply, so you can do some really tedious things quickly by combining the jq processing powers with shell scripting.
Here's an example I had lying around that can restore groups nn a production course from a beta course (assuming the instructor catches that they somehow broke their course groups before beta re-syncs (also, this specific issue hasn't come up for over a year, but I just tested it and it seems like it still works, so I guess that's good?
<SPAN class="comment token">#!/bin/bash</SPAN><BR /><SPAN class="comment token">#### invoke with script name and canvas course id of course that need to be fixed</SPAN><BR /><SPAN class="comment token">#### ./fixGroupOops.sh 20512</SPAN><BR /><SPAN class="comment token">#### this will make all :course_id groups on production mirror beta </SPAN><BR /><SPAN class="comment token">#### so be sure that is what you want</SPAN><BR /><BR /><SPAN class="comment token">## set variables (edit these as necessary)</SPAN><BR /><SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">[</SPAN> $<SPAN class="number token">1</SPAN> <SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN> then<BR /> crs_id<SPAN class="operator token">=</SPAN>$<SPAN class="number token">1</SPAN><BR /> token<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">{</SPAN>secretlongtokenstring<SPAN class="punctuation token">}</SPAN><BR /> beta_can<SPAN class="operator token">=</SPAN>uni<SPAN class="punctuation token">.</SPAN>beta<SPAN class="punctuation token">.</SPAN>instructure<SPAN class="punctuation token">.</SPAN>com<BR /> can<SPAN class="operator token">=</SPAN>uni<SPAN class="punctuation token">.</SPAN>instructure<SPAN class="punctuation token">.</SPAN>com<BR /> canvapi<SPAN class="operator token">=</SPAN><SPAN class="string token">"curl -sS -H \"Authorization: Bearer $token\""</SPAN><BR /> alias canvapi<SPAN class="operator token">=</SPAN><SPAN class="string token">"curl -sS -H \"Authorization: Bearer $token\""</SPAN><BR /> curl_b_group_ids<SPAN class="operator token">=</SPAN><SPAN class="string token">"$canvapi -X GET \"https://$beta_can/api/v1/courses/$crs_id/groups\""</SPAN><BR /> <BR /> echo <SPAN class="string token">"restoring groups from $beta_can/courses/$crs_id to $can/courses/$crs_id"</SPAN><BR /> <BR /> <SPAN class="comment token">#mk a dir for the crs_id </SPAN><BR /> mkdir $crs_id<BR /> <BR /> <SPAN class="comment token">## get groups for course</SPAN><BR /> eval $curl_b_group_ids <SPAN class="operator token">|</SPAN> jq <SPAN class="string token">'.[] | .id'</SPAN> <SPAN class="operator token">|</SPAN> <SPAN class="keyword token">while</SPAN> read group_id<SPAN class="punctuation token">;</SPAN> do \<BR /> echo <SPAN class="string token">"getting group $group_id from $beta_can"</SPAN><SPAN class="punctuation token">;</SPAN> \<BR /> curl_group_id<SPAN class="operator token">=</SPAN><SPAN class="string token">"$canvapi -X GET \"https://$beta_can/api/v1/groups/$group_id/users\" -F \"per_page=1000\""</SPAN><SPAN class="punctuation token">;</SPAN> \<BR /> eval $curl_group_id <SPAN class="operator token">|</SPAN> jq <SPAN class="string token">'.'</SPAN> <SPAN class="operator token">></SPAN> $crs_id<SPAN class="operator token">/</SPAN>$group_id<SPAN class="punctuation token">;</SPAN> done<BR />ls $crs_id <SPAN class="operator token">|</SPAN> <SPAN class="keyword token">while</SPAN> read group_id<SPAN class="punctuation token">;</SPAN> do cat $crs_id<SPAN class="operator token">/</SPAN>$group_id <SPAN class="operator token">|</SPAN> jq <SPAN class="operator token">-</SPAN>r <SPAN class="operator token">-</SPAN>c <SPAN class="string token">'.[] | .sis_user_id'</SPAN><SPAN class="operator token">|</SPAN> \<BR /> <SPAN class="keyword token">while</SPAN> read <SPAN class="operator token">-</SPAN>r user_name<SPAN class="punctuation token">;</SPAN> do \<BR /> fix_oops<SPAN class="operator token">=</SPAN><SPAN class="string token">"$canvapi -X POST \"https://$can/api/v1/groups/$group_id/memberships\" -F \"user_id=sis_user_id:$user_name\""</SPAN><SPAN class="punctuation token">;</SPAN>\<BR /> echo <SPAN class="string token">"restoring $user_name to $can/courses/$crs_id/groups/$group_id"</SPAN><BR /> eval $fix_oops<SPAN class="operator token">|</SPAN> jq <SPAN class="string token">'.'</SPAN> <SPAN class="operator token">>></SPAN> $crs_id<SPAN class="operator token">/</SPAN>fix_log<SPAN class="punctuation token">.</SPAN>txt<SPAN class="punctuation token">;</SPAN> done<SPAN class="punctuation token">;</SPAN>done<BR /> exit <SPAN class="number token">0</SPAN><BR />fi<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
It saves everything in a sub folder so you can look over it and make sure (after the fact) that it worked. Not really sure how often this specific one comes up (I think we only ever had to do it for one or two faculty members, and they eventually figured out how to not have this problem), but it's a good example of 22 lines of code saving hours of tedium (the course that caused this to be written had many groups involving hundreds of students - a nightmare to check in the groups ui) -- note the -F \"per_page=1000\" might no longer work in which case moving to an api tool might be prudent...
I have a developer key in our Test instance: How would I use this in a local program to make a REST API call? Looking at https://developerdocs.instructure.com/services/canvas/oauth2/file.developer_keys: Developer keys are OAuth2 client ID and secret pairs stored in Canvas that allow third-party applications to request…
I'm building a tool for my institution that imports grades from another LTI tool into Canvas so instructors can automate late assignment policies not otherwise supported by Canvas. I'm trying to use the LTIv1.3 ID which is readily available on the LTI-side as a link to the student record on Canvas, but I can't find the ID…
How can one track Canvas Media use without having the reports available from Canvas Studio? Checking API endpoints and the Data 2 schema, there do not seem to be dedicated Canvas Media endpoints. Am I missing some? A method I've considered is using the body field of the wiki_pages table, as this contains the html code of…
Hello, I'm trying to utilize an API call to pull completed certificates and was hoping to download and store the certs as a backup. I can successfully run the API call but it will not allow me to view the certificate unless I access the URL while signed into an account where it's "my" cert. Is there a permissions setting…
UI for weekly progression idea that rest in the Schedule tab of the **K-12 UI** of Canvas it's incomplete and I really would not take it and push it into instance but I am going to post it here because in the past people like @James && @robotcars || @Steve_25 have had terrific input in the past... maybe w/their eyes and…