The following C# code demonstrates how to authenticate and consume ProTrack+ API.
Send a POST request to the authentication endpoint to obtain a token.
This token is valid for 24 hours time.
// C# Code to Get Authentication Token using System; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading.Tasks; class Program { static async Task Main() { string token = await GetAuthToken(); if (!string.IsNullOrEmpty(token)) { await CallApiWithToken(token); } } static async Task<string> GetAuthToken() { string authUrl = "https://protrackplusapi.ddot.dc.gov/Authentication/Authenticate/login"; // Replace with actual API URL (as needed) var credentials = new { username = "your_username", password = "your_password" }; using var client = new HttpClient(); var response = await client.PostAsync(authUrl, new StringContent(JsonSerializer.Serialize(credentials), Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); using var doc = JsonDocument.Parse(json); return doc.RootElement.GetProperty("token").GetString(); } Console.WriteLine("Authentication failed."); return null; }
Once you have the token, use it in the 'Authorization' header. You can use the same token for 24 hours time.
// C# Code to Call API with Token static async Task CallApiWithToken(string token) { string apiUrl = "https://protrackplusapi.ddot.dc.gov/api/TaskOrder/GetTaskOrders/{PIID}"; // Replace with actual API URL (as needed) using var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); var response = await client.GetAsync(apiUrl); if (response.IsSuccessStatusCode) { string result = await response.Content.ReadAsStringAsync(); Console.WriteLine("API Response: " + result); } else { Console.WriteLine("API call failed: " + response.StatusCode); } }
The following JavaScript code can be used to consume the API from a web page.
// JavaScript to Call API with Token async function authenticate() { const authUrl = 'https://protrackplusapi.ddot.dc.gov/Authentication/Authenticate/login'; //Replace with actual API URL (as needed) const response = await fetch(authUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: 'your_username', password: 'your_password' }) }); const data = await response.json(); return data.token; } async function callApi() { const apiUrl = 'https://protrackplusapi.ddot.dc.gov/api/TaskOrder/GetTaskOrders/{PIID}'; //Replace with actual API URL (as needed) const token = await authenticate(); const response = await fetch(apiUrl, { method: 'GET', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' } }); const data = await response.json(); console.log(data); }