Module 6 β External API Requests
Fetch real data from the internet and render it in your pages.
Overview π
APIs (Application Programming Interfaces) allow applications to communicate with each other over the internet. In web development, this almost always means sending HTTP requests to a server and getting back JSON data. JavaScript's fetch API, combined with async/await, makes this straightforward. This module covers how to make GET and POST requests, handle responses, and deal with errors.
Why This Matters π‘
Almost every real application pulls data from an external source β weather data, user profiles, product listings, financial data. Knowing how to make API calls and handle the responses is one of the most practical skills in front-end development. It is also your first direct encounter with asynchronous programming, which is fundamental to JavaScript.
Learning Goals π―
By the end of this module you should be able to:
- Understand what an API is and how HTTP requests work
- Use
fetchwithasync/awaitto make GET requests - Parse JSON responses
- Handle errors with
try/catch - Make a POST request with a JSON body
- Use Postman to test an API before writing code
- Display fetched data in the DOM
Vocabulary π
| Term | Definition |
|---|---|
| API | A set of rules for how two applications communicate |
| HTTP | The protocol used to send requests and responses over the web |
| GET | An HTTP method for reading data |
| POST | An HTTP method for sending data to create or update a resource |
| JSON | JavaScript Object Notation β a data format for APIs |
| Promise | An object representing a future value from an async operation |
| async/await | Syntax for writing asynchronous code that looks synchronous |
| Status code | A number indicating the result of a request (200 = OK, 404 = not found, 500 = server error) |
Core Concepts π§
Making a GET request
// async function with error handling
const fetchUser = async (id) => {
try {
const response = await fetch(`https://api.example.com/users/${id}`)
if (!response.ok) {
throw new Error(`request failed: ${response.status}`)
}
const data = await response.json()
return data
} catch (error) {
console.error('fetch error:', error)
}
}
Making a POST request
const createUser = async (userData) => {
try {
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData),
})
if (!response.ok) {
throw new Error(`request failed: ${response.status}`)
}
const newUser = await response.json()
return newUser
} catch (error) {
console.error('create user error:', error)
}
}
Rendering fetched data to the DOM
const loadPokemon = async (name) => {
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`)
const pokemon = await response.json()
const img = document.querySelector('#pokemon-img')
const title = document.querySelector('#pokemon-name')
img.src = pokemon.sprites.front_default
title.textContent = pokemon.name
}
loadPokemon('pikachu')
Examples π»
Fetching and rendering a list from a public API:
const renderPosts = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts?_limit=5')
const posts = await response.json()
const list = document.querySelector('#posts')
posts.forEach((post) => {
const item = document.createElement('li')
item.textContent = post.title
list.appendChild(item)
})
}
renderPosts()
Handling a loading state:
const loadData = async () => {
const status = document.querySelector('#status')
status.textContent = 'loading...'
try {
const response = await fetch('https://api.example.com/data')
const data = await response.json()
status.textContent = ''
renderData(data)
} catch (error) {
status.textContent = 'something went wrong. try again.'
}
}
Common Mistakes β οΈ
- Not checking
response.ok. A 404 or 500 response does not throw an error βfetchonly rejects on network failures. Always checkresponse.okbefore calling.json(). - Forgetting
await. Withoutawait, you get a Promise object instead of the data. This is one of the most common bugs in async code. - Not wrapping in
try/catch. If the network is down or the server returns an error, your app crashes silently. Always handle the error case. - Calling an async function and using the return value directly. Async functions return Promises. You need to
awaitthem or use.then(). - Forgetting
Content-Typeheader on POST. Without it, the server may not know how to parse your request body.
Debugging Tips π
- Use Postman to test an API endpoint before writing any JavaScript. Confirm you get the expected data shape.
- Open DevTools β Network tab to inspect real requests: URL, method, status code, request headers, and response body.
console.log(response.status)before calling.json()to see if the request succeeded.- If you get a CORS error, the API does not allow requests from your origin. Look for a public CORS-friendly version, use a proxy, or call it from your own backend.
- Check the response structure with
console.log(data)before trying to access nested properties.
Exercise ποΈ
The exercise for this module is in the class repository:
ttpr-lagcc-spring-2026 β Module 6 Exercise (opens in new tab)
Build a PokΓ©mon lookup app using the PokΓ©API (opens in new tab). A user types a PokΓ©mon name, clicks a button, and the app fetches and displays the PokΓ©mon's image, name, and types. Handle loading and error states.
Additional Resources π
- MDN β Using Fetch (opens in new tab) β complete fetch reference
- MDN β async/await (opens in new tab) β understanding async and promises
- javascript.info β Fetch (opens in new tab) β practical fetch walkthrough
- PokΓ©API (opens in new tab) β free, public REST API, great for practice
- JSONPlaceholder (opens in new tab) β fake REST API for testing
- Postman (opens in new tab) β test API endpoints without writing code first
Recap Checklist βοΈ
- I understand what an HTTP request and response are
- I can use
fetchwithasync/awaitto make a GET request - I check
response.okbefore processing the response - I handle errors using
try/catch - I can make a POST request with a JSON body and correct headers
- I can render fetched data to the DOM
- I know how to use the Network tab in DevTools to inspect requests