Found this content helpful? Log in or sign up to leave a like!

Using the API to give information about "course migrations" (importing content into a course)

Jump to solution
NickRiches
Community Explorer

I am using the API to list course migrations for my institution. The idea is to see which recently created courses have not had any content migrated into them, and then automatically do the migration.

NB in the Canvas GUI, the procedure I am referring to is Settings > Import Module Content, but as far as I can tell, in the context of the API itself, this is called a "Migration" (the term also seems to be used for transferring content from another LMS, but I don't think that is the meaning in the context of the API).

See my code Python below.

For the particular account I am picking up hundreds of courses using the courses API call, but when I try to list content migrations, there are none. Given that content migrations happen every year, this does not seem right. I am wondering what's happening here.

Thanks

PS thanks to this post which helped me to sort out the pagination issue. It was very hard to wrap my head around. NB I needed to tweak the code to get it to work.

 



import requests
import json
import os
from dotenv import load_dotenv
from io import BytesIO
import pandas as pd

os.chdir("/path/to/folder")

#  GET BASE URL AND ACCESS TOKEN (NB client_id and secret are not needed)

load_dotenv(dotenv_path="/path/to/folder.env")
base_url = os.getenv('XXXX_CANVAS_BASE_URL')
access_token = os.getenv('XXXX_CANVAS_ACCESS_TOKEN') # 

headers = {
    'Authorization': f'Bearer {access_token}'
}

# CREATE FUNCTION REQUESTS_GET. IT HANDLES THE PAGINATINO

def requests_get(base_url, resource_url, headers, params):
    r = requests.get(base_url + resource_url, params=params, headers=headers)
    raw = r.json()
    data_set = []
    for x in raw: # identifies each entry
        data_set.append(x)
    while not 'last' in r.links:
        r = requests.get(r.links['next']['url'], headers=headers, params=params)
        raw = r.json()
        for x in raw:
            data_set.append(x)
    return(data_set)

resource_url = "/api/v1/accounts/1/courses" # API call
parameters = {"search_term" : "2025","per_page" : "100"}

courses = requests_get(base_url=base_url,
                      resource_url=resource_url,
                      params=parameters,
                      headers=headers)


resource_url = "/api/v1/accounts/1/content_migrations"
parameters = {"per_page" : "100"}

migrations = requests_get(base_url=base_url,
                      resource_url=resource_url,
                      params=parameters,
                      headers=headers)

 

Labels (1)
1 Solution
NickRiches
Community Explorer
Author

Ahhh. I think I've got it. There is a different resource URL: GET /api/v1/audit/course/accounts/:account_id which is described at https://developerdohttps://developerdocs.instructure.com/services/canvas/file.all_resources/course_a... . This seems to provide the information I want. It hangs as I probably need to give a very tight time window, otherwise it tries to access years worth of data!

View solution in original post