Create a new virtual environment for your CLI game project and install the requests library. Then, create a new Python script to test making the API request and receive a randomly generated name.
Because of the user-friendly abstractions of the requests library, you don't need a lot of code to make this API request:
import requests
min_len = 4
max_len = 6
URL = f"https://uzby.com/api.php?min={min_len}&max={max_len}"
response = requests.get(URL)
print(response.text)
After you run this short script, you'll get a name as a string printed out to your console:
Zaiki
Keep in mind that these names are randomly generated, which is why your output will be different, even if you use the same numbers for min_len and max_len.
API Request Practice
- Explore the API some more by making more calls with different values for
min_lenandmax_len. - Can you change the arguments the API takes to receive an error message from the API? What does it look like?
In a future lesson, you'll incorporate this API call into your CLI game code to generate a random name for your player based on the real name they provide.