API request with headers

I need to add an API key to my request but it returns an error. Do you know why I can’t use Headers?

var url = "https://api.iopool.com/v1/pools";
var headers = new Headers();

headers.append('x-api-key', 'myKey);

fetch(url, headers)
  .then(response => response.json())
  .then(data => console.log(data));

:x: Script Error
:warning: ReferenceError: Headers is not defined
at API Iopool.js:2:15
at processTicksAndRejections (node:internal/process/task_queues:96:5)

fetch(url, {
  headers : {
    'x-api-key': myKey

See last example here.

1 Like

Yes thank you ! There was one missing } :wink:

fetch(url, { headers : { 'x-api-key': apiKey }})
  .then(response => response.json())
  .then(data => console.log(data));

I am also trying to setup a script from my IoPool. Could you share your full script ? Mine is… not so efficient :slight_smile:

Hello, here is the script I use to do my tests. I haven’t made much progress on the application.

var url = "https://api.iopool.com/v1/pools/";
var apiKey = "yourApiKey";
var titre;
var temp;
var heureReleve;

fetch(url, { headers : { 'x-api-key': apiKey }})
  .then(response => 
  .then(data => {
    titre = data[0].title;
    temp = data[0].latestMeasure.temperature;
    heureReleve = data[0].latestMeasure.measuredAt;
    console.log(titre, 'n°',data[0].id);
    console.log('Temperature:',temp.toFixed(1), '°C');
    console.log('Dernier relevé le', heureReleve)