The Instructure Community will enter a read-only state on November 22, 2025 as we prepare to migrate to our new Community platform in early December. Read our blog post for more info about this change.
Found this content helpful? Log in or sign up to leave a like!
We have been asked by instructors to have the student profile picture that is created by Campus Security for ID badges to be put on student Canvas profile pictures. The photos are part of a SQL database (the database is entitled "IDCARD") and are linked up to to our MyTCC Portal in a public site, MyClasses. Is anyone using a link or API all of that type from your ID photo database into Canvas? If so, what is that method and can we use it, too? Or is it something that one of our IT Dept. developers can look at and adapt to our network?
Thanks!
Deb Padden, eLearning Support Specialist
Tacoma Community College, Tacoma, WA
This one is a bit tricky. With the initial user creation, according to the API, there are no parameters for adding the avatar. However, you can add it when editing a user. Further, the required parameters will depend upon whether you want the avatar linked remotely (from where they're currently stored) or copied to the Canvas server. Given the concept, I would assume you want them linked remotely to ensure the most recent image is always displayed (this assumes that a new image for a user is named and stored in the same place as the old, overwriting it upon creation).
Here's a quick little function for editing a user profile to set the avatar to a remote image using a PHP script:
<?php
// Domain to your Canvas instance
$server = 'canvas.instructure.com';
// Add the access token for system-level AccountAdmin here
$auth = 'Authorization: Bearer <access token goes here>';
/* BEGIN AVATAR LINKING HERE */
// Retrieve this value however you must
$userID = 1;
// Example call of the function
$oldProfile = executeCURL('/users/' . $userID, 'GET');
$newProfile = linkAvatar($userID, 'https://s-media-cache-ak0.pinimg.com/736x/a4/84/4e/a4844e1a6040040af19dfeb1a92602ac.jpg');
$success = ($oldProfile[1] == $newProfile[1]) ? true : false;
// Insert Unsuccessful Executions Here
/* END AVATAR LINKING HERE */
/**
* Formats and executes cURL for avatar linking
*/
function linkAvatar($userID, $avatarURL) {
$data = json_encode(array('user' => array('avatar' => array('url' => $avatarURL))));
$apiURL = '/users/' . $userID;
return executeCURL($apiURL, 'PUT', $data);
}
/**
* Executes cURL requests
*/
function executeCURL($apiURL, $requestType, $data = NULL) {
global $server, $auth;
$apiURL = $server . $apiURL . (($requestType == 'GET') ? (((strpos($apiURL, '?') !== false) ? '&' : '?') . 'per_page=100') : '');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestType);
if($data !== NULL) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data), $auth));
$results = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($results, 0, $headerSize);
$results = json_decode(substr($results, $headerSize));
curl_close($ch);
return array($header, $results);
}
?>
This uses a generic XMLHttpRequests handling function, but your institution may have a different one. The returned data from the function is an array for the results header and body.
Thank you for this sample! I will work with our programmer and the Security office and try to pull the avatars into our test environment. I'll let you know how the test turns out.
Let me know if you need any help. All I ask is that you remember that what I posted is just an example and may not work 100% with your systems without modification.
Key thing about my example is that is doesn't accommodate settings multiple avatars at once. Though. it's easy enough to modify to use a multidimensional array instead of a single value.
Good luck! :smileygrin:
Hi Christopher,
I am a Developer at Tacoma Community College Working with Deb on this solution. Perhaps you can help me out with one of our hurdles. We are using PowerShell to interact with the and I am attempting to use the API calls to update the Avatar data but am running into a 422 error, which lead me to believe my requested POST is not in the proper format. We are calling the POST method with the following API call
Canvas_website/api/v1/users/id/user[avatar][avatar_url] = "string of the url"
we updated emails previously using a similar style of call but I suspect the Avatar might be slightly different in the type of input it can receive. How would we structure this API call to update the Avatar information? Thanks..
-Seth Hill
Welcome to the Canvas Community! ![]()
Back on topic, I ran into the same issue. At first, I'd grabbed the API address and moved on. After about 20 minutes of trying to figure out what I had coded wrong, I took a look at the example for editing a user via the API. According to the API, you need to use PUT, not POST.
In looking at the URL example you gave, after the id you have a "/" but it should be a "?". So it should look like this:
Canvas_website/api/v1/users/id?user[avatar][avatar_url] = "string of the url"
So, first things first, this is awesome. Love the way you built it and commented everything out. Just awesome. I do have a few questions though. I know this is a 2.5 year old thread and I was wondering if this still works haha. We are trying to implement it but are having just a little trouble getting it to work. We put in our Canvas address, get our token through settings, change our $userID to the one we want a picture on, and we changed the URL that has our picture we want loaded. We run it and...crickets. We are not getting anything to show. Is there a piece of the puzzle missing? Here is the code we are using to get it to work.
<?php
// Domain to your Canvas instance
$server = 'iecc.instructure.com';
// Add the access token for system-level AccountAdmin here
$auth = 'Authorization: Bearer 123412'; // we put the real token in here, this is a placeholder//
/* BEGIN AVATAR LINKING HERE */
// Retrieve this value however you must
$userID = 53;
// Example call of the function
$oldProfile = executeCURL('/users/' . $userID, 'GET');
$newProfile = linkAvatar($userID, 'iecc.edu'); //we have a url here, this is a placeholder.
$success = ($oldProfile[1] == $newProfile[1]) ? true : false;
// Insert Unsuccessful Executions Here
/* END AVATAR LINKING HERE */
/**
* Formats and executes cURL for avatar linking
*/
function linkAvatar($userID, $avatarURL) {
$data = json_encode(array('user' => array('avatar' => array('url' => $avatarURL))));
$apiURL = '/users/' . $userID;
return executeCURL($apiURL, 'PUT', $data);
}
/**
* Executes cURL requests
*/
function executeCURL($apiURL, $requestType, $data = NULL) {
global $server, $auth;
$apiURL = $server . $apiURL . (($requestType == 'GET') ? (((strpos($apiURL, '?') !== false) ? '&' : '?') . 'per_page=100') : '');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestType);
if($data !== NULL) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data), $auth));
$results = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($results, 0, $headerSize);
$results = json_decode(substr($results, $headerSize));
curl_close($ch);
return array($header, $results);
}
?>
We have successfully implemented a solution.
We are using a python script which saves the image file into the profile pictures folder for each user.
The high level view is we used the api call /api/v1/users/self/files
with the parameters:
Name
Size
content_type
parent_folder_path: 'profile pictures'
as_user_id
these are all in a single object that we called inform_parameters
post the data using the api_url and parameter object stated above
this sets up some needed data ahead of time to prep canvas for the upload and give us some needed information.
after we get the response back we query the response to get the command and data needed for the upload, we then use this to post a request to upload the picture.
After verifying the upload was successful we set the avatar image for the user to the token of the uploaded image.
we use api/v1/users/id/avatars to get the avatar object for the user and make sure it is the one we uploaded based on file name.
then set the user[avatar][token] = to the value of the token of the uploaded image
we have successfully done this in test and production for 2000+ accounts.
Thanks for you help everyone.
Ah, so you ultimately decided to copy the images instead of linking to them from your current source. Will you be implementing it as part of a CRON to ensure the images stay up-to-date? If not, are you doing a one-time bulk process followed by integration with your systems to have it update Canvas when it is updated/set?
Seth and his team were marvelous in working with the Canvas Administrators here at Tacoma Community College in creating this automated call to a home-grown database for our security ID cards into our Portal and Canvas. The instructors will be able to truly see and engage with their students by having profile pictures of all students, not just the ones who put a picture in. As Seth said before, thanks for your help everyone!
Hi Seth
I have looked at the scripts available on GITHub these all generate errors, would you be willing to share you solution or point me in the right direction as to why the scripts on this site don't work. I dont have programming experience and have so far spent many hours using google and YouTube to try to work through the errors being generated all to no success. We are a school of 1200 high school students
Thanks
Helmut
@helmut_h_wirth , I see that you have posted a similar question at Assign Profile Pictures . So that we can keep all questions and comments unified in a single thread, and to keep Community members from having to duplicate their efforts, I'm requesting that anyone who can help post their responses to the above-linked discussion question (rather than here), and I've also shared the newly-posted question with this group.
Community helpTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign inTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign in