Add People to Course via API

Jump to solution
ScottMo
Community Novice

Does anyone have an example of how to add a user to a course via the API? I can see how to do it via browser but im trying to automate enrollment into a class.

Labels (2)
0 Likes
1 Solution
stimme
Community Coach
Community Coach

@ScottMo We create enrollments through the API in certain cases. Using a script to create them via the API is almost as convenient as SIS Import CSVs, but the resulting enrollments can be managed by teachers. The Enrollments API also allows one to bypass the invitation step (I wish that Instructure would build this into the +People GUI workflow). Here is a template BASH script.

#!/bin/bash
# Enrollment template developed by Emory University Teaching & Learning Tech.
# This is a template to enroll a set of users in a set of courses or sections
# Fill in the variables (no space after equal sign), save as a new file, and run
# See comments before each variable for options

# Institution URL stem. For FFT (canvas.instructure.com), the stem is just "canvas".

url_stem=

# Canvas instance: prod, test, beta

canvas_instance=

# Limit access to target section: "false", "true"

section_limited_access=""

# Enrollment targets: course, section
# In general, it's best to target sections, particularly if your SIS Imports
# create sections. Targeting an enrollment at a course should be safe in a
# manually created course with only one section.

enrollment_target=

# Target ID types: sis, canvas

target_id_type=

# Supply course ID or section ID values separated by spaces or new lines

targets=(
)

# Enrollment types: Student, Teacher, Ta, Designer, Observer

enrollment_type=

# User ID types: login, sis, integration, canvas

user_id_type=

# Supply user ID values separated by spaces or new lines

users=(
)

###### Edits to template are not typically necessary below this line ######

# canvasAPItoken variable is set from file if it exists and is not empty
# file not needed if variable set in environment
if [ -s ~/.canvasAPItoken.txt ]; then canvasAPItoken=`cat ~/.canvasAPItoken.txt`; fi

# section_limited_access is checked
if [ $section_limited_access != "false" -a $section_limited_access != "true" ];
then echo "section_limited_access must be \"false\" or \"true\". Exiting"; exit 1 ;
fi

# canvas_domain is determined by canvas_instance and url_stem
if [ $canvas_instance == "prod" ]; then canvas_domain="$url_stem.instructure.com" ;
elif [ $canvas_instance == "test" ] ; then canvas_domain="$url_stem.test.instructure.com" ;
elif [ $canvas_instance == "beta" ] ; then canvas_domain="$url_stem.beta.instructure.com" ;
else echo "$canvas_instance is not a valid enrollment type. Exiting"; exit 1 ;
fi

# enrollment_target is checked
if [ $enrollment_target != "course" -a $enrollment_target != "section" ];
then echo "$enrollment_target is not a valid enrollment target. Exiting"; exit 1 ;
fi

# target_prefix is determined by target_id_type
if [ $target_id_type == "sis" ]; then target_prefix="sis_${enrollment_target}_id:" ;
elif [ $target_id_type == "canvas" ] ; then target_prefix="" ;
else echo "$target_id_type is not a valid target type. Exiting"; exit 1 ;
fi

# enrollment_role_id is determined by enrollment_type. Customize this block
# to swap out default roles with custom roles or just add some custom roles.
if [ $enrollment_type == "Student" ]; then enrollment_role_id=3 ;
elif [ $enrollment_type == "Teacher" ] ; then enrollment_role_id=4 ;
elif [ $enrollment_type == "Ta" ] ; then enrollment_role_id=5 ;
elif [ $enrollment_type == "Designer" ] ; then enrollment_role_id=6 ;
elif [ $enrollment_type == "Observer" ] ; then enrollment_role_id=7 ;
else echo "$enrollment_type is not a valid enrollment type. Exiting"; exit 1 ;
fi

# user_prefix is determined by user_id_type
if [ $user_id_type == "login" ]; then user_prefix="sis_login_id:" ;
elif [ $user_id_type == "sis" ] ; then user_prefix="sis_user_id:" ;
elif [ $user_id_type == "integration" ] ; then user_prefix="sis_integration_id:" ;
elif [ $user_id_type == "canvas" ] ; then user_prefix="" ;
else echo "$user_id_type is not a valid user ID type. Exiting"; exit 1 ;
fi

for user_id in ${users[@]}

do

for target_id in ${targets[@]}

do

echo "Attempting to enroll $user_id in $target_id"

api_response=`curl -s -S https://$canvas_domain/api/v1/${enrollment_target}s/$target_prefix$target_id/enrollments \
-X POST \
-F "enrollment[role_id]=$enrollment_role_id" \
-F "enrollment[type]=${enrollment_type}Enrollment" \
-F "enrollment[user_id]=$user_prefix$user_id" \
-F "enrollment[enrollment_state]=active" \
-F "enrollment[limit_privileges_to_course_section]=$section_limited_access" \
-F "enrollment[notify]=false" \
-H "Authorization: Bearer ${canvasAPItoken}"`

echo "$api_response\n"

done

done

 

View solution in original post

0 Likes