Python ExchangeRate API Lab Exercise
Overview
In this lab, you will practice making API calls using Python’s requests library. You’ll use the Exchange Rate API to fetch current currency exchange rates and manipulate the data within your Python program. This exercise will help you understand how to interact with web APIs, handle JSON data, and build a simple currency converter.
Objectives
- Use the requests library to make HTTP GET requests to an API.
- Parse and extract data from JSON responses.
- Handle user input to create dynamic API queries.
- Implement basic error handling for API responses.
Instructions
-
Import the
requestspackage into your Python file. -
Sign up for a free account to get your API key.
- Visit Exchange Rate API to sign up for a free account.
- After signing up, you’ll receive an API key. Keep this key secure and avoid sharing it publicly.
-
Set up the API endpoint URL.
The base URL for fetching the latest exchange rates is:
https://v6.exchangerate-api.com/v6/YOUR-API-KEY/latest/USD-
Replace
YOUR-API-KEYwith the API key you obtained. -
Replace USD with the three-letter currency code you want to use as the base currency (ex: EUR, JPY, GBP).
-
-
Make a GET request to the API endpoint using the requests library.
-
Parse the JSON response to extract the exchange rate for a target currency.
- The JSON response contains a key called conversion_rates, which is a dictionary of currency codes and their exchange rates relative to the base currency.
- Refer to the Exchange Rate API Python Documentation for more detailed usage information.
-
Print the exchange rate.
- Display the exchange rate using a formatted string:
print(f"The exchange rate from USD to EUR is {exchange_rate}") -
Test your code by running
python3 main.pyEnsure your program outputs the correct exchange rate.