API integration examples Free template

Fetch API client with error handling

A tiny, reusable JavaScript API client: JSON in and out, timeouts, readable errors and a simple page to try it.

About this template

A small wrapper around fetch that every API integration needs: it sends and receives JSON, adds a timeout so a slow server cannot hang your page, turns HTTP errors into real Error objects with the status and the server's message, and keeps the base URL and headers in one place. The page lets you type an address and see the result. Note: the playground preview blocks all network access on purpose, so run this template on your own computer (download it) to talk to a real API.

fetchapijsontimeouterror handling

Look under the hood

Included source files

851 bytes
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>API client</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <main>
    <h1>API client</h1>
    <p>Enter the path of a resource on the API below and press Get. Run this page from your own computer: browsers allow calls to public APIs that permit it.</p>
    <form id="form">
      <label for="base">API base address</label>
      <input id="base" value="https://jsonplaceholder.typicode.com">
      <label for="path">Path</label>
      <input id="path" value="/todos/1">
      <button type="submit">Get</button>
    </form>
    <p id="state" role="status"></p>
    <pre id="result"></pre>
  </main>
  <script src="api-client.js"></script>
  <script src="app.js"></script>
</body>
</html>

Update history

  1. 1.0.0

    First release.