Skip navigation
All Places > Canvas Developers > Blog > Author: Kevin Wright

Canvas Developers

2 Posts authored by: Kevin Wright

This is a follow up to my previous blog post, "A Simple Python GET Script". Personally, I find myself using POST requests quite frequently. So while the GET script was great for showing at a basic level how Python can interact with your Canvas instance, this blog post can hopefully show why this is such powerful tool. 

Creating a New User

I'd like to start off by saying that Canvas makes it extremely easy to create new users. In fact, the ease of use of the Canvas system is the reason that Financial Mentors of America (FMA) chose to develop it's online content in it, as opposed to other LMS that are available. But there are a few advantages of taking the complicated route and creating the user through the API. 

 

The main reason I do it this way, in some cases, is because creating a user through the API gives me full control over the process. I can completely register a user on the back-end, pick him a password, and skip sending the auto-generated confirmation email to send my own welcome letter instead. You can view all the other neat things by viewing the official API documentation

 

This, in a way, demonstrates the real power of the Canvas LMS. You don't need to know anything about the back-end to be able to use Canvas to a great extent. However, for the users who take the time to learn it, it opens up a whole new world of functionality. 

The Script

POST Script in Python

The requests library really makes this process simple. You'll notice from the first blog post that there are many of the same elements in it. You still have a URL, although the URL changes depending which API call you are making. You still have headers and you still need your access token. In fact, I have only changed two parts of the last script. 

  1. payload = {...,...,...}
  2. r = requests.post(..., ..., params=payload)

 

Payload

This is where we tell our script which of the special parameters we want to include in our post. In this case, I've picked the users name, assigned to the name the email address, picked his password, chose not to send his confirmation email, and auto-registered him. Once again, for any request you make, there are almost always custom parameters you can include in your request. These are all documented in the Canvas API documentation

 

 

r = requests.post(..., ..., params = payload)

This is where we tell Canvas what kind of API request we are making. In my first blog, we told Canvas to GET information, in this blog we are telling it to POST information into our Canvas instance. In addition to the URL and the headers, we include in the POST the "Payload" which contained the specific information we wanted our POST request to do. 

 

The Result

Script Result
So I have python here set-up to return to me some mumbo-jumbo, However, the status code 200 means that my request went through. I was able to log-in with my user and the selected password, and my parameters went through successfully. I usually use stuff like this in conjunction with enrollment API, so after I have the created user, I can enroll him/her into a course with whatever level of access I like, with about the same level of control. 

 

Hopefully, this has been useful in not only giving you an example of some simple, working Python code that can interact with Canvas but also demonstrating some of the inherent usefulness in using the API to gain more control over some of the things you may have to do in Canvas on a regular basis. 

Hey guys, 

 

I'll spare you my life story on this one, but I am the LMS Administrator at a non-profit institution. What that means is that I'm the administrator, the course designer, the data-analyst, the help team, and all things betwixt. I've had to learn about APIs from the ground up. Since most of the content in here is geared toward advanced users, I thought I would post something for the user who is just getting started. 

 

Step One - Get access token

 

Step One

 

The first thing you need when performing any API operation is an access token. You have to have administrative rights to do this. The good news is that you can auto-generate them yourself. Click on account - settings. Scroll down until you come to this and generate a new access token. 

 

Step Two

 

This step is the script itself. Python has a library available to make API requests. Naturally, this is called "requests". The documentation for this library can be found here.  There are several low priced books also available on Google-Play-Books. These books all assume some level of basic knowledge but if you want to further explore it, then they are good resources. Below is a picture of the script.

 

Python Code

 

The first part, URL, is the URL you are making your request to. The next and most important section is your headers. This is where you need your access token, or your request will never work. The part that is blurred it is where you will copy & paste your access token. 

 

After that, we use the variable r to form our request. This is where the library comes in handy. Here, we call our URL and use the headers to access it. Then we tell python to print our results. In this case, I have them printed in text and json format. The results are printed below. 

 

GET Result

 

The point of this is that it works. It returns your result with no hiccups. The next step of this project for me, personally, is to have these results saved to a CSV file that I can easily analyze. But the point of this is that it works. 

 

There are many useful things you can do with API calls, including POSTS, that allow you to create a course, delete a course, create and delete users, without ever having to access Canvas itself.