Accessing the Rubrik API with C# .NET using RestSharp (part 1)

As I described in a previous post, at my job we recently migrated our VM backups to Rubrik. This has been extremely successful, but it broke our previous backup monitoring application, an ASP.NET web app I developed to monitor backup jobs from our previous backup application, VRanger. This web app queried the VRanger MSSQL database directly to pull job status data, and so it was a pretty simple app. Pull data from one database, insert data into another database. No biggie. But we just replaced our backup solution with Rubrik, which is an all-in-one appliance that does not have an easily-accessible SQL database to query. What it does have, however, is a REST API.

    REST API and Swagger

Rubrik, just like Nutanix, has a pretty full-featured REST API for performing operations from other applications. Chris Wahl of Rubrik has written a couple of blog posts and given some talks about using this API in vRealize Orchestrator, and has provided some sample code in PowerShell, but so far the example code out there is pretty scant, and since what I want to do right now isn’t in vRO or PowerShell, there wasn’t any good examples to work off of. So I had to figure it out myself. Hopefully this info will be helpful to someone else.

First of all, the most useful resource for the Rubrik API is the built-in API documentation. You can access it at

https://[Rubrik URL]/swagger-ui


This documentation is invaluable for learning what functions are available, what arguments they expect, and what data they return.


I am most familiar with writing code in .NET using C#, so I started looking for libraries meant to access REST services. I quickly came upon RestSharp, which is available to install via NuGet within Visual Studio. RestSharp has a nice feature that it handles serializing and deserializing data in XML and JSON formats, which comes very handy for interacting with Rubrik. Rubrik data is passed in JSON format, and not having to worry about the conversion is pretty nice.

    Accessing Rubrik API with RestSharp

Before you can do anything else in the Rubrik API, you need to log in. You need to pass the login credentials in the body of the login request, and if successful it returns a JSON object that contains a token that needs to be passed to all subsequent API requests. Here is how I made it work:

public class LoginCreds
  public string userId { get; set; }
  public string password { get; set; }

public class LoginData
  public string status { get; set; }
  public string description { get; set; }
  public string userId { get; set; }
  public string token { get; set; }

private string Login(LoginCreds MyLogin)
  var client = new RestClient();
  client.BaseUrl = new Uri("https://[Rubrik URL]");
  var request = new RestRequest(Method.POST);

  request.RequestFormat = DataFormat.Json;
  request.Resource = "login";


  var response = client.Execute(request);
  return response.Data.token;

The Login function should return the login token, which will be reused on all subsequent API calls.

One important note is that in the client.Execute() call, you specify a data type for the RestSharp library to de-serialize the return JSON data into. Then response.Data is an object in the format you specify, in this case LoginData. For API calls that return a collection of objects, you can specify the return type as a List<>, as you will see in the next example.

Note that I’m not including any exception handling in these examples. You will definitely want to enclose in a Try{} at least the client.Execute statement and any places where you reference response.Data.

public class VM
  public string id { get; set; }
  public string managedId { get; set; }
  public string moid { get; set; }
  public string name { get; set;
  public string vCenterId { get; set; }
  public string hostName { get; set; }
  public string hostId { get; set; }
  public string clusterName { get; set; }
  public string powerStatus { get; set; }
  public string configuredSlaDomainId { get; set; }
  public string configuredSlaDomainName { get; set; }
  public string effectiveSlaDomainId { get; set; }
  public string effectiveSlaDomainName { get; set; }
  public string effectiveSlaSourceManagedId { get; set; }
  public DateTime protectionDate { get; set; }
  public string ipAddress { get; set; }
  public string toolsInstalled { get; set; }
  public string snapshotCount { get; set; }
  public string primaryClusterUuid { get; set; }
  public string isReplicationEnabled { get; set; }
  public string isArchived { get; set; }

private void GetVMs(string token)
  var client = new RestClient();
  client.BaseUrl = new Uri("https://[Rubrik URL]");
  var request = new RestRequest(Method.GET);
  request.RequestFormat = DataFormat.Json;
  request.Resource = "vm";
  string authtoken = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(token + ":"));
  request.AddHeader("Authorization", "Basic " + authtoken);
  var response = client.Execute>(request);
  if (response.ResponseStatus == ResponseStatus.Error)
    txtOutput.Text += "VM Count = " + response.Data.Count + Environment.NewLine;
    foreach (VM theVM in response.Data)
      txtOutput.Text += + " - " + theVM.configuredSlaDomainName + Environment.NewLine;

That covers it for now. I was pretty excited when I got that much working. I haven’t actually written anything that actually does anything with this API yet, but having got this far, the rest should be cake. I will show what else I actually do with the Rubrik API in the next post.

Currently drinking: Sierra Nevada Northern Hemisphere Harvest Wet Hop IPA

1 Comment

Filed under Uncategorized

One Response to Accessing the Rubrik API with C# .NET using RestSharp (part 1)

  1. Tom


    Was looking around to see if you made any additional posts on this topics? I did not see any yet.

Leave a Reply

Your email address will not be published.