Back

Step by Step Guide to Building a Delighted API Integration in JS

Aug 16, 20245 minute read

Introduction

Hey there, fellow code wrangler! Ready to dive into the world of customer feedback? Let's talk Delighted API. This nifty tool lets you programmatically send surveys and gather insights. In this guide, we'll walk through building a solid integration that'll have you collecting and analyzing feedback like a pro.

Prerequisites

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

  • Node.js and npm (you're probably nodding already)
  • A Delighted API key (grab one from your account if you haven't)
  • Axios or your favorite HTTP client (we'll use Axios in our examples)

Setting up the project

Let's get our project off the ground:

mkdir delighted-integration cd delighted-integration npm init -y npm install axios dotenv

Create a .env file for your API key:

DELIGHTED_API_KEY=your_api_key_here

Authentication

Delighted uses API key authentication. Let's set that up:

require('dotenv').config(); const axios = require('axios'); const delightedApi = axios.create({ baseURL: 'https://api.delighted.com/v1', auth: { username: process.env.DELIGHTED_API_KEY, password: '' } });

Making API requests

Now we're cooking! Here's how to make a basic GET request:

async function getSurveys() { try { const response = await delightedApi.get('/survey_responses.json'); return response.data; } catch (error) { console.error('Error fetching surveys:', error.response.data); } }

Key API endpoints

You'll be working with these endpoints a lot:

  • /survey_responses.json - List survey responses
  • /people.json - Create survey invitations
  • /metrics.json - Retrieve metrics like NPS

Implementing core functionalities

Let's implement some key features:

// Send a survey invitation async function sendSurveyInvitation(email) { try { const response = await delightedApi.post('/people.json', { email }); return response.data; } catch (error) { console.error('Error sending invitation:', error.response.data); } } // Retrieve NPS score async function getNpsScore() { try { const response = await delightedApi.get('/metrics.json'); return response.data.nps; } catch (error) { console.error('Error fetching NPS:', error.response.data); } }

Error handling and best practices

Remember to handle rate limiting and check for specific error codes:

if (error.response && error.response.status === 429) { console.log('Rate limit reached. Retry after:', error.response.headers['retry-after']); }

Testing the integration

Don't forget to test! Here's a quick example using Jest:

jest.mock('axios'); test('getSurveys returns survey data', async () => { axios.get.mockResolvedValue({ data: [{ id: 1, score: 9 }] }); const surveys = await getSurveys(); expect(surveys).toEqual([{ id: 1, score: 9 }]); });

Conclusion

And there you have it! You've just built a solid Delighted API integration. You're now armed with the power to programmatically send surveys, gather responses, and calculate NPS scores. As you get more comfortable, explore advanced features like trend analysis and custom survey questions.

Resources

Now go forth and gather that valuable customer feedback! Happy coding!