← Back to Course

🌐 APIs and External Data

An API lets one program ask another program for data or actions. Modern applications often depend on APIs to load information from services outside the app itself.

Using Fetch

Example: Load JSON from an API

async function loadPosts(): Promise<void> {
  const response = await fetch("https://jsonplaceholder.typicode.com/posts");
  const posts = await response.json();
  console.log(posts[0]);
}

loadPosts().catch((error) => {
  console.error("Failed to load posts", error);
});

What to Watch For

  • Network requests can fail
  • Responses often arrive as JSON
  • You usually need async/await to work with APIs cleanly
← Back to Code Foundations