Fetch One Country's Holidays

In the first lesson, curl showed you the raw exchange for Portugal’s 2026 holidays: a status line reading HTTP/1.1 200 OK, a Content-Type header, and a body of JSON text. The planner can’t run curl for every friend, though. It has to send the same request from JavaScript and read the same parts of the answer.

You may have fetched data in a script before with two await expressions and logged what came back. In this lesson, you’ll send the planner’s first real request that way, then read the response’s status code, its ok flag, and its headers, and match each one to a line curl printed. You’ll also load the saved file from the first lesson when the server doesn’t answer.

Send the Planner’s First Request

curl printed Portugal’s holidays on your screen, but the planner needs them inside a running program. The function that sends a request to a server from JavaScript is fetch(), which is built into Node.js and every browser. You give it a URL, and it returns a promise. Save this as holidays.mjs in your planner folder:

Language: JavaScript Filename: holidays.mjs
const url =
  "https://date.nager.at/api/v4/Holidays/PT/2026";

const response = await fetch(url);
const holidays = await response.json();

console.log(holidays.length, "holidays");
console.log(holidays[0].date, holidays[0].name);

The .mjs extension makes the file an ES module, so await works at its top level. Run it, and Nager.Date answers:

Language: Shell
$ node holidays.mjs
17 holidays
2026-01-01 New Year's Day

This output is from a run on November 27, 2026, and it changes only if Nager.Date updates Portugal’s list. Here’s the two-await pattern again, with this course’s request:

  • The first await waits for fetch(url). With only a URL, fetch() sends a GET request, as curl did. The promise fulfills with an object that represents the server’s answer, called a Response.
  • The second await waits for response.json(). That method reads the body, which is still text, and parses it as JSON, the same job JSON.parse() did in the REPL in the first lesson. Reading the body takes time too, so response.json() returns another promise.

Only then is holidays an array you can index. Notice also that the URL is complete, origin included. In a browser, fetch() can take a bare path and resolve it against the page’s address. A Node.js script has no page, so with a path such as "/api/v4/Holidays/PT/2026", fetch() still returns a promise, but that promise rejects with a TypeError whose message starts Failed to parse URL. The first await is where the error is thrown.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.