Overview

We publish an extensive documentation page for the Spira 6.0 REST API that describes the resources, methods, requests, bodies and sample JSON/XML representations.

However many customers have asked for some C# / .NET samples of the REST API in action. So we have created a simple repository of sample code for accessing the REST API in C#.

Sample Code

Here's a snippit of some sample code for connecting to the Spira REST API and authenticating:

       /// <summary>
        /// Authenticates against the server. Need to call before using other methods.
        /// This overload allows you to use the same API Key / RSS Token as the REST Service
        /// </summary>
        /// <param name="userName">The username of the user</param>
        /// <param name="apiKey">The user's API Key / RSS Token</param>
        /// <param name="plugInName">The name of the plug-in</param>
        /// <returns>The credentials object if successful, NULL if not</returns>
        /// <remarks>Also checks to make sure they have enough connection licenses</remarks>
        public RemoteCredentials Connection_Authenticate(string userName, string apiKey, string plugInName)
        {
            //Create the request
            RestRequest request = new RestRequest("Connection_Authenticate2");
            request.Credential = new System.Net.NetworkCredential();
            request.Credential.UserName = userName;
            request.Credential.Password = apiKey;
            request.Method = "GET";
            request.Url = baseUrl + "/" + "users";
            request.Headers.Add(new RestHeader() { Name = StandardHeaders.Content_Type, Value = "application/json" });

            //Authenticate against the web service by calling a simple method
            RestClient restClient = new RestClient(request);
            RestResponse response = restClient.SendRequest();
            if (!response.IsErrorStatus && response.Headers.FirstOrDefault(h => h.Name == Properties.Resources.Rest_StatusCode).Value == "200 OK" && !String.IsNullOrEmpty(response.Body))
            {
                //Deserialize the user
                RemoteUser remoteUser = JsonConvert.DeserializeObject<RemoteUser>(response.Body);

                RemoteCredentials remoteCredentials = new RemoteCredentials();
                remoteCredentials.UserId = remoteUser.UserId.Value;
                remoteCredentials.UserName = userName;
                remoteCredentials.PlugInName = plugInName;
                remoteCredentials.IsSystemAdmin = remoteUser.Admin;
                remoteCredentials.ApiKey = apiKey;

                return remoteCredentials;
            }
            
            //Failure
            return null;
        }
    }

The full sample code GitHub repository can be found here: