0
0
Rest APIprogramming~30 mins

First API request and response in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
First API request and response
📖 Scenario: You are building a simple program to get the current weather from a public API. This will help you learn how to make your first API request and see the response.
🎯 Goal: Learn how to send a GET request to a weather API and display the temperature from the response.
📋 What You'll Learn
Use the requests library to make an API call
Send a GET request to the URL https://api.weatherapi.com/v1/current.json?key=demo&q=London
Extract the temperature in Celsius from the JSON response
Print the temperature with a friendly message
💡 Why This Matters
🌍 Real World
APIs let programs get live data from websites and services. Weather apps use APIs to show current weather.
💼 Career
Knowing how to make API requests is a key skill for developers working with web services, mobile apps, or data integration.
Progress0 / 4 steps
1
Set up the API URL
Create a variable called url and set it to the string 'https://api.weatherapi.com/v1/current.json?key=demo&q=London'.
Rest API
Need a hint?

Use a string variable to store the full API URL exactly as given.

2
Import requests and make the GET request
Import the requests library and create a variable called response that stores the result of requests.get(url).
Rest API
Need a hint?

Use import requests at the top. Then call requests.get(url) and save it in response.

3
Extract temperature from JSON response
Create a variable called data that stores the JSON content from response using response.json(). Then create a variable called temp_c that gets the temperature in Celsius from data['current']['temp_c'].
Rest API
Need a hint?

Use response.json() to get the data. Then access the temperature with data['current']['temp_c'].

4
Print the temperature
Write a print statement that outputs: "The current temperature in London is {temp_c}°C." using an f-string.
Rest API
Need a hint?

Use print(f"The current temperature in London is {temp_c}°C.") to show the temperature.