Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Developer Tools for the Canvas User > Tutorial #2
Before getting started with APIs, read through paragraphs 1-5 of the linked thread by @stuart_ryan
Canvas APIs: Getting started, the practical ins and outs, gotchas, tips, and tricks
Stuart confirmed there is a working beta environment on the Canvas Free For Teachers accounts at https://canvas.beta.instructure.com
To save on lengthy screenshots, I've combined some panels, please let me know if this causes confusion.
// this is a simple JSON object, with the hex value of the color you chose
// you'll usually see JSON like you see in the console, with all the white space removed
{hexcode: "#F0592B"}
// but it's often useful to nest it with tabs while you're working and learning
{
hexcode: "#F0592B" // this is the data that was sent to Canvas to save the color
}
// it has one (1) value, simple right?
URL - API endpoint | /api/v1/users/:id/colors/:asset_string |
Method | PUT - replaces all current representations of the target resource with the request payload. |
ID - your user id | self - use self when you can't remember your user_id [1] |
Asset String | course_# - get the course number from the course URL |
Parameters | {hexcode: "#F0592B"} // the payload you will PUT to the URL |
var payload = { hexcode: '#FDC010' } // yellow
// http://api.jquery.com/jquery.ajax/
$.ajax({
url : '/api/v1/users/self/colors/course_1214595',
type: 'PUT',
data: payload
})
// #8BC34A - a green hex code
{readyState: 1, setRequestHeader: ƒ, getAllResponseHeaders: ƒ, getResponseHeader: ƒ, overrideMimeType: ƒ, …}
Congratulations!
You've just used javascript, jquery, ajax, and the Canvas api to Update custom color - Users - Canvas LMS REST API Documentation
Review the documentation anytime you want to interact with Canvas programmatically. The documentation is a great starting point to any project, then planning how you will use it in your script or program.
:smileycool: Escalating
// you see a string like this
{"course_id":1214595,"name":"W○RKR∞M⸚CARROLL","nickname":"My W○RKR∞M ⸚"}
// we can indent and format, making it easier to read
{
"course_id":1214595, // the id of the course we want to rename
"name":"W○RKR∞M⸚CARROLL", // the current nickname of the course
"nickname":"My W○RKR∞M ⸚" // what we are changing the nickname to
}
Three values were returned in response to setting the Course Nickname using the Dashboard Card. However, we only need to send 1 value to change the nickname.
Take a look at Set course nickname - Users - Canvas LMS REST API Documentation
Note the Endpoint and the Method, it looks like this
Let's update the snippet of code we used in the first section of Tutorial #2
We need to update the payload with the nickname, and change the URL
// update the payload with the values we need to set
// indent them to be easier to read
var payload = {
"nickname":"My W○RKR∞M ⸚ Test" // what we are changing the nickname to
}
// http://api.jquery.com/jquery.ajax/
$.ajax({
url : '/api/v1/users/self/course_nicknames/1214595',
type: 'PUT',
data: payload
})
[1] Profile - Users - Canvas LMS REST API Documentation
or in the browser console, try ENV.current_user_id
:smileydevil: Rapid Escalation
Check out Developer Tools #3 - Update the DOM with an AJAX Response and we'll update the Dashboard without Refreshing the page
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Constantly curious about Canvas, customization and Community collaboration.
To participate in the Instructure Community, you need to sign up or log in:
Sign In