Background

This document captures the conventions to be used for all edX REST APIs. 

Useful reading:

High Level Requirements

Conventions

1. URL Naming

See list of example APIs at the end of this document.

2. Identifying Resources

3. HTTP Verbs

A note on HTTP PATCH

"Plain" HTTP PATCH (RFC 5789) is neutral with respect to content type and only specifies that the request body provide instructions for how to update the resource in question, not the format of those instructions.  Several flavors of PATCH specific to JSON documents have more explicit definition, as noted above.  

In DRF, the default PATCH handling fairly closely resembles what is specified by merge patch, but it does not require (or understand) the "application/merge-patch+json" media type.  In edX REST APIs, if an implementation will use DRF's default PATCH handling, the implementation MUST recognize and accept the merge patch media type; API clients SHOULD use this media type, preferring it to the more typical "application/json" type.

The reason for this stricture is to explicitly leave room for supporting multiple PATCH styles in any given API, should this become desirable, without breaking existing clients.

4. URL Parameters

5. Errors

6. Version

7. Pagination

8. Documentation

9. Discoverability

10. Multiple Formats

11. Authentication

By convention, our REST APIs support the following two authentication schemes:

12. Serialization Conventions

To Be Discussed

Expanding referenced objects

APIs can support expanding related objects, e.g. the team API optionally includes user profile information for each member.  This is preferable to requiring the client to make multiple subsequent AJAX calls.

For example, there can be three levels for a related object (using "username" as an example):

"username": "andya"
"user": {
  "username": "andya",
  "url": "https://openedx.example.com/api/user/v1/accounts/andya"
}
"user": {
  "username": "andya",
  "country": "UK",
  "profile_image": {
    "has_image": True,
    "image_url_full": "http://my-image-storage.net/media/profile_images/123456789_500.jpg",
    "image_url_large": "http://my-image-storage.net/media/profile_images/123456789_120.jpg",
    "image_url_medium": "http://my-image-storage.net/media/profile_images/123456789_50.jpg",
    "image_url_small": "http://my-image-storage.net/media/profile_images/123456789_30.jpg",
  },
  ...
}

By default:

Docstring Format

This section describes the format for DRF APIs only. See the edX Python guidelines for conventions for ordinary Python docstrings: https://github.com/edx/edx-platform/wiki/Python-Guidelines

edX API docstrings serve two purposes:

  1. They are interpreted by DRF to provide dynamic API documentation

  2. They are compiled by the Sphinx Napoleon extension to be included in our published API documentation set

These two tools put constraints on the format of the docstring, so we developed the following compromise that should work for both:

Here is a representative docstring (from https://github.com/edx/edx-platform/blob/master/common/djangoapps/enrollment/views.py#L67):

class EnrollmentView(APIView, ApiKeyPermissionMixIn):
    """
    **Use Cases**

        Get the user's enrollment status for a course.

    **Example Requests**:

        GET /api/enrollment/v1/enrollment/{user_id},{course_id}

    **Response Values**

        * created: The date the user account was created.
        * mode: The enrollment mode of the user in this course.
        * is_active: Whether the enrollment is currently active.
        * course_details: A collection that includes:
            * course_id: The unique identifier for the course.
            * enrollment_start: The date and time that users can begin enrolling in the course.
              If null, enrollment opens immediately when the course is created.
            * enrollment_end: The date and time after which users cannot enroll for the course.  
              If null, the enrollment period never ends.
            * course_start: The date and time at which the course opens.  If null, the course 
              opens immediately when created.
            * course_end: The date and time at which the course closes.  If null, the course never ends.
            * course_modes: An array of data about the enrollment modes supported for the course. 
                Each enrollment mode collection includes:
                * slug: The short name for the enrollment mode.
                * name: The full name of the enrollment mode.
                * min_price: The minimum price for which a user can enroll in this mode.
                * suggested_prices: A list of suggested prices for this enrollment mode.
                * currency: The currency of the listed prices.
                * expiration_datetime: The date and time after which users cannot enroll in the course in this mode.
                * description: A description of this mode.
            * invite_only: Whether students must be invited to enroll in the course; true or false.
        * user: The ID of the user.
    """
    ...


Review Process

APIs must go through a community review process and review by the architecture council before being accepted into the platform. See the Architecture Review Process for the process for gaining approval.

Example APIs

There are a number of existing APIs that you can crib from:

Here are some representative URLs

/api/course/v1/courses/
/api/enrollment/v1/enrollments/
/api/discussion/v1/threads/{thread_id}/