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.
Before we jump in, make sure you've got:
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
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: '' } });
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); } }
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 NPSLet'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); } }
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']); }
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 }]); });
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.
Now go forth and gather that valuable customer feedback! Happy coding!