Back

Reading and Writing Data Using the SimplyBook.me API

Aug 17, 20246 minute read

Hey there, fellow JavaScript enthusiasts! Ready to dive into the world of SimplyBook.me API integration? Let's roll up our sleeves and get our hands dirty with some code!

Introduction

SimplyBook.me's API is a powerful tool that lets you tap into their booking system. Whether you're building a custom interface or syncing data with your app, this API has got you covered. We'll focus on creating a seamless user-facing integration that keeps everything in sync.

Authentication: Your VIP Pass

First things first – let's get you authenticated. You'll need API credentials from SimplyBook.me. Once you've got those, here's how you can implement the OAuth 2.0 flow:

const axios = require('axios'); async function getAccessToken(clientId, clientSecret) { const response = await axios.post('https://user-api.simplybook.me/login', { company: 'your_company', login: clientId, password: clientSecret }); return response.data.token; }

Keep this token handy – you'll need it for all your API requests!

Reading Data: Get What You Need

Now that you're in, let's fetch some data. Here's how you can grab available services and bookings:

async function getServices(token) { const response = await axios.get('https://user-api.simplybook.me/admin/services', { headers: { 'X-Company-Login': 'your_company', 'X-Token': token } }); return response.data; } async function getBookings(token, startDate, endDate) { const response = await axios.get(`https://user-api.simplybook.me/admin/bookings?startDate=${startDate}&endDate=${endDate}`, { headers: { 'X-Company-Login': 'your_company', 'X-Token': token } }); return response.data; }

Writing Data: Make Your Mark

Time to create and update bookings. Check this out:

async function createBooking(token, bookingData) { const response = await axios.post('https://user-api.simplybook.me/admin/bookings', bookingData, { headers: { 'X-Company-Login': 'your_company', 'X-Token': token } }); return response.data; } async function updateBooking(token, bookingId, updateData) { const response = await axios.put(`https://user-api.simplybook.me/admin/bookings/${bookingId}`, updateData, { headers: { 'X-Company-Login': 'your_company', 'X-Token': token } }); return response.data; }

Syncing Data: Stay in the Loop

To keep everything up-to-date, set up a webhook listener. Here's a basic Express.js implementation:

const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const event = req.body; // Handle the event based on its type switch (event.type) { case 'booking.created': // Handle new booking break; case 'booking.updated': // Handle booking update break; // Add more cases as needed } res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook listener running on port 3000'));

Error Handling and Rate Limiting: Play Nice

Always be prepared for errors and respect those rate limits! Here's a simple retry mechanism:

async function makeApiCall(fn, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await fn(); } catch (error) { if (error.response && error.response.status === 429) { // Rate limited, wait before retrying await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))); } else if (i === maxRetries - 1) { throw error; // Max retries reached, rethrow the error } } } }

Best Practices: The Cherry on Top

  1. Cache frequently accessed data to reduce API calls.
  2. Use batch operations when possible to minimize requests.
  3. Always encrypt sensitive data before storing or transmitting.

Wrapping Up

And there you have it! You're now equipped to build a robust SimplyBook.me integration. Remember, the key to a great user-facing app is keeping everything in sync and handling errors gracefully.

Feel free to explore more endpoints and functionalities in the SimplyBook.me API documentation. Happy coding, and may your bookings always be in sync!