Automate invoice creation in Harvest from Trello card movement

Hey everyone, I’m trying to set up a system where an invoice is automatically generated in Harvest when a card is moved to a specific list in Trello. I’ve looked into Zapier, but it doesn’t have built-in invoice functionality for this. I’m thinking of coding this myself using either JavaScript or Python since those are the options available in Trello’s actions. I’ve got the JSON request ready, but I’m stuck on how to actually send it to Harvest’s API.

Here’s a simplified version of what I’m trying to do:

function createInvoice(cardData) {
  const invoiceDetails = {
    clientId: 12345,
    dueDate: 'NET 15',
    currency: 'USD',
    subject: `Invoice for ${cardData.name}`,
    projectId: 67890
  };

  // Need help with this part
  // How to send POST request to Harvest API?
  // Using basic auth and handling the response
}

Can anyone help me figure out how to make this POST request work? I’ve tried using XMLHttpRequest, but I can’t get it to function outside of Postman. Any tips or sample code would be super helpful. Thanks!

I’ve implemented a similar automation using Python and the requests library. It’s quite straightforward and reliable. Here’s a basic example:

import requests

def create_invoice(card_data):
    url = 'https://api.harvestapp.com/v2/invoices'
    headers = {
        'Authorization': 'Bearer YOUR_TOKEN',
        'Harvest-Account-ID': 'YOUR_ACCOUNT_ID',
        'Content-Type': 'application/json'
    }
    response = requests.post(url, json=invoiceDetails, headers=headers)
    return response.json()

You’ll need to replace ‘YOUR_TOKEN’ and ‘YOUR_ACCOUNT_ID’ with your actual Harvest credentials. This method has worked well for me in production. Remember to handle potential API errors and rate limits in your implementation.

hey, i did this before. try the fetch api:

fetch('https://api.harvestapp.com/v2/invoices', {method:'POST', headers:{'Authorization':'Bearer TOKEN','Content-Type':'application/json'}, body: JSON.stringify(invoiceDetails)})

hope it helps, LMK.

I’ve actually tackled this exact problem before, and I found that using Node.js with the ‘axios’ library worked wonders. It’s super clean and handles promises nicely. Here’s a quick snippet that might help:

const axios = require('axios');

async function createInvoice(cardData) {
  try {
    const response = await axios.post('https://api.harvestapp.com/v2/invoices', invoiceDetails, {
      headers: {
        'Authorization': 'Bearer YOUR_TOKEN',
        'Harvest-Account-ID': 'YOUR_ACCOUNT_ID',
        'Content-Type': 'application/json'
      }
    });
    console.log('Invoice created:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error creating invoice:', error.response.data);
    throw error;
  }
}

Just make sure to npm install axios first. This approach has been rock-solid for me in production. It handles errors gracefully and the async/await syntax makes it easy to integrate with your Trello webhook or whatever trigger you’re using. Good luck with your automation!