Back

Reading and Writing Data Using the Planning Center API

Aug 16, 20245 minute read

Hey there, fellow JavaScript devs! Ready to dive into the world of Planning Center API integration? Let's get our hands dirty with some code and learn how to sync data like pros.

Introduction

Planning Center's API is a powerful tool for integrating church management data into your applications. Whether you're building a custom dashboard or syncing with other systems, this API has got you covered. In this article, we'll focus on creating a user-facing integration that keeps data in sync efficiently.

Authentication

First things first, let's tackle authentication. Planning Center uses OAuth 2.0, so you'll need to set up your client credentials. Here's a quick example of how to obtain an access token:

const axios = require('axios'); async function getAccessToken(clientId, clientSecret, code) { const response = await axios.post('https://api.planningcenteronline.com/oauth/token', { grant_type: 'authorization_code', code, client_id: clientId, client_secret: clientSecret, }); return response.data.access_token; }

Reading Data

Now that we're authenticated, let's fetch some data. The API uses RESTful endpoints, so it's pretty straightforward. Here's how you might fetch a list of people:

async function getPeople(accessToken) { const response = await axios.get('https://api.planningcenteronline.com/people/v2/people', { headers: { Authorization: `Bearer ${accessToken}` }, }); return response.data.data; }

Don't forget to handle pagination! The API returns links for the next and previous pages in the response headers.

Writing Data

Writing data is just as easy. Let's create a new person:

async function createPerson(accessToken, personData) { const response = await axios.post('https://api.planningcenteronline.com/people/v2/people', { data: { type: 'Person', attributes: personData, }, }, { headers: { Authorization: `Bearer ${accessToken}` }, }); return response.data.data; }

Syncing Data

To keep your data fresh, you'll want to implement webhooks. Planning Center can send real-time updates to your application. Here's a basic Express.js webhook handler:

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 console.log('Received webhook:', event); res.sendStatus(200); });

Remember to respect rate limits! The API has a limit of 100 requests per minute per organization. Implement a queuing system if you're making lots of requests.

Best Practices

  1. Cache aggressively: Store frequently accessed data locally and update it periodically.
  2. Handle errors gracefully: Implement retry mechanisms for failed requests.
  3. Keep it in sync: Use a combination of webhooks and periodic full syncs to ensure data consistency.

Conclusion

There you have it! You're now equipped to build robust integrations with the Planning Center API. Remember, the key to a great integration is keeping the data fresh and handling edge cases smoothly.

Happy coding, and may your integrations be ever seamless! 🚀

For more details, check out the Planning Center API documentation. It's a goldmine of information that'll help you level up your integration game.