.NET Examples

Table of Contents

This document provides a few .NET samples to help get started. In addition to the examples below, a sample C# project can be downloaded here.

Example 1:

The following is an example of a console application that authenticates with the MyAdmin API, retrieves a list of device plans, and looks up details for a specific device:

static void Main(string[] args)
{
   WebServerInvoker api = new WebServerInvoker(null, 60000) { Url = "https://myadminapi.geotab.com/v2/MyAdminApi.ashx" };

   Dictionary<string, object> parameters = new Dictionary<string, object>
   {
      {"username", "user@geotab.com"},
      {"password", "<password>"}
   };

   ApiUser apiUser = api.Invoke("Authenticate", typeof (ApiUser), parameters) as ApiUser;

   Guid apiKey = apiUser.UserId;
   Guid sessionId = apiUser.SessionId;

   parameters = new Dictionary<string, object>
   {
      {"apiKey", apiKey},
      {"sessionId", sessionId}
   };
   ApiDevicePlan[] devicePlans =
      api.Invoke("GetDevicePlans", typeof (ApiDevicePlan[]), parameters) as ApiDevicePlan[];

   Console.WriteLine("Device Plans");
   Console.WriteLine("Level\tName");
   foreach (ApiDevicePlan devicePlan in devicePlans)
   {
      Console.WriteLine("{0}\t{1}", devicePlan.Level, devicePlan.Name);
   }


   Console.Write("\n\nDevice Information");


   parameters = new Dictionary<string, object>
   {
      {"apiKey", apiKey},
      {"sessionId", sessionId},
      {"serialNo", "G6XXX0XXXD08"}
   };
   ApiDeviceInstallResult device =
      api.Invoke("LookupDevice", typeof (ApiDeviceInstallResult), parameters) as ApiDeviceInstallResult;


   Console.WriteLine("Device Details:");
   Console.WriteLine("Firmware Version {0}", device.FirmwareVersion);
   Console.WriteLine("Comments: {0}", device.Comments);
   Console.WriteLine("Last Server Communication: {0}", device.LastServerCommunication.ToString());
   Console.WriteLine("Possible Issues: {0}", device.PossibleIssues);


   Console.ReadKey();
}
hYgO1H6JtTzw9PnYR70di qee6D2WRg8LDLKNK8fLu4NV8IfeVqi6PKwvWgtdmujE3aEbIkxCFVC uHMYA3ucEQMllgJjoigtCwuplq09cJ3SI3yBJo76pfJLmfocW5V

Example 2:

This example demonstrates how to create a support ticket with an attachment.

WebServerInvoker api = new WebServerInvoker(null, 60000) { Url = "https://myadminapi.geotab.com/v2/MyAdminApi.ashx" };


Dictionary<string, object> parameters = new Dictionary<string, object>
{
    {"username", "user@geotab.com"},
    {"password", "<password>"}
};


ApiUser apiUser = api.Invoke("Authenticate", typeof(ApiUser), parameters) as ApiUser;


Guid apiKey = apiUser.UserId;
Guid sessionId = apiUser.SessionId;


parameters = new Dictionary<string, object>
{
    {"apiKey", apiKey},
    {"sessionId", sessionId}
};


// Read the attachment from the disk
FileStream file = File.Open("C:\\attachment.jpg", FileMode.Open, FileAccess.Read);
byte[] attachment = new byte[file.Length];
file.Read(attachment, 0, (int)file.Length);
string contentType = "image/jpeg";


parameters = new Dictionary<string, object>
{
    {"apiKey", apiKey},
    {"sessionId", sessionId},
    {"userEmail", "user@geotab.com"},
    {"file", attachment},
    {"contentType", contentType},
    {"fileName", "attachment.jpg"}
};


string fileToken = api.Invoke("UploadSupportTicketFile", typeof(string), parameters) as string;


parameters = new Dictionary<string, object>
{
    {"apiKey", apiKey},
    {"sessionId", sessionId},
    {"userEmail", "user@geotab.com"},
    {"subject", "API Example"},
    {"description", "This is an example of the API"},
    {"fileToken", fileToken}
};
ApiTicket returnTicket = api.Invoke("CreateSupportTicket", typeof(ApiTicket), parameters) as ApiTicket;


Console.WriteLine("New ticket ID: {0}", returnTicket.Id);
Console.ReadKey();
EgWDMphwSQURQ idSLSQECnCncK6Hgu5bONkpoyjoF2DLcEW 4T0DCMK3lAGV2gyz1JyzQpW772hYW4MZUTRvA4S8aJU1VMygPkRpbWynr2fC0Cus17eXd64XDUdjcwd
a0aJ2Z77M4YeIoA ovsqGn Wf2Y 1cBM4BHhTVFfyqOljpEYDWTl9hwSlhl Ay tX9mZIGYaV4OIUdlzFXfXkzQLSOTAXzGnADlr0fa kgjG2t34 osFP f 6G87k32s