Accessing the Public Data API with C#

On This Page:

API Version 2.0 C# Sample Code

Single Series

Here is an example of how to use POST method in c# (Version 2.0, single series):

//Post request example version 2.0
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.bls.gov/publicAPI/v2/timeseries/data/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Using Javascript Serializer
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  var jS = new JavaScriptSerializer();
  var newJson = jS.Serialize(new SeriesPost()
  {
      seriesid = (new List() { "CUUR0000SA0" }).ToArray(),
      startyear = "",
      endyear = "",
      catalog = true,
      calculations = true,
      annualaverage = true,
      registrationKey = "EnterRegistrationKeyHere"
  });
  //View the JSON output
  System.Diagnostics.Debug.WriteLine(newJson);
  streamWriter.Write(newJson);
  streamWriter.Flush();
  streamWriter.Close();
}

Multiple Series

Here is an example of how to use POST method in c# (Version 2.0, multiple series):

//Post request example version 2.0
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.bls.gov/publicAPI/v2/timeseries/data/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Using Javascript Serializer
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  var jS = new JavaScriptSerializer();
  var newJson = jS.Serialize(new SeriesPost()
  {
      seriesid = (new List() { "CUUR0000SA0", "WPUSOP3000" }).ToArray(),
      startyear = "",
      endyear = "",
      catalog = true,
      calculations = true,
      annualaverage = true,
      registrationKey = "EnterRegistrationKeyHere"
  });
  //So you can see the JSON thats output
  System.Diagnostics.Debug.WriteLine(newJson);
  streamWriter.Write(newJson);
  streamWriter.Flush();
  streamWriter.Close();
}

One or More Series with Optional Parameters

Here is an example of how to use POST method in c# (Version 2.0, one or more series with optional parameters):

 

//Post request example version 2.0
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.bls.gov/publicAPI/v2/timeseries/data/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Using Javascript Serializer
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  var jS = new JavaScriptSerializer();
  var newJson = jS.Serialize(new SeriesPost()
  {
      seriesid = (new List() { " CUUR0000SA0" }).ToArray(),
      startyear = "2010",
      endyear = "2015",
      catalog = true,
      calculations = true,
      annualaverage = true,
      registrationKey = "EnterRegistrationKeyHere"
  });
  //View the JSON output
  System.Diagnostics.Debug.WriteLine(newJson);
  streamWriter.Write(newJson);
  streamWriter.Flush();
  streamWriter.Close();
}

 

API Version 1.0 C# Sample Code

Single Series

Here is an example of version 1.0 single series:

 

//Post request example version 1.0
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.bls.gov/publicAPI/v1/timeseries/data/CUUR0000SA0");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

Multiple Series

Here is an example of version 1.0 multiple series:

 

//Post request example version 1.0
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.bls.gov/publicAPI/v1/timeseries/data/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Using Javascript Serializer
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  var jS = new JavaScriptSerializer();
  var newJson = jS.Serialize(new SeriesPost()
  {
      seriesid = (new List() { "CUUR0000SA0", "WPUSOP3000" }).ToArray(),
      startyear = "",
      endyear = "",
      catalog = true,
      calculations = true,
      annualaverage = true,
  });
  //So you can see the JSON thats output
  System.Diagnostics.Debug.WriteLine(newJson);
  streamWriter.Write(newJson);
  streamWriter.Flush();
  streamWriter.Close();
}

Multiple Series, Multiple Years

Here is an example of version 1.0 multiple series specifying years:

 

//Post request example version 1.0
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.bls.gov/publicAPI/v1/timeseries/data/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
//Using Javascript Serializer
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  var jS = new JavaScriptSerializer();
  var newJson = jS.Serialize(new SeriesPost()
  {
      seriesid = (new List() { "CUUR0000SA0", "WPUSOP3000" }).ToArray(),
      startyear = "2010",
      endyear = "2015",
      catalog = true,
      calculations = true,
      annualaverage = true,
  });
  //So you can see the JSON thats output
  System.Diagnostics.Debug.WriteLine(newJson);
  streamWriter.Write(newJson);
  streamWriter.Flush();
  streamWriter.Close();
}

 

 

Last Modified Date: June 22, 2016