How to use the Upload API? (C#)
C# example
This tutorial is written in C#. Download the full C#.NET example project with helper library.
When to use the Upload API?
Use the Upload API when you want the Platform to retain a copy of your submitted data so you can return to it at any time, view results visually in the web app, and re-run processing if needed.
How to work with the API (C#)
The following steps walk through uploading data and retrieving results using C#.
Libraries used:
IdentityModel.ClientNewtonsoft.JsonSystem.Text
Step 1: Generate a bearer token
Follow the How to generate a bearer token? (C#) tutorial to obtain an access token before proceeding.
Step 2: Upload data
This step sends an HTTP POST request to the /UploadData endpoint.
Set the variable values
var apiKey = "[Your_APIKey]";
var apiUrl = "https://api.example.com";
var accessToken = "[Your_AccessToken]";
var csv = File.ReadAllText("Samples\\ExampleData.csv");
var clientId = "[Your_ClientId]";
apiKeyandclientIdare available in the Platform web app.accessTokencomes from Step 1.csvshould point to your data file.
Create the HTTP client
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiUrl);
var request = new HttpRequestMessage(HttpMethod.Post, "/api/UploadData");
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
Build the request body
var uploadRequest = new UploadRequest
{
Name = "Sample upload",
Data = csv,
Username = clientId,
Key = apiKey,
Overwrite = false
};
Set Overwrite = true to replace an existing submission with the same Name.
Send the request
request.Content = new StringContent(JsonConvert.SerializeObject(uploadRequest), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
The response is a processing token — a string GUID you will use to retrieve results.
Step 3: Retrieve results
This step sends an HTTP GET request to the /GetResults endpoint.
Set the variable values
Set dataKey = "status" initially to check whether processing has completed.
Build and send the request
var queryString = string.Format("?token={0}&key={1}&dataKey={2}", processingToken, apiKey, dataKey);
var request = new HttpRequestMessage(HttpMethod.Get, "/api/GetResults" + queryString);
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
Step 4: Handle the response
Set dataKey = "feedback" and re-send the request to retrieve error details. Resolve any issues in your data file, then return to Step 2 with Overwrite = true and the same Name.
Set dataKey to one of the following values and send the request to retrieve your output. Send only one value at a time:
statusfeedbacktableofresultids
Complete code
using IdentityModel.Client;
using Newtonsoft.Json;
using Platform_API_Demo_Client.Models;
using System.Text;
namespace Platform_API_Demo_Client
{
public static class UploadApi
{
public static async void Run()
{
var identityUrl = "https://identity.example.com";
var apiUrl = "https://api.example.com";
var apiKey = "[Your_APIKey]";
var clientId = "[Your_ClientId]";
var clientSecret = "[Your_ClientSecret]";
var csv = File.ReadAllText("Samples\\ExampleData.csv");
var accessToken = await Authenticate(identityUrl, clientId, clientSecret, "platform_upload_api");
if (accessToken == null) { return; }
var processingToken = await Upload(apiUrl, apiKey, accessToken, csv, clientId);
Console.WriteLine("Processing token: " + processingToken);
while (await GetResults(apiUrl, apiKey, accessToken, processingToken, "status") == "processing")
{
Console.WriteLine("Still processing...");
Thread.Sleep(2000);
}
var feedback = await GetResults(apiUrl, apiKey, accessToken, processingToken, "feedback");
Console.WriteLine("Feedback: {0}", feedback);
var resultIds = await GetResults(apiUrl, apiKey, accessToken, processingToken, "tableofresultids");
Console.WriteLine("Result IDs: {0}", resultIds);
}
static async Task<string> Upload(string apiUrl, string apiKey, string accessToken, string csv, string clientId)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiUrl);
var request = new HttpRequestMessage(HttpMethod.Post, "/api/UploadData");
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
var uploadRequest = new UploadRequest
{
Name = "Sample upload",
Data = csv,
Username = clientId,
Key = apiKey,
Overwrite = false
};
request.Content = new StringContent(JsonConvert.SerializeObject(uploadRequest), Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
static async Task<string> Authenticate(string identityUrl, string clientId, string clientSecret, string scope)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(identityUrl);
var openIdDiscoveryDocument = client.GetDiscoveryDocumentAsync(identityUrl).GetAwaiter().GetResult();
var request = new ClientCredentialsTokenRequest
{
Address = openIdDiscoveryDocument.TokenEndpoint,
ClientId = clientId,
ClientSecret = clientSecret,
Scope = scope
};
var tokenResponse = await client.RequestClientCredentialsTokenAsync(request);
if (tokenResponse != null)
{
return tokenResponse.AccessToken;
}
else
{
Console.WriteLine("Authentication failed");
throw new Exception("Authentication failed");
}
}
static async Task<string> GetResults(string apiUrl, string apiKey, string accessToken, string processingToken, string dataKey)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(apiUrl);
var queryString = string.Format("?token={0}&key={1}&dataKey={2}", processingToken, apiKey, dataKey);
var request = new HttpRequestMessage(HttpMethod.Get, "/api/GetResults" + queryString);
request.Version = new Version(2, 0);
request.Headers.Add("authorization", $"Bearer {accessToken}");
var response = await client.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
}
}