Hey there, fellow JavaScript aficionado! Ready to supercharge your Fathom integration with some webhook magic? Let's dive right in and get those real-time updates flowing.
Before we start, make sure you've got:
If you're good to go, let's jump into the fun stuff!
First things first, we need to authenticate with the Fathom API. It's pretty straightforward:
const axios = require('axios'); const fathomApi = axios.create({ baseURL: 'https://api.fathom.com/v1', headers: { 'Authorization': `Bearer YOUR_API_KEY_HERE` } });
Replace YOUR_API_KEY_HERE
with your actual API key, and you're set!
Now, let's create a webhook. Here's how you do it:
async function createWebhook() { try { const response = await fathomApi.post('/webhooks', { url: 'https://your-app.com/webhook', events: ['pageview', 'goal_complete'] }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } } createWebhook();
Easy peasy, right? This sets up a webhook that'll ping your app whenever there's a pageview or a goal completion.
Fathom offers a bunch of event types you can listen to. We used 'pageview' and 'goal_complete' in the example above, but there are more:
pageview
goal_complete
campaign_view
campaign_conversion
Mix and match these in the events
array to get exactly what you need.
When Fathom sends a webhook, you'll get a juicy payload. Here's what it might look like:
{ "event": "pageview", "timestamp": "2023-06-15T14:30:00Z", "data": { "site_id": "ABCD1234", "page_url": "https://example.com/awesome-page", "referrer": "https://google.com", "unique_visitor": true } }
To handle this in your app, you might do something like:
app.post('/webhook', (req, res) => { const { event, data } = req.body; if (event === 'pageview') { // Do something with the pageview data console.log(`New pageview on ${data.page_url}`); } res.sendStatus(200); });
Security is crucial, folks! Fathom signs each webhook with a secret. Here's how you can verify it:
const crypto = require('crypto'); function verifyWebhookSignature(payload, signature, secret) { const hmac = crypto.createHmac('sha256', secret); const digest = hmac.update(payload).digest('hex'); return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest)); } app.post('/webhook', (req, res) => { const signature = req.headers['x-fathom-signature']; const isValid = verifyWebhookSignature(JSON.stringify(req.body), signature, 'your_webhook_secret'); if (!isValid) { return res.status(401).send('Invalid signature'); } // Process the webhook... });
Fathom provides some nifty tools for testing your webhooks. Use them! They're a lifesaver when you're trying to figure out why that darn webhook isn't firing.
If you're stuck, double-check your API key, webhook URL, and event types. And don't forget to check your server logs!
A few pro tips to keep in mind:
And there you have it! You're now armed with the knowledge to implement webhooks in Fathom like a pro. Remember, practice makes perfect, so don't be afraid to experiment and iterate.
Got questions? Hit up the Fathom API docs or jump into the community forums. Happy coding, and may your webhooks always fire true!