Calling Instructure Canvas LMS API in C# ASP.NET MVC Using RestSharp

In this example I am calling the Canvas LMS API to get a list of enrolled students, including their grades, for a specific course.

A little background, if you try to call the Canvas API client side (JavaScript), you will get a CORS Same Origin Policy error: No ‘Access-Control-Allow-Origin’, I tried it 🙁 . So use server-side code, this is where C# comes into place 🙂

Back to C#…To accomplish this I am making use of RestSharp as it is really easy to make API calls with this library.

Since I want to get a list of students for a specific course, I need to use List users in course API call.

GET /api/v1/courses/:course_id/users

Now, since in my custom web application I do not have the Canvas given :course_id, I can make use of the SIS ID as as outlined in Object IDs, SIS IDs, and special IDs since I have that available. And I end up with the API call looking like this

GET /api/v1/courses/sis_course_id:<ID>/users

This call will bring back the list of users I need, each of them being an User Object. At this point, if I make the call like this, I will get back teacher[s], student[s], student_view[s], ta[s], observer[s] and, designer[s], so to filter out for ONLY students, I need to add enrollment_type[] to the call.

GET /api/v1/courses/sis_course_id:<ID>/users?enrollment_type[]=student

But wait, I also need their grades, so I need to add include[]=enrollments to the call, which will bring the course information including grades.

GET /api/v1/courses/sis_course_id:<ID>/users?enrollment_type[]=student&include[]=enrollments

Enrollment Object
Grade Object

By default Canvas limits the response to 10 users, so adding per_page parameter allows me to specify more users in the response. I chose to bring back “1000” users since we will never have more than 1000 users in a course. See Pagination.

Now the API call looks like this

GET /api/v1/courses/sis_course_id:<ID>/users?enrollment_type[]=student&include[]=enrollments&per_page=1000

Last, but not least, and the most important, AUTHENTICATION 🙂 Depending on your needs you can either generate an Access Token (Manual Token Generation) or use their OAuth2. In my case I generated an Access Token and use that every time.

The access token will need to be included in the API call like this

GET /api/v1/courses/sis_course_id:<ID>/users?enrollment_type[]=student&include[]=enrollments&per_page=1000&access_token=<ACCESS-TOKEN>

Now that I finally have what I need from Canvas, it’s time for some C# code!!

Side note: Before writing your app, play with the Canvas API as described in the Getting Started With The Api page.


You will need three C# Models (or Classes).

These models represent the User Object, Enrollment Object and Grade Object. And depending on your needs, you can omit properties from each object.

User Model

public class User
    public int id { get; set; }
    public string name { get; set; }
    public string sis_user_id { get; set; }
    public string login_id { get; set; }
    public List<Enrollment> enrollments { get; set; }

Enrollment Model

public class Enrollment
    public int user_id { get; set; }
    public int course_id { get; set; }
    public string enrollment_state { get; set; }
    public string sis_course_id { get; set; }
    public List<Grade> grades { get; set; }

Grade Model

public class Grade
    public float? current_score { get; set; }
    public float? current_grade { get; set; }
    public float? final_score { get; set; }
    public float? final_grade { get; set; }

Canvas API Class

This class wil consume your Canvas API call

public static class CanvasAPI
    const string token = "<ACCESS-TOKEN>";
    const string domain = "";

    public static List<User> GetUsersInCourse(string sisCourseId) 
        string url = $"https://{domain}/api/v1/courses/sis_course_id:{sisCourseId}/users?enrollment_type[]=student&include[]=enrollments&per_page=1000&access_token={token}";
        var restClient = new RestClient(url);
        var request = new RestRequest(Method.GET);
        var response = restClient.Execute<List<User>>(request);

        return response.Data;

Don’t forget to install RestSharp via Nuget package and include it in your class.

using RestSharp;


Inside your controller call the CanvasAPI class.

public ActionResult GetCanvasUsers() 
    List<User> canvasusers = GetUsersInCourse(sisCourseId);

JSON Response

The JSON response that results from the Canvas API call looks like this

  "name":"John Doe",
  "sortable_name":"Doe, John",
  "short_name":"John Doe",

That’s it. Share this post if you liked it.


Esau Silva
Software Engineer at Region One ESC
Full Stack Software Engineer working with Microsoft technologies, ReactJS is awesome and avid Brazilian Jiu-Jitsu practitioner
Spread the love

2 thoughts on “Calling Instructure Canvas LMS API in C# ASP.NET MVC Using RestSharp

  1. Mihir says:

    Getting data using access token will not work in real time. We have to go with OAuth process. Could you please guide m how to do this OAuth process?

Leave a Reply

Your email address will not be published. Required fields are marked *