Contents Lesson 11 of 21

11 min read · foundations

Your first request, and the data that comes back

By the end of this lesson you will have fetch.js, a few lines that ask a real server for Apple's daily prices and print the status, the number of bars and the first bar — and you will have seen it fail in three different ways.

Everything so far comes together in about eight lines: ask a real server for real prices and print them.

The code

// fetch.js — run with: node fetch.js
const url = "https://eodhd.com/api/eod/AAPL.US?api_token=demo&fmt=json&from=2026-08-20";

const res = await fetch(url);
console.log(res.status);          // 200, you hope

const bars = await res.json();    // text -> array of objects
console.log(bars.length);
console.log(bars[0]);

Every piece is something you have met: a URL with a query, a status code, JSON becoming objects, an array indexed from zero.

await, and why it exists

A request goes over the internet and takes time. await means wait here for the answer before continuing. Without it you get a Promise — a placeholder for an answer that has not arrived — and printing one shows Promise { <pending> } rather than your data.

That is a confusing first sight, and now it will not be: Promise { <pending> } means a missing await.

The check that matters

if (!res.ok) {
  console.log("request failed:", res.status);
} else {
  const bars = await res.json();
  console.log(bars.length);
}

The else is doing real work: it is what stops res.json() running on a response you already know is an error.

fetch does not throw on a 404 or a 429 — those are successful conversations in which the server said no. Only a network failure throws. So code without an res.ok check happily tries to read prices out of an error message.

Build Your First Tool finds this exact omission in generated code, and Backtest Strategies finds a 404 whose body is not even JSON, so res.json() throws somewhere you were not expecting.

About api_token=demo

That is a shared demonstration key, good for a few tickers and not yours. Build Your First Tool has you get your own free key and — this is the point of the previous lesson — put it somewhere the browser never sees.

Try it now

Type the code above into fetch.js and run it with node fetch.js. Then replace the last four lines (from const bars down) with the checked version from "The check that matters", so that one file both prints the status and refuses to parse an error. That is the file you commit.

If Node prints a warning about MODULE_TYPELESS_PACKAGE_JSON or "Reparsing as ES module" above your output, it is not an error: there is a package.json in a folder above yours, and Node is telling you it guessed the file type. The await at the top of the file is what triggers the guess. Your output follows the warning.

The prompt

Type fetch.js yourself rather than pasting it — copying by hand is how you read every character — and run it. Then paste it into your chat assistant:

I typed this script by hand from a lesson. Do not rewrite it. Walk me
through what happens, line by line, when I run node fetch.js: which
lines wait for the network, and what each console.log prints. Then
tell me what it would print if the server answered 403, without
running anything.

[paste fetch.js]

A good answer names both await lines as the waits, and says what a 403 does to your version: the status prints, and then, unless something checks res.ok first, res.json() throws on a body that is not JSON.

Check it yourself

  1. The first line printed is 200.
  2. The second is the number of trading days from 20 August 2026 to the latest close. It grows by one each trading day: 55 when we ran it on 6 November 2026.
  3. The run spent nothing: demo is a public token no account is charged for. With your own key from Build Your First Tool, the same request costs one call of a free plan's twenty a day.
  4. The third line is the bar for 20 August 2026. Measured with the demo token on 6 November 2026, the server sent:
date open high low close adjusted_close volume
2026-08-20 317.455 320.28 310.65 311.3 311.3 40959184

Break it on purpose

Break it three ways, putting the file back to working order after each. First, misspell the ticker and read the status. Second, remove the await from the const res = await fetch(url); line and change console.log(res.status); to console.log(res);: you will see Promise { <pending> }, a placeholder for an answer that has not arrived. The next line fails too, because a placeholder has no status and no .json: your checked version prints request failed: undefined, and the first version stops with TypeError: res.json is not a function. Both come from the one missing await. Third, turn off your wifi to see what a real throw looks like. Three deliberate failures teach more than one success.

So you know the first one when you see it: with the demo key and APPL typed for AAPL in the URL, the script printed these two lines on 28 September 2026 (Node prints the offending body first, then a caret under it, then the SyntaxError line, then the stack trace; on Windows it may also end with an Assertion failed ... async.c line, which is a glitch in how Node exits and not something you did), from the first version of the script, the one without the res.ok check:

403
SyntaxError: Unexpected token 'F', "Forbidden" is not valid JSON

A 403 rather than a 404, because the demo key refuses any symbol it does not cover before looking it up, and a throw on the next line, because the body is the plain word Forbidden. That second line is what the res.ok check exists to prevent.

Compare with ours

Our fetch.js, with the check folded in
// fetch.js — run with: node fetch.js
const url = "https://eodhd.com/api/eod/AAPL.US?api_token=demo&fmt=json&from=2026-08-20";

const res = await fetch(url);
console.log(res.status);

if (!res.ok) {
  console.log("request failed:", res.status);
} else {
  const bars = await res.json();
  console.log(bars.length);
  console.log(bars[0]);
}

The misspelled ticker now prints 403 twice and stops, instead of throwing on a body that was never JSON.

Checkpoint

node fetch.js
git status   # only the files you expect, and no .env*
git add -A
git commit -m "fetch.js: first real request, status checked before the body is parsed"

Then answer this lesson's questions.