Back

Reading and Writing Data Using the Hubspot Ticketing API

Aug 9, 2024 • 6 minute read

Hey there, fellow JavaScript aficionados! Ready to dive into the world of Hubspot Ticketing API? Let's get our hands dirty with some data syncing for user-facing integrations. Buckle up!

Setting the Stage

First things first, let's get our environment primed. You'll need to install the Hubspot client library:

npm install @hubspot/api-client

Now, let's set up those API credentials:

const hubspot = require('@hubspot/api-client'); const hubspotClient = new hubspot.Client({ accessToken: 'YOUR_ACCESS_TOKEN' });

Fetching Those Tickets

Time to grab some ticket data! Here's a quick way to fetch tickets:

async function getTickets() { const limit = 100; const after = undefined; const properties = ['subject', 'content', 'hs_pipeline_stage']; try { const apiResponse = await hubspotClient.crm.tickets.basicApi.getPage(limit, after, properties); return apiResponse.results; } catch (e) { console.error(e); } }

Pro tip: Don't forget to handle pagination for large datasets!

Creating and Updating Tickets

Now, let's create a new ticket:

async function createTicket(ticketData) { try { const apiResponse = await hubspotClient.crm.tickets.basicApi.create({ properties: ticketData }); return apiResponse; } catch (e) { console.error(e); } }

Updating is just as easy:

async function updateTicket(ticketId, updateData) { try { const apiResponse = await hubspotClient.crm.tickets.basicApi.update(ticketId, { properties: updateData }); return apiResponse; } catch (e) { console.error(e); } }

Real-time Sync Magic

Want to keep things fresh? Set up a webhook listener:

const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const { objectId, propertyName, propertyValue } = req.body; // Process the webhook data console.log(`Ticket ${objectId} updated: ${propertyName} = ${propertyValue}`); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook listener running on port 3000'));

Syncing Smarter, Not Harder

For efficient syncing, use the lastmodifieddate property:

async function getRecentlyModifiedTickets(since) { const filter = { propertyName: 'lastmodifieddate', operator: 'GTE', value: since }; const filterGroup = { filters: [filter] }; const sort = JSON.stringify({ propertyName: 'lastmodifieddate', direction: 'DESCENDING' }); const properties = ['subject', 'content', 'hs_pipeline_stage']; const limit = 100; try { const apiResponse = await hubspotClient.crm.tickets.searchApi.doSearch({ filterGroups: [filterGroup], sorts: [sort], properties, limit }); return apiResponse.results; } catch (e) { console.error(e); } }

Handling Hiccups and Staying in Line

Always be prepared for API hiccups:

async function resilientApiCall(apiFunction, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await apiFunction(); } catch (error) { if (error.response && error.response.status === 429) { const retryAfter = error.response.headers['retry-after'] || 1; await new Promise(resolve => setTimeout(resolve, retryAfter * 1000)); } else if (i === maxRetries - 1) { throw error; } } } }

Testing, 1-2-3

Don't forget to test! Here's a quick Jest test to get you started:

jest.mock('@hubspot/api-client'); test('getTickets returns ticket data', async () => { const mockTickets = [{ id: '1', properties: { subject: 'Test Ticket' } }]; hubspotClient.crm.tickets.basicApi.getPage.mockResolvedValue({ results: mockTickets }); const result = await getTickets(); expect(result).toEqual(mockTickets); });

Turbocharging Performance

Implement a simple cache to reduce API calls:

const NodeCache = require('node-cache'); const cache = new NodeCache({ stdTTL: 300 }); // 5 minutes TTL async function getCachedTicket(ticketId) { const cachedTicket = cache.get(ticketId); if (cachedTicket) return cachedTicket; const ticket = await hubspotClient.crm.tickets.basicApi.getById(ticketId); cache.set(ticketId, ticket); return ticket; }

Wrapping Up

There you have it, folks! You're now armed with the knowledge to sync Hubspot ticket data like a pro. Remember to keep your code clean, your errors handled, and your API calls efficient. Happy coding!