Any service provider worth its salt has to have an API. It’s one of the first questions we ask our providers and understandably one we get asked a lot as well. With our latest release we also updated our API, so I figured what better way to show off the API that with a sample of how easy it is to build a server.

First using Visual Studio 2010 we’ll install RestSharp using NuGet (if you don’t have NuGet installed stop reading and do yourself a favor and install it).

Install-Package RestSharp

Then create a rest client pointing at the CenturyLink Cloud API.

`private RestClient Client = new RestClient("");

Now we are ready to call the API. Once you create an API user in your account you can use it to logon and store the authentication in the client cookies.

`var request = new RestRequest("/auth/logon");
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
request.AddBody(new { APIKey = "b3b41e0818ad4dfdb9d43c964f680de1", Password = "plAtform6{+*" }); `

`var response = Client.Execute(request);
var content = DeserializeContent(response);
if (content.StatusCode != 0)
 throw new ApplicationException(content.Message); `

`if (Client.CookieContainer == null)
 Client.CookieContainer = new System.Net.CookieContainer(); `

 .Select(c => new Cookie(c.Name, c.Value, c.Path, c.Domain))
 .ForEach(c => Client.CookieContainer.Add(c));`

You’ll notice a few helpers methods I created to deal with deserializing the JSON response into a dynamic object. I’ve included that code at bottom for reference.

Let’s queue up the server for creation

`var request = new RestRequest("/server/createserver");
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
 Template = "WIN2008R2STD-64",
 Alias = "DEMO",
 Description = "API Demo Server",
 HardwareGroupID = 123,
 ServerType = 1, // 1-Standard, 2-Enterprise
 ServiceLeve = 2, // 1-Premium, 2-Standard
 Cpu = 2,
 MemoryGB = 4,
 ExtraDriveGB = 0,
 PrimaryDNS = "",
 SecondaryDNS = "",
 Network = "vlan123_172.1.123"

var response = Execute(request);
if (response.StatusCode != 0)
 throw new ApplicationException(response.Message);`

CreateServer returns a RequestID for which we can use to query for the status of the server build. And since all server provisioning is done through Blueprints, we can use the GetDeploymentStatus method on the Blueprint API to check on how things are going.

`var request = new RestRequest("/blueprint/getdeploymentstatus");
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
request.AddBody(new { RequestID = 1149 /*content.RequestID*/ });

while (true)
 var response = Execute(request);
 if (response.StatusCode != 0)
  throw new ApplicationException(response.Message);
 if (response.PrecentComplete == 100)


And finally we Logout.

`var request = new RestRequest("auth/logout");
request.RequestFormat = DataFormat.Json;
var response = Execute(request);
if (response.StatusCode != 0)
 throw new ApplicationException("Failed to Logout");`

And there you have it! 4 easy steps to provision a server from code. There are a ton of other things that you can do with the full API, but I’ll leave that up to you to discover.

As promised, the helper methods

`private dynamic Execute(RestRequest request)
 var response = Client.Execute(request);
 return DeserializeContent(response);
private dynamic DeserializeContent(RestResponse response)
 using (var reader = new StringReader(response.Content))
  using (var json = new JsonTextReader(reader))
   return JsonSerializer.Create(new

Originally posted on Troy’s blog.