Common APIs Demonstration
Enroll/Pre-enroll users via API
By default, Open Edx provides api/enrollment/v1/enrollment for a user to enroll. The API enrolls currently logged in users whose state has been saved in bearer token. If you don’t have a bearer token, see section Set up authentication for API to obtain one. For enrolling/pre-enrolling a specified user rather than the logged-in one, you need to modify the backend code of the API. After modification, you would be able to use a single API for the above scenario.
curl -X POST -H "Authorization: Bearer 59d340683440839845f1a2733ac98bec57aac69d" -H "Content-Type: application/json" -d '{"mode":"honor", "user":"user@mailinator.com", "course_details":{"course_id": "course-v1:E-LearnOrg+MOOC101+2014_H1"}}' "http://{LMS_URL}/api/enrollment/v1/enrollment"
After issuing this request, the user user@mailinator.com” will be enrolled in the course having title How to Build a Custom MOOC identified by course id as course-v1:E-LearnOrg+MOOC101+2014_H1. Also note that, if a user does not exist or have not yet registered in the platform, a pre-enrollment entry of the user with his email id has been created in Course enrollment alloweds. Once the user with this email id register and activate his account on the Open Edx platform, he/she will be automatically enrolled on this particular course.
Unenroll users via API
Similarly, the same API had to be modified for unenrolling a specified user from a course. The curl command is the same as above except the inclusion of “unenroll” parameter which needs to be “true” in string representation (i.e surrounded with double quotations).
curl -X POST -H "Authorization: Bearer 59d340683440839845f1a2733ac98bec57aac69d" -H "Content-Type: application/json" -d '{"mode":"honor", "user":"user@mailinator.com", "course_details":{"course_id": "course-v1:E-LearnOrg+MOOC101+2014_H1"},"unenroll":"true"}' "http://{LMS_URL}/api/enrollment/v1/enrollment"
If a pre-enrollment entry had been created against the user user@mailinator.com, it would be deleted.
Users final grade for a particular course
Open Edx provides api/grades/v1/courses API for fetching all the users grade for a particular course. The course id must be passed on the URL as a GET request. The output json will be a list of users containing their final grade.
curl --header "Authorization: Bearer 59d340683440839845f1a2733ac98bec57aac69d" -H "Content-Type: application/json" -X GET "http://{LMS_URL}/api/grades/v1/courses/course-v1:edX+DemoX+Demo_Course/" | python -m json.tool
Users gradebook for a particular course
Open Edx provides api/grades/v1/gradebook API for fetching all the users gradebook for a particular course. The gradebook contains the final grade as well as the grades in each subsection a user gets.
curl --header "Authorization: Bearer 59d340683440839845f1a2733ac98bec57aac69d" -H "Content-Type: application/json" -X GET "http://{LMS_URL}/api/grades/v1/gradebook/course-v1:edX+DemoX+Demo_Course/" | python -m json.tool
Note that, you need to enable waffle switch for writable gradebook before you can use the gradebook API. Otherwise you will be met with the following response.
Error: {"developer_message":"The writable gradebook feature is not enabled for this course.","error_code":"feature_not_enabled"}