Getting Started Using the API with an Example in C#

Follow

Applies to: Starter, Pro, Plus, & Enterprise Plans

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. 

In the main method we will begin by setting up the connection to your portal where you should enter the portal route, account credentials and your API private key.

URL_Domain = "http://<portal name>.myabsorb.com/api/Rest/v1/";
                
string Url = URL_Domain + "Authenticate?username=<username>&password=<password>&privateKey=<private key>";

string token;

HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text";

Next we'll send our credentials in order to generate the API token.  

// Clear out all buffers for the current writer 
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
	streamWriter.Flush();
	streamWriter.Close();
}

// Get response  
HttpWebResponse myWebResponse = (HttpWebResponse)request.GetResponse();
// Get the response stream  
StreamReader reader = new StreamReader(myWebResponse.GetResponseStream());

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = serializer.DeserializeObject(reader.ReadToEnd());

token =  jsonObject.ToString();

Console.WriteLine("Client Token: " + 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.

public void GetCourses(string token)
{
	try
	{
		string Url = URL_Domain + "Courses";

		HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
		request.Method = "GET";
		request.ContentType = "application/json";
		request.Headers.Add("Authorization", token);

		// Get response  
		HttpWebResponse myWebResponse = (HttpWebResponse) request.GetResponse();
		// Get the response stream  
		StreamReader reader = new StreamReader(myWebResponse.GetResponseStream());

		Console.WriteLine(reader.ReadToEnd());
		Console.ReadLine();
	}

	catch (Exception es)
	{
		Console.WriteLine(es);
		Console.ReadLine();
	}

}

 

public void GetEnrollments(string token)
{
	try
	{
		string Url = URL_Domain + "courses/<courseID>/enrollments?status=0";

		HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
		request.Method = "GET";
		request.ContentType = "application/json";
		request.Headers.Add("Authorization", token);

		// Get response  
		HttpWebResponse myWebResponse = (HttpWebResponse)request.GetResponse();
		// Get the response stream  
		StreamReader reader = new StreamReader(myWebResponse.GetResponseStream());

		Console.WriteLine(reader.ReadToEnd());
		Console.ReadLine();

	}

	catch (Exception es)
	{
		Console.WriteLine(es);
		Console.ReadLine();
	}
}

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,

public void Upload(string token)
{
	try
	{
		string Url = URL_Domain + "users/upload";


		HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;
		request.Method = "POST";
		request.ContentType = " text/json";
		request.Headers.Add("Authorization", token);



		string data = "[{\"Id\":\"\",\"FirstName\":\"Ed\",\"LastName\":\"test\",\"Username\":\"testusername1\",\"EmailAddress\":\"test@test.com\",\"ActiveStatus\":0,\"ExternalId\":\"39\",\"CustomFields\":{},\"DepartmentId\": \"deb8238d-f0d6-40b6-a71d-6bc8ed9a983d\"}]";
		using (var streamWriter = new StreamWriter(request.GetRequestStream()))
		{
			streamWriter.Write(data);
			streamWriter.Flush();
			streamWriter.Close();
		}

		// Get response  
		HttpWebResponse myWebResponse = (HttpWebResponse) request.GetResponse();
		// Get the response stream  
		StreamReader reader = new StreamReader(myWebResponse.GetResponseStream());

		// Console application output  
		Console.WriteLine(reader.ReadToEnd());
		Console.ReadLine();

	}

 

Have more questions? Submit a request

0 Comments

Article is closed for comments.