Use the SIS Term ID with API instead of the Canvas Term ID?

Jump to solution
MikeBrinkman
Community Participant

I was wondering if it is possible to use the SIS Term ID with API instead of the Canvas Term ID? I know in user API calls, I can use api/v1/users/sis_user_id: (which isn't documented), I'm hoping that there's a similar undocumented part in the API for using sis_term_id.

My current code:

 

def post_new_course(sci, n, sti):
    api_url = 'https://mycanvasurl:443/api/v1/accounts/1/courses'
    parameters = {
        'course[course_code]': sci,
        'course[sis_course_id]': sci,
        'course[name]': n,
        'course[term_id]': get_canvas_terms(sti)
    }
    result = requests.post(api_url,headers=header,data=parameters).json()

 

To get the term_id, I had to write this ridiculous bit of code:

 

def get_canvas_terms(sti):
    api_url = 'https://mycanvasurl:443/api/v1/accounts/1/terms'
    result = requests.get(api_url,headers=header).json()
    for (i, j) in result.items():
        items = j
        for item in items:
            terms = item
            for (k, v) in terms.items():
                #print (k, v)
                if v == sti:
                    term_id = terms.get('id')
                    print ("Term ID: " + str(term_id))
                    return term_id

 

Obviously this can be tightened up, but I'd like to have a more elegant solution if possible so I can skip this bit. Any suggestions are welcome!

0 Likes
1 Solution
JamesSekcienski
Community Contributor

Hello @MikeBrinkman 

If you make the following update, you should be able to eliminate the need for the second function to get the Canvas term id based on the SIS term id.  This worked for me when I tested it using the format for the sis_term_id

 

 

import json

def post_new_course(sci, n, sti):
    api_url = 'https://mycanvasurl:443/api/v1/accounts/1/courses'
    parameters = json.dumps({
        "course": {
            "course_code": sci,
            "sis_course_id": sci,
            "name": n,
            "term_id": f"sis_term_id:{sti}"
        }
    })
    result = requests.post(api_url,headers=header,data=parameters).json()

 

 

 

I'm not sure how you have your header defined, but with the above format, you will need to include the following in the header too since it is using JSON format for the payload (https://canvas.instructure.com/doc/api/index.html

 

 

'Content-Type': 'application/json'

 

 

 

View solution in original post