1. Home
  2. /
  3. Developer Guide
  4. /
  5. Rest API
  6. /
  7. Source code examples

Source code examples

⚠️ The authToken parameter below is part of the legacy authentication method. For new integrations, use OAuth 2.0 instead and send the access token in the Authorization: Bearer <access_token> header rather than as a URL parameter. See 1. AuthToken — OAuth 2.0 Authentication for the full documentation.

You will find on this page sample code source that interacts with the Cirrus Shield REST API. We grouped the examples by either tool or programming language. If you don’t know where to start, start with the Postman collection.

Postman

Download the following Postman collection, it contains a various set of calls to the REST API of Cirrus Shield : Download

.Net

Here are some samples of using Cirrrus Shield’s REST API with .Net

  • Authenticate (OAuth 2.0)

Use the Client Credentials flow to obtain an access token, then attach it to every call via the Authorization: Bearer header. See 1. AuthToken — OAuth 2.0 Authentication for the full details and how to get a token.

var accessToken = await GetOAuthAccessTokenAsync(); // Client Credentials flow — see the OAuth 2.0 guide
Http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
  • Select a list of projects
        public async Task<List<Project>> GetActiveProjects()
        {
            try
            {
                List<Project> listOfActiveProjects = new List<Project>();
                var csProjectsStr = await Http.GetStringAsync(ServiceEndpoint + "/Query?selectQuery=Select+Id,Name+from+Project+where+Status='In+Progress'");

                XElement csProjectsXml = XElement.Parse(csProjectsStr);

                foreach (XElement xe in csProjectsXml.Descendants("Project"))
                {
                    Project prj = new Project() { Id = xe.Element("Id").Value, Name = xe.Element("Name").Value };
                    listOfActiveProjects.Add(prj);
                }
                return listOfActiveProjects.OrderBy(o => o.Name).ToList();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
  • Create a record (object : Time)
public async Task<Time> SaveTimeItem(Time timeItem)
        {
            try
            {
                var xmlData = new XElement("Data", "");
                var xmlTime = new XElement("Time", "");
                xmlTime.Add(new XElement("Id", timeItem.Id));
                if (string.IsNullOrEmpty(timeItem.Name))
                    xmlTime.Add(new XElement("Name", timeItem.Name));
                xmlTime.Add(new XElement("OwnerId", timeItem.OwnerId));
                xmlTime.Add(new XElement("Work_Site", timeItem.Work_Site));
                xmlTime.Add(new XElement("Date", timeItem.Date.ToString("yyyy-MM-dd"))); // DateTime format: yyyy-MM-dd HH-mm-sszz
                xmlTime.Add(new XElement("Description", timeItem.Description));
                xmlTime.Add(new XElement("Hours", timeItem.Hours.ToString(System.Globalization.CultureInfo.CurrentCulture)));
                xmlTime.Add(new XElement("Project", timeItem.ProjectId));
                xmlTime.Add(new XElement("Project_Budget", timeItem.ProjectBudgetId));
                xmlTime.Add(new XElement("Check_out_Date_Time", timeItem.CheckOutDateTime.ToString("yyyy-MM-dd HH:mm:sszz")));
                xmlTime.Add(new XElement("Check_in_Date_Time", timeItem.CheckInDateTime.ToString("yyyy-MM-dd HH:mm:sszz")));

                xmlData.Add(xmlTime);

                var content = new StringContent("=" + HttpUtility.UrlEncode(xmlData.ToString()), Encoding.UTF8, "application/x-www-form-urlencoded"); // we could also use FormUrlEncodedContent
                var response = await Http.PostAsync(ServiceEndpoint + "/DataAction/Time?action=upsert&matchingFieldName=id", content);
                var responseStatusCode = response.StatusCode;
                var responseBody = await response.Content.ReadAsStringAsync();
                var apiResponse = XElement.Parse(responseBody);
                Console.WriteLine(" ### Saving Time Item PAYLOAD \n" + apiResponse + "\n ### ");

                timeItem.Id = apiResponse.Descendants("id").First().Value;

                if (string.IsNullOrEmpty(timeItem.Name))
                    timeItem.Name = apiResponse.Descendants("name").First().Value;

                if ((responseStatusCode != System.Net.HttpStatusCode.OK) || (apiResponse.Descendants("Success").First().Value == "False"))
                {
                    Console.WriteLine("ErrorMessage : " + apiResponse.Descendants("ErrorMessage").First().Value);
                    Console.WriteLine("Response Body : " + responseBody);
                    throw new Exception ("There was an issue saving this Time, the server replied with the following information -- Status code: " + responseStatusCode + " -- Error Message : " + apiResponse.Descendants("ErrorMessage").First().Value + " -- Please contact your administrator\n");
                }
                return timeItem;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
  • Delete a record (object : Time)
        public async Task<bool> DeleteTimeItem(Time timeItem)
        {
            try
            {
                var xmlData = new XElement("Data", "");
                var xmlTime = new XElement("Time", "");
                xmlTime.Add(new XElement("Id", timeItem.Id));
                xmlData.Add(xmlTime);

                var content = new StringContent("=" + HttpUtility.UrlEncode(xmlData.ToString()), Encoding.UTF8, "application/x-www-form-urlencoded"); // we could also use FormUrlEncodedContent
                var response = await Http.PostAsync(ServiceEndpoint + "/DataAction/Time?action=delete&matchingFieldName=id", content);
                Console.WriteLine("REST API DELETE call (careful with what you do on production data): " + response);

                var responseStatusCode = response.StatusCode;
                var responseBody = await response.Content.ReadAsStringAsync();

                if (responseStatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception("There was an issue deleting this Time, the server replied with the following error -- Status code: " + responseStatusCode + " -- Response body : " + responseBody + " -- Please contact your administrator\n");
                }

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

PHP

Here are some samples of using the REST API with PHP.

  • Create an Opportunity

This example previously used the legacy authToken flow. Authenticate now with OAuth 2.0 (Client Credentials flow) and include the access token in the Authorization: Bearer <access_token> header of each curl call (via CURLOPT_HTTPHEADER) rather than as a URL parameter. See 1. AuthToken — OAuth 2.0 Authentication for a full example of obtaining the token.

  • Select a Lead

This example previously used the legacy authToken flow. Authenticate now with OAuth 2.0 (Client Credentials flow) and include the access token in the Authorization: Bearer <access_token> header of each curl call (via CURLOPT_HTTPHEADER) rather than as a URL parameter. See 1. AuthToken — OAuth 2.0 Authentication for a full example of obtaining the token.

Was this article helpful to you? No Yes

How can we help?