API GET or Zapier trigger to POST action

2k Views Asked by At

What i am essentially trying to do is generate an invoice in Harvest when a card is placed in a list in Trello. I have tried Zapier, but there is no invoice functionality built in to it.

I will need to develop this myself. Trello has a Javascript or Python action so i am limited to those two languages.

ZAPIER: Trello Trigger > Javascript or Python code

I have the JSON request that i need to be sent to

https://[site].harvestapp.com/invoices
Authorization: Basic amNtMjU4MkBnbWFpbC5jb206YTEwMDUw**NB
Content-Type: application/javascript
Accept: application/json

{ "invoice": { "due_at_human_format": "NET 10", "client_id": 3849315, "currency" : "United States Dollar - USD", "issued_at": "2015-04-22", "subject": "Your invoice subject goes here", "notes": "Some notes go here", "number": "303197", "kind": "project", "projects_to_invoice": "120353", "import_hours": "yes", "import_expense": "yes", "period_start": "2015-03-01", "period_end": "2016-03-31", "expense_period_start": "2015-03-31", "expense_period_end": "2016-03-31" } }

How can i post this in bare-bones python or JavaScript using basic login authentication including logic? A sample bit of code would be helpful.

UPDATE: I have added this code, but cannot seem to get it to work outside Postman

var data = JSON.stringify({
  "invoice": {
    "due_at_human_format": "NET 10",
    "client_id": 3849315,
    "currency": "United States Dollar - USD",
    "issued_at": "2015-04-22",
    "subject": "Your invoice subject goes here",
    "notes": "Some notes go here",
    "number": "303197",
    "kind": "project",
    "projects_to_invoice": "120353",
    "import_hours": "yes",
    "import_expense": "yes",
    "period_start": "2015-03-01",
    "period_end": "2016-03-31",
    "expense_period_start": "2015-03-31",
    "expense_period_end": "2016-03-31"
  }
});

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("POST", "https://[url].harvestapp.com/invoices");
xhr.setRequestHeader("authorization", "Basic amNtMjU4MkBnbWFpbC***TEwMDUwMTNB");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "2c652344-1be5-8969-adf3-a7ca9ee7179f");

xhr.send(data);

1

There are 1 best solutions below

1
On

Trello has a full fledged REST API, so you're not limited to just Python or JavaScript unless that's a limitation set by the Zap that you're using -- which is possible as Zapier is built mostly on Django.

With that being said, it's very easy to just fire off a POST request in either language. Here's a JavaScript example for you, though you'll have to adapt this to meet your specific needs.

var request = new XMLHttpRequest();
var url = 'https://[site].harvestapp.com/invoices'; // or whatever your url actually is
var headers = {
    Authorization: 'amNtMjU4MkBnbWFpbC5jb206YTEwMDUw**NB',
    Content-Type: 'application/javascript',
    Accept: 'application/json'
};
var params = { // your JSON encoded data };

request.open( 'POST', url, true );
request.setRequestHeader( headers );
request.send( params );

In Python it would look something like this:

import requests
url = 'https://[site].harvestapp.com/invoices'
request.headers = {
    'Authorization': 'amNtMjU4MkBnbWFpbC5jb206YTEwMDUw**NB',
    'Content-Type': 'application/javascript',
    'Accept': 'application/json'
}
data = {} # put your data there instead of an empty dictionary
request.post( url, data )