Hey there, fellow JavaScript dev! Ready to supercharge your Freelancer integration with webhooks? Let's dive right in and get those real-time updates flowing!
Webhooks are like the cool kids of API integrations - they notify you instantly when something interesting happens on Freelancer. No more constant polling or refreshing. The Freelancer API's got your back with some nifty webhook capabilities, so let's make the most of them!
Before we jump in, make sure you've got:
First things first, let's create a simple Express server to catch those webhook events:
const express = require('express'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { console.log('Webhook received:', req.body); res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Boom! You've got a basic webhook receiver up and running. Easy peasy, right?
Now, let's tell Freelancer where to send those juicy updates:
Here's a quick snippet to set up a webhook subscription programmatically:
const axios = require('axios'); axios.post('https://www.freelancer.com/api/webhooks/0.1/webhooks/', { events: ['project.awarded', 'bid.placed'], url: 'https://your-awesome-app.com/webhook', status: 'active' }, { headers: { 'Freelancer-OAuth-V1': 'your-oauth-token' } }) .then(response => console.log('Webhook configured:', response.data)) .catch(error => console.error('Oops!', error));
When those webhooks start rolling in, you'll want to handle them like a pro:
app.post('/webhook', (req, res) => { const signature = req.headers['freelancer-signature']; if (verifySignature(req.body, signature)) { // Process the webhook payload handleWebhookEvent(req.body); res.sendStatus(200); } else { res.sendStatus(403); } }); function verifySignature(payload, signature) { // Implement signature verification logic here // Return true if valid, false otherwise } function handleWebhookEvent(payload) { switch(payload.event_type) { case 'project.awarded': // Handle project award break; case 'bid.placed': // Handle new bid break; // Add more cases as needed } }
Freelancer's got a bunch of cool events you can listen for. Some fan favorites:
project.awarded
: Cha-ching! Time to celebrate a new project win.bid.placed
: Keep an eye on the competition with new bid alerts.message.posted
: Never miss a client message again.Sometimes things go sideways. No worries, we've got you covered:
app.post('/webhook', async (req, res) => { try { // Process webhook await processWebhook(req.body); res.sendStatus(200); } catch (error) { console.error('Webhook processing failed:', error); res.status(500).json({ error: 'Internal server error' }); // Implement retry logic here } });
Testing locally? ngrok is your new best friend. It'll give you a public URL to use for webhook testing. Keep an eye on the Freelancer API dashboard for webhook activity, and you'll be squashing bugs in no time!
A few pro tips to keep your webhook game strong:
And there you have it! You're now a Freelancer webhook wizard. Remember, the key to great integrations is staying on top of those real-time events. So go forth and webhook all the things!
Need more info? The Freelancer API docs are a goldmine of webhook goodness. Happy coding, and may your integrations be ever seamless!