Back

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

Aug 16, 20245 minute read

Introduction

Hey there, fellow developer! Ready to supercharge your customer feedback game? Let's dive into building a Delighted API integration in C#. Delighted's API is a powerhouse for gathering and analyzing customer feedback, and we're about to harness that power in our C# application. Buckle up!

Prerequisites

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

  • Visual Studio or your favorite C# IDE
  • .NET Core 3.1 or later
  • A Delighted API key (if you don't have one, grab it from your Delighted dashboard)

Setting up the project

Let's get our hands dirty:

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

Authentication

Time to get cozy with the Delighted API:

private const string ApiKey = "your_api_key_here"; private readonly RestClient _client; public DelightedApiClient() { _client = new RestClient("https://api.delighted.com/v1/"); _client.Authenticator = new HttpBasicAuthenticator(ApiKey, ""); }

Implementing core API functionalities

Sending surveys

Let's spread some survey love:

public async Task SendSurvey(string email) { var request = new RestRequest("people.json", Method.POST); request.AddParameter("email", email); var response = await _client.ExecuteAsync(request); // Handle response }

Retrieving survey responses

Time to see what your customers think:

public async Task<List<SurveyResponse>> GetResponses() { var request = new RestRequest("survey_responses.json", Method.GET); var response = await _client.ExecuteAsync<List<SurveyResponse>>(request); return response.Data; }

Managing people

Add, update, or bid farewell to your survey recipients:

public async Task AddPerson(string email) { var request = new RestRequest("people.json", Method.POST); request.AddParameter("email", email); await _client.ExecuteAsync(request); } public async Task DeletePerson(string email) { var request = new RestRequest($"people/{email}.json", Method.DELETE); await _client.ExecuteAsync(request); }

Handling webhooks

Stay in the loop with real-time updates:

[HttpPost("webhook")] public IActionResult HandleWebhook([FromBody] WebhookPayload payload) { // Process the webhook payload return Ok(); }

Error handling and best practices

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

try { // API call here } catch (HttpRequestException ex) { // Implement retry logic }

And remember, play nice with rate limits. Nobody likes a spammer!

Testing the integration

Let's make sure everything's ship-shape:

[Fact] public async Task SendSurvey_ShouldReturnSuccessStatusCode() { var client = new DelightedApiClient(); var response = await client.SendSurvey("[email protected]"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }

Extending the integration

Now that you've got the basics down, why not flex those coding muscles? Try customizing survey questions or diving into trends and metrics. The sky's the limit!

Conclusion

And there you have it! You've just built a rock-solid Delighted API integration in C#. Remember, this is just the beginning. Keep exploring the API docs, experiment with different endpoints, and most importantly, have fun with it!

Happy coding, and may your NPS scores be ever in your favor! 🚀