π» Week 01, Day 04 - Lab
Authenticating to APIs
By the end of this lab, you should be able to: i) Create an OpenWeather API account and generate an API key, ii) Store an API key in a .env file and load it in Python with python-dotenv, iii) Read the OpenWeather API documentation and identify how it differs from Open-Meteo, iv) Set up your midterm project folder on Nuvolos, v) Make your first authenticated API request to OpenWeather.
β° Thursday, 16 July 2026 | Either 2:00-3.30pm or 3.30-5:00pm π Check your timetable for the location of your class
This morning you learned pandas. This afternoon you start your midterm project.
Your class teacher is here to help with the technical setup: creating your API key, storing it safely, reading the OpenWeather documentation, and working through any errors you run into when you make your first request.
π£οΈ Lab Roadmap
| Part | Time | What you do |
|---|---|---|
| Part I | First 10 min | Sign up for OpenWeather and generate your API key |
| Part II | Next 15 min | Set up your project folder on Nuvolos |
| Part III | Next 15 min | Learn about .env files and python-dotenv |
| Part IV | Remaining time | Read the API docs, make your first request, start collecting data |
Part I: Sign up for OpenWeather
Do this first, before anything else. API keys can take anywhere from a few minutes to 30 minutes to activate after signup. Start now so the key is ready by the time you need it.
π― Action points
- Go to openweathermap.org/users/sign_up and create a free account.
- Once logged in, go to API keys in your account dashboard and generate a key.
- Copy the key and save it somewhere safe (a text file on your computer, a password manager, or a note). Do not paste it into a notebook cell you plan to submit.
While you wait for the key to activate, move on to Part II.
Part II: Set up your project folder
During the lecture, the midterm assignment folder was distributed to your Nuvolos instance. You should see it at /files/assignments/midterm/.
It contains three files:
| File | What it is |
|---|---|
README.md.template |
Instructions template |
01-collection.py |
Starter script (for the scripts approach) |
NB01-Data-Collection.ipynb |
Starter notebook (for the notebooks approach) |
π― Action points
Rename
README.md.templatetoREADME.md. Open it and fill in the text, including your LSE Student ID.Pick one approach: notebooks or scripts. If you prefer notebooks, delete
01-collection.py. If you prefer scripts, deleteNB01-Data-Collection.ipynb. You do not need to decide right now, but at the time of submission you will need to keep only one. Picking early helps you start working in the right file.Create any folders you need. You will probably want a
data/folder for your collected data and possibly afigures/folder later.
π€ Notebooks or scripts?
Both are valid. Notebooks are good if you want to see outputs inline and write markdown explanations next to your code. Scripts are good if you want to run your code from the terminal and keep it modular.
If you are not sure, start with the notebook. You can always move code into a script later.
Part III: Storing your API key safely
Your API key is a secret. It identifies your account and should not appear in any file you submit.
π― Action points
In your project folder, create a file called
.env(the filename starts with a dot). Add one line:OPENWEATHER_API_KEY=paste_your_key_hereIn your notebook or script, load the key like this:
import os from dotenv import load_dotenv load_dotenv() api_key = os.environ["OPENWEATHER_API_KEY"]If
python-dotenvis not installed, run this in a terminal cell:pip install python-dotenv
Do not submit your .env file. It contains your personal API key. When you hand in your project, the .env file should not be included.
π Why not just paste the key into the code?
If you write api_key = "abc123..." directly in your notebook, anyone who sees that file has your key. That includes your class teacher, anyone you share the file with, and any version control system you might use later. The .env approach keeps the key in a separate file that stays on your machine and never gets shared.
Part IV: Read the docs, make your first request
Your key should be active by now (or close to it). Open the OpenWeather API documentation and find the endpoint you need for the midterm.
How OpenWeather differs from Open-Meteo
This time, we switch to a different API called OpenWeather.
In the labs so far, you used the Open-Meteo API without any password or key. That works for small requests, but public APIs without authentication can lock you out if too many people send requests at the same time. With OpenWeather, each request goes under your account and the provider knows who is asking.
You will need to figure out which endpoint gives you the data you need for the midterm question.
π― Action points
Read the documentation for the endpoint you plan to use. Find where the API key goes in the URL and what parameters you need.
Try a request in your notebook or script:
import requests response = requests.get( "https://api.openweathermap.org/data/2.5/weather", params={ "lat": 51.5085, "lon": -0.1257, "appid": api_key, "units": "metric", }, timeout=60, ) print(response.status_code) print(response.json())If you get a
200status code, your key is working and you can start building your data collection. If you get401, the key has not activated yet. Wait a few more minutes and try again.For the rest of the session, work on your midterm. Your class teacher is here to help with technical problems: API errors, JSON navigation, Python bugs, pandas questions.
Your class teacher can help with specific technical questions (βwhy is this line giving me an error?β, βhow do I read this part of the API response?β). They cannot evaluate your analytical choices (βis this good enough?β, βshould I collect data from these cities?β). Those decisions are yours.
β¨ Wrap-up
Submission will be done directly through Nuvolos. Detailed instructions for how to submit will come on Monday. For now, focus on collecting your data and getting comfortable with the OpenWeather API.
If your key still has not activated by the end of the lab, do not worry. It will be ready by tonight. Keep working on the parts that do not need a working API key yet (setting up your folder, writing your README, planning which cities to use).