Back

Step by Step Guide to Building a SafetyCulture API Integration in C#

Aug 17, 20247 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of SafetyCulture API integration? You're in for a treat. This guide will walk you through building a robust integration in C#, allowing you to tap into SafetyCulture's powerful features. Let's get cracking!

Prerequisites

Before we jump in, make sure you've got:

  • Visual Studio or your favorite C# IDE
  • .NET Core 3.1 or later
  • A SafetyCulture account with API access

Got your API key handy? Great! If not, hop over to your SafetyCulture account settings and grab one. We'll need it soon.

Setting up the project

Fire up Visual Studio and create a new C# console application. We'll keep it simple for now, but feel free to adapt this to your specific project needs.

Next, let's grab the necessary NuGet packages:

Install-Package Newtonsoft.Json
Install-Package RestSharp

These will make our lives easier when dealing with HTTP requests and JSON parsing.

Authenticating with the API

Alright, let's get that authentication sorted. Create a new class called SafetyCultureClient:

public class SafetyCultureClient { private readonly RestClient _client; private const string BaseUrl = "https://api.safetyculture.io/"; public SafetyCultureClient(string apiKey) { _client = new RestClient(BaseUrl); _client.AddDefaultHeader("Authorization", $"Bearer {apiKey}"); } // We'll add more methods here soon! }

This sets up our base client with the API key authentication. Easy peasy!

Making API requests

Now for the fun part - let's make some API calls! Add these methods to your SafetyCultureClient class:

public async Task<string> GetInspections() { var request = new RestRequest("inspections/search", Method.GET); var response = await _client.ExecuteAsync(request); return response.Content; } public async Task<string> CreateInspection(string templateId) { var request = new RestRequest("inspections", Method.POST); request.AddJsonBody(new { template_id = templateId }); var response = await _client.ExecuteAsync(request); return response.Content; }

These methods will fetch inspections and create a new one, respectively.

Parsing and processing data

Time to make sense of that JSON data. Let's create some models:

public class Inspection { public string Id { get; set; } public string TemplateId { get; set; } public string Status { get; set; } // Add more properties as needed } public class InspectionList { public List<Inspection> Inspections { get; set; } }

Now we can deserialize our API responses:

var inspections = JsonConvert.DeserializeObject<InspectionList>(jsonResponse);

Implementing key features

Let's put it all together and implement some key features:

public async Task ListInspections() { var jsonResponse = await GetInspections(); var inspections = JsonConvert.DeserializeObject<InspectionList>(jsonResponse); foreach (var inspection in inspections.Inspections) { Console.WriteLine($"Inspection ID: {inspection.Id}, Status: {inspection.Status}"); } } public async Task CreateNewInspection(string templateId) { var jsonResponse = await CreateInspection(templateId); var newInspection = JsonConvert.DeserializeObject<Inspection>(jsonResponse); Console.WriteLine($"Created new inspection with ID: {newInspection.Id}"); }

Error handling and best practices

Don't forget to implement proper error handling:

try { // Your API call here } catch (HttpRequestException ex) { Console.WriteLine($"API request failed: {ex.Message}"); }

Consider implementing retry logic for transient failures and respect rate limits to be a good API citizen.

Testing the integration

Last but not least, let's write a simple test:

[Fact] public async Task GetInspections_ReturnsValidResponse() { var client = new SafetyCultureClient("your-api-key"); var response = await client.GetInspections(); Assert.NotNull(response); Assert.Contains("inspections", response); }

Conclusion

And there you have it! You've just built a solid foundation for your SafetyCulture API integration in C#. From here, you can expand on this base, adding more features and fine-tuning your implementation.

Remember, the SafetyCulture API is powerful and flexible, so don't be afraid to explore and experiment. Happy coding, and may your integrations be ever smooth and bug-free!