How To Use the REST API#

This how-to will help you get setup to be able to make authenticated requests to the edx-platform REST API.

Assumptions#

  • You have access to the edx-platform Django Admin (/admin) Panel.

  • You have a user that you want to make the rest calls as (UserA).

  • You are familiar with the basics of HTTP and Rest

  • For the purposes of this tutorial we’ll assume your LMS is located at https://lms.example.com

Steps#

  1. Go to https://lms.example.com/admin/oauth2_provider/application/

  2. Click Add Application

  3. Choose “UserA” for the user.

  4. Choose Confidential Client Type

  5. Choose “Client Credentials” for the Authorization Grant Type

  6. Set a name for your application.

  7. Save the client_id and client_secret.

  8. The best way to interact with the edx-platform REST API is by making requests using the JWT Authorization header. Use the client_id and client_secret to get a JWT token.

    import base64
    import requests
    
    client_id = "vovj0AItd9EnrOKjkDli0HpSF9HoooaTY9yueafn"
    # Client secrets should not be exposed in your code, we put it here to
    # make the example more clear.
    client_secret = "a3Fkwr24dfDSlIXt3v3q4Ob41CYQNZyGmtK8Y8ax0srpIa2vJON3OC5Rvj1i1wizsIUv1W1qM1Q2XPeuyjucNixsHXZsuw1dn2B9nH3IyjSvuFb5KoydDvWX8Hx8znqD"
    
    credential = f"{client_id}:{client_secret}"
    encoded_credential = base64.b64encode(credential.encode("utf-8")).decode("utf-8")
    
    headers = {"Authorization": f"Basic {encoded_credential}", "Cache-Control": "no-cache"}
    data = {"grant_type": "client_credentials", "token_type": "jwt"}
    
    token_request = requests.post(
        "http://lms.example.com/oauth2/access_token", headers=headers, data=data
    )
    access_token = token_request.json()["access_token"]
    
  9. The code above will produce a JWT token that you can use to hit any existing edx-platform API endpoint.

    Example, get all of UserA’s Enrollments#
    enrollment_request = requests.get(
        "http://lms.example.com/api/enrollment/v1/enrollment",
        headers={"Authorization": f"JWT {access_token}"},
    )