Back

Step by Step Guide to Building a Setmore Appointments API Integration in C#

Aug 16, 20245 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your app with appointment scheduling capabilities? Let's dive into integrating the Setmore Appointments API into your C# project. This powerful API will let you manage bookings like a pro, and I'm here to walk you through it.

Prerequisites

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

  • Visual Studio (or your favorite C# IDE)
  • .NET Core 3.1 or later
  • A Setmore account with API credentials (if you don't have one, go grab it!)

Setting up the project

Let's get our hands dirty:

  1. Fire up Visual Studio and create a new C# project.
  2. Install these NuGet packages:
    Install-Package Newtonsoft.Json
    Install-Package RestSharp
    

Authentication

First things first, let's get you authenticated:

using RestSharp; using Newtonsoft.Json.Linq; public class SetmoreAuth { private const string BASE_URL = "https://developer.setmore.com/api/v1"; private string refreshToken; public string GetAccessToken() { var client = new RestClient(BASE_URL); var request = new RestRequest("/o/oauth2/token", Method.POST); request.AddParameter("refreshToken", refreshToken); var response = client.Execute(request); var json = JObject.Parse(response.Content); return json["data"]["token"]["access_token"].ToString(); } }

Pro tip: Implement a token refresh mechanism to keep your access fresh!

Basic API requests

Now that we're authenticated, let's make some requests:

public class SetmoreClient { private readonly RestClient client; private readonly string accessToken; public SetmoreClient(string accessToken) { client = new RestClient(BASE_URL); this.accessToken = accessToken; } public IRestResponse Get(string endpoint) { var request = new RestRequest(endpoint, Method.GET); request.AddHeader("Authorization", $"Bearer {accessToken}"); return client.Execute(request); } // Implement Post, Put, and Delete methods similarly }

Implementing key features

Let's put our client to work:

public class AppointmentManager { private readonly SetmoreClient client; public AppointmentManager(SetmoreClient client) { this.client = client; } public List<TimeSlot> GetAvailableTimeSlots(string staffKey, DateTime date) { var response = client.Get($"/bookings/slots?staff_key={staffKey}&date={date:yyyy-MM-dd}"); // Parse response and return time slots } public Appointment CreateAppointment(AppointmentDetails details) { var response = client.Post("/bookings", details); // Parse response and return created appointment } // Implement UpdateAppointment and CancelAppointment methods }

Error handling and logging

Don't let those pesky errors catch you off guard:

try { // Your API call here } catch (Exception ex) { Logger.LogError($"API call failed: {ex.Message}"); // Handle the error gracefully }

Testing the integration

Time to put our code through its paces:

[TestMethod] public void TestGetAvailableTimeSlots() { var manager = new AppointmentManager(new SetmoreClient(accessToken)); var slots = manager.GetAvailableTimeSlots("STAFF_KEY", DateTime.Today); Assert.IsNotNull(slots); Assert.IsTrue(slots.Count > 0); }

Best practices and optimization

Remember to:

  • Implement rate limiting to play nice with Setmore's API
  • Cache frequently accessed data to reduce API calls

Conclusion

And there you have it! You've just built a solid Setmore Appointments API integration in C#. With this foundation, you can expand and customize to your heart's content. Keep exploring the API docs for more features to implement.

Resources

Now go forth and schedule those appointments like a boss! Happy coding!