1

This is my second post here. The first one received no answer but I am this kind of stubborn guy looking to achieve his ideas. As mentioned in my profile I am a real beginner... I can teach you wheelchair tennis or basketball if you want, but nothing about coding...

Well, here I am with my problem:

I have multiple HTTP GET URL from this service

I would like to fetch each url in one zapier code step.

Zapier team told me :

I've pointed out a few things to fix below, but in the interest of full transparency, this Zap is going to be tough to make workable unless you are comfortable using a Code step. This Zap will return quite a bit of data and it appears the way you're using this a code step would work much better (ex. making HTTP calls using a For loop).

Well, here I am with this bit of code... No "for loop" yet... I still don't know how to write that.

Can you please give me advice based on this sample?

var url1 = 'https://triplogmileage.com/web/api/trips';
var url2 = 'https://triplogmileage.com/web/api/vehicules';
var url3 = 'https://triplogmileage.com/web/api/users';
var options = {
  method: 'GET',
  headers: {'Authorization': 'apikey 028cbda51a7c4a919546414e37f22298', 'Authorization': 'apikey 028cbda51a7c4a919546414e37f22298','Authorization': 'apikey 028cbda51a7c4a919546414e37f22298'}
};
fetch(url1,url2,url3,options)
.then(function(json) {
   var output = {};
    callback(null, output);
  })
.then(function(json) {
   var output = {};
    callback(null, output);
  })
.then(function(json) {
   var output = {};
    callback(null, output);
  })
.catch(callback);

When testing on zapier, no error appears but I dont have the data as json file.

Zapier after test says:

runtime_meta duration_ms 45 
memory_used_mb 75 
logs async true 
id 1JTWcHwBNqjR190PRJbg8QLiRIvk6QKh

Am I on the right way?

Thanks,

  • Do you have an API key? – Mike Loffland Jul 19 '18 at 18:43
  • I do. It is the same for each url. I have been able today to have the json file from one URL. I am the admin user in triplog and the api key in the sample above is : apikey 5c36c4ee66ba4c649c8a36abf2c2a906 (changed to another just after) . I just cant remember how I have been able to fetch data from "trips url" today... Spending a lot of time on this and beggining to have sparks in the eyes. – Thomas LIOT Jul 19 '18 at 18:57

1 Answers1

0

David here, from the Zapier Platform team. There's a couple of things to note here!

First off, I hope that's not your real API key. This is public, so it's like putting your password out on the open internet. If you can, reset or invalidate it ASAP.

For learning some fundamental javascript, I'd check out this: https://learnxinyminutes.com/docs/javascript. Though others could write the code for you, you understanding it will go a long way towards making it maintainable.

As for multiple urls, the way you're calling fetch is incorrect - it can only take a single url at a time. The official docs are here, with a helpful walkthrough here. With each fetch taking one url, you'll need 3 fetches to hit all 3 endpoints.

The easiest way to do this is using await, which our Code steps support now! There's a similar question about that here: How can I fetch an array of URLs with Promise.all?. You'll use await Promise.all and be able to get each of the 3 responses. There's also a pretty complete looking function here: https://gist.github.com/bschwartz757/5d1ff425767fdc6baedb4e5d5a5135c8

Your code as written doesn't work because of the misuse of fetch and the chain of promises that don't do what you expect. Hopefully the resources provided will point you in the right direction!

xavdid
  • 5,092
  • 3
  • 20
  • 32
  • Great! If this answer solved your problem, make sure to upvote it and mark it as correct ([info](https://stackoverflow.com/help/someone-answers)). – xavdid Jul 19 '18 at 19:52
  • 1
    Thanks a lot David, this will help! Don't worry, I changed the apikey everytime after publishing. I am aware about this security issue. I let the old API key in the code but changed for a new one instantly. I am learning here, what I did is grab pieces of code from here and there and tried tried tried. I really want to understand how things work, your answer is precious since it gives me some hints on how to make it work. Thanks a lot! Code and post soon to be updated! – Thomas LIOT Jul 19 '18 at 19:56
  • Glad to hear it! – xavdid Jul 19 '18 at 20:03