Assigning a user as an observer with the API

Jump to solution
Community Member

I have been exercising the APi a bit now for an integration project I am working on.

One of the few issues I am having involves setting a user as an observer while enrolling a student in a course.

Before I get to this point, I have:

Added two users through the API using PHP:

URI: accounts/self/users


'pseudonym' => [
    'unique_id' => $args['email'],
    'password' => $args['password'],
    'integration_id' => $args['uId'],
    'send_confirmation' => false
'user' => [
    'name' => $args['first_name'] . ' ' . $args['last_name'],
    'sortable_name' => $args['last_name'] . ', ' . $args['first_name'],
    'skip_registration' => true
'communication_channel' => [
    'type' => 'email',
    'address' => $args['email'],
    'skip_confirmation' => true

Both users appear to be created correctly, and then I move on to assign one of the users to a course as a student, and assign the other as an observer:

URI: courses/'<course ID>/enrollments

$oid = <Canvas user ID for the intended observer>

$sid = <Canvas user ID for the intended student>


'user_id' => $oid,
'enrollment_type' => 'ObserverEnrollment',
'role' => 'Observer',
'enrollment' => [
    'user_id' => $sid,
    'type' => 'StudentEnrollment'

The good:

The student is changed to type 'student'

The student is enrolled in the course


The ugly (and why I am here):

The parent is not assigned an observer role

The parent is not changed to type 'observer'


Am I missing something simple? Or am I misunderstanding a basic API principle as implemented by Canvas?

Seriously appreciate any help or feedback!




1 Solution
Community Coach
Community Coach

Hi @Rickpunzel,

I'm not familiar with PHP at all, but I'll infer some of the syntax from your post and hope it's correct.  Looking at the enrollments API, I think your enrollment params objects should look more like the following:

For the student:

'enrollment' => [
    'user_id' => $sid,
    'type' => 'StudentEnrollment'


For the observer:

'enrollment' => [
    'user_id' => $oid,
    'type' => 'ObserverEnrollment',
    'associated_user_id' => '$sid


Basically, an enrollment always needs enrollment[user_id] which is the Canvas_id of the user you're working with, and enrollment[type] which is what kind of role you want the user to have.  For the observer-type roles, a third piece of information is needed, enrollment[associated_user_id] which is the canvas_is of the user you want this enrollment to observe.  You can of course use more parameters if you wish, but those are the bare minimum.  I hope this may solve your issue!


View solution in original post