⚡ Phase 2 — Build Things · Week 7

Connecting to APIs

⏱ ~4 hours 📚 4 lessons 🛠 VS Code · OpenWeather API · Claude AI · GitHub

APIs let you pull live data into your site — weather, news, sports scores, business data. A WV outdoor guide site could show live weather at Seneca Rocks. A local news aggregator could pull WV headlines. This is how you build things that stay current.

Lesson 1 of 4

🔌 What an API Is

API stands for Application Programming Interface. Forget the acronym. Here's the mental model: an API is a waiter at a restaurant. You (your code) tell the waiter (the API) what you want. The waiter goes to the kitchen (another service's server), gets what you asked for, and brings it back.

🍽 The restaurant analogy — how APIs work
👤
Your App
"Get me weather for NYC"
🧑‍💼
API Request
Formatted ask
🗄
Weather Server
Looks up data
📦
JSON Response
Data comes back
📺
You Show It
On your page
Lesson 2 of 4

📡 Fetching Data with JavaScript

JavaScript has a built-in command called fetch() that talks to APIs. You give it a URL, it returns data. The data comes back in a format called JSON.

weather.js
// fetch() goes and gets data from a URL
fetch('https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_KEY')
  .then(response => response.json())   // convert to JSON
  .then(data => {
    // data is now a JavaScript object you can use
    console.log(data.main.temp);       // temperature
    document.getElementById('temp').textContent = data.main.temp + '°';
  });
🤖
Get a free OpenWeather API key

Go to openweathermap.org/api → sign up free → copy your API key. Paste it into Claude and say "Write me the fetch code for current weather in [city] using this key."

Lesson 3 of 4

🗂 Reading JSON Data

JSON (JavaScript Object Notation) is how APIs send data back to you. It looks like a labeled list. Once you can read it, you can pull out any piece of data you want.

example-weather-response.json
{
  "name": "London",
  "main": {
    "temp": 18.5,           // ← data.main.temp
    "humidity": 72          // ← data.main.humidity
  },
  "weather": [{
    "description": "light rain"  // ← data.weather[0].description
  }]
}

I'm calling the OpenWeather API and getting this JSON back: [paste the response]. I want to display: city name, temperature in Fahrenheit, weather description, and humidity. Write the JavaScript to extract each value and display it in these HTML elements I already have: [paste your HTML].

Lesson 4 of 4

🔑 API Keys & Keeping Them Safe

API keys are like passwords — they identify your account and control access. For this week's project, you'll use them directly in your JavaScript (fine for learning). Later we'll cover safer approaches.

⚠️
Don't post API keys on GitHub

If you push code to a public GitHub repo with an API key in it, bots will find it within minutes and use it. For now, keep your projects private on GitHub, or use keys with usage limits. We'll cover proper secret management in Week 8.

🏗 Week 7 Project

Live Weather Dashboard

Build This

A weather dashboard that shows live data for any city

  • A text input where you type a city name
  • A search button that fetches the weather
  • Display: temperature, city name, description, humidity, wind speed
  • A weather emoji icon that changes based on conditions
  • Error handling for city not found

Build a weather dashboard page (HTML + CSS + JS). User types a city name, clicks Search, and sees current weather. Use the OpenWeather API with this key: [YOUR KEY] Show: city name, temperature in both °C and °F, weather description, humidity, and wind speed. Add an appropriate emoji for the weather condition. Handle the error case where the city isn't found. Make it look clean and modern. I'm a beginner — add comments.

Week 7 Done!

Mark it complete and keep your momentum going.