Getting Started Using the API v1 with an Example in C#

Before we can make calls we need to generate an API token. All requests to the LMS must include an API token as authentication. This token is generated using your API Private Key and your Absorb user account credentials and will be used to verify both client access to the API as well as a specific admin's access to certain LMS data (based on their admin roles set up in the LMS). Tokens expire after 4 hours or when a new API token has been generated. 

This example makes use of a third party package called Rest Sharp in order to simplify the process. For simplicity, all calls return plain text but it's possible to parse the json response into a defined model using Rest Sharp.

private static string GenerateToken()
        {
            var client = new RestClient($"http://{PortalName}.myabsorb.com/api/Rest/v1/");
            var request = new RestRequest("Authenticate", Method.POST);
            request.AddHeader("Content-Type", "application/json");

            var credentials = new
            {
                Username = "Username",
                Password = "Password",
                PrivateKey = "PrivateKey"
            };

            request.AddJsonBody(credentials);

            var response = client.Execute(request);
            var content = response.Content;

            //This removes outer quotes from the token
            var token = content.Trim('"');
            return token;
        }

The next two methods demonstrate making GET requests for courses or a specific enrollments. The only difference is a modification to the URL string.

private static void GetCourses(string token)
        {
            var client = new RestClient($"http://{PortalName}.myabsorb.com/api/Rest/v1/");
            var request = new RestRequest("Courses", Method.GET);
            request.AddHeader("Authorization", token);
            request.AddHeader("Content-Type", "application/json");

            var response = client.Execute(request);
            var content = response.Content;
            Console.WriteLine(content);
        }

private static void GetEnrollments(string token, string courseId)
        {
            var client = new RestClient($"http://{PortalName}.myabsorb.com/api/Rest/v1/");
            var request = new RestRequest($"courses/{courseId}/enrollments?status=0", Method.GET);
            request.AddHeader("Authorization", token);
            request.AddHeader("Content-Type", "application/json");
var response = client.Execute(request); var content = response.Content; Console.WriteLine(content); }

Note the GetEnrollments will only pull active enrollments or Status = 0 based on the URL string. 

To make a POST we just pass the string data along with our request. Below is a user upload,

private static void UploadUser(string token)
        {
            var client = new RestClient($"http://{PortalName}.myabsorb.com/api/Rest/v1/");
            var request = new RestRequest($"users/upload", Method.POST);
            request.AddHeader("Authorization", token);
            request.AddHeader("Content-Type", "application/json");
//users/upload expects a list of one or more users var user = new[] { new { FirstName = "sample", LastName = "user", UserName = "Sample.User", EmailAddress = "test@test.com", DepartmentId = "7f81e30a-93d6-4f26-b285-92d6e48f0376" } }.ToList(); request.AddJsonBody(user); var response = client.Execute(request); var content = response.Content; Console.WriteLine(content); }

 

Note: Due to support for TLS1.1 being depreciated there may be some cases where an additional line of ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; will be needed to force compliance. 
Was this article helpful?
1 out of 2 found this helpful