API to get Canvas ID from Student ID

Jump to solution
chocochippie
Community Member

I’m currently developing a Python project for a university that processes a PDF or CSV containing student IDs and their extended exam times. The goal is to automate the process of adding extra time to specific exams. During development, I realized I need the unique Canvas ID for each user, which appears in the URL when I click on a student’s profile, rather than just the student ID (which shows in the sis id column).

I’m trying to find a way to convert the student ID into the Canvas ID. Here’s the code I’ve written so far:

 

 

def _extract_info_from_csv(file):
    self.df = pd.read_csv(file)

    user_time_dict = {}

    for sis_id in self.df['Student']:
        try:
            sis_id = int(sis_id)  # Convert to int
            print(f"Fetching user for SIS ID: {sis_id}")

            user = self.canvas.get_user(sis_id, "sis_user_id")  # Attempt to fetch the Canvas user ID
            print(f"User retrieved: {user}")  # Inspect the user object

            if user:
                canvas_id = user.id  # Adjust this based on your actual user object structure
                extension = self.df.loc[self.df['Student'] == sis_id, 'Extension'].values[0]
                user_time_dict[canvas_id] = extension
            else:
                print(f"No user found for SIS ID: {sis_id}")
        except Exception as e:
            print(f"Error fetching user for SIS ID {sis_id}: {e}")

    return user_time_dict

 

 

Unfortunately, the script isn't able to find the user based on the student ID, and am unable to convert the student ID to canvas unique ID. I don't know why. Any suggestions on how I might resolve this issue? 

0 Likes
1 Solution
jmerlenbach1
Community Explorer

You can utilize your SIS ID as part of the URI for your API call using the following syntax.

 

/api/v1/users/sis_user_id:########/

 

More info can be found here:https://canvas.instructure.com/doc/api/file.object_ids.html.

 

Hope that helps!

View solution in original post

0 Likes