Hey there, fellow code wrangler! Ready to dive into the world of Zoho Invoice API integration? Buckle up, because we're about to embark on a journey that'll supercharge your invoicing game. This guide will walk you through creating a robust integration that'll have you manipulating invoices like a pro.
Before we jump in, make sure you've got:
Let's get this party started:
mkdir zoho-invoice-integration cd zoho-invoice-integration npm init -y npm install axios dotenv
Zoho uses OAuth 2.0, so let's tackle that first:
const axios = require('axios'); require('dotenv').config(); async function getAccessToken() { const response = await axios.post('https://accounts.zoho.com/oauth/v2/token', null, { params: { grant_type: 'refresh_token', client_id: process.env.CLIENT_ID, client_secret: process.env.CLIENT_SECRET, refresh_token: process.env.REFRESH_TOKEN } }); return response.data.access_token; }
Pro tip: Store your credentials in a .env
file and never commit it to version control. Your future self will thank you!
Time to create our API client:
const baseURL = 'https://invoice.zoho.com/api/v3'; async function createApiClient() { const accessToken = await getAccessToken(); return axios.create({ baseURL, headers: { 'Authorization': `Zoho-oauthtoken ${accessToken}` } }); }
Now for the fun part - let's play with some invoices:
async function fetchInvoices() { const client = await createApiClient(); const response = await client.get('/invoices'); return response.data; } async function createInvoice(invoiceData) { const client = await createApiClient(); const response = await client.post('/invoices', invoiceData); return response.data; } async function updateInvoice(invoiceId, updateData) { const client = await createApiClient(); const response = await client.put(`/invoices/${invoiceId}`, updateData); return response.data; } async function deleteInvoice(invoiceId) { const client = await createApiClient(); const response = await client.delete(`/invoices/${invoiceId}`); return response.data; }
Don't let those pesky errors catch you off guard:
async function apiRequest(method, url, data = null) { const client = await createApiClient(); try { const response = await client[method](url, data); return response.data; } catch (error) { if (error.response && error.response.status === 429) { // Handle rate limiting console.log('Rate limit hit. Retrying after cooldown...'); await new Promise(resolve => setTimeout(resolve, 60000)); return apiRequest(method, url, data); } throw error; } }
Want to stay in the loop? Set up a webhook listener:
const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const event = req.body; console.log('Received webhook event:', event); // Process the event res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook listener running on port 3000'));
Don't forget to test your code! Here's a quick example using Jest:
const { fetchInvoices } = require('./zohoApi'); test('fetchInvoices returns array of invoices', async () => { const invoices = await fetchInvoices(); expect(Array.isArray(invoices)).toBe(true); expect(invoices.length).toBeGreaterThan(0); });
And there you have it! You're now armed with the knowledge to build a killer Zoho Invoice API integration. Remember, the API is your playground - don't be afraid to explore and experiment. Happy coding!
For the complete code and more examples, check out my GitHub repo: zoho-invoice-integration
Now go forth and invoice like a boss! 💪🚀