A Simple Python POST Script - Creating a New User

kevinw
Community Contributor
2
5714

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. 

2 Comments