HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

9) Integrating APIs Lesson

API Project: CLI Game

4 min to complete · By Martin Breuss

After testing the functionality of the Uzby API first in your web browser and then in a Python script, you're ready to incorporate it into your CLI game project.

From reading the API docs and exploring the API by making your own requests, you've learned some aspects about it. Just as the documentation describes, you need to provide it with length values between 2 and 40 to get a valid result:

Name Required Description Value Example
min YES Minimum length of the name to generate Integer (>=2) 4
max YES Maximum length of the name to generate Integer (<=40) 12

You might have found that if you provide any other values as parameters to your API call, the API will return an error message instead of the expected name:

  • Name cannot be shorter than 2 characters if your min value is below 2
  • Name cannot be longer than 40 characters if your max value is above 40
  • Invalid Request if you provide negative numbers or any input that can't be converted to a valid integer in the expected range

These cornerstones of your API calls will inform the code that you need to write to include it safely in your game code. Informed with this, you're ready to write your pseudocode for the functionality you want to have:

# Collect the name from your player
# Check whether it meets the length requirements for the API call
# Ping the Uzby API to create a new random name for your player,
# using the length of their given name as input
# Inform the player about their in-game name

When a new player first starts your game, they'll be asked to enter their name. Then, you'll use the Uzby name generator API to create a unique, pronounceable in-game name for them and let them know about it.

Uzby Practice

  • Write the necessary code to include this functionality in your game.
  • Are there any possible issues that you can think about with this implementation?
  • What happens if your user doesn't have an internet connection while playing the game?

At the end of this section, you'll see an example implementation of this new feature. Try to write your own solution before checking it out. Practice thinking through the task at hand and finding your own way to solve the challenge.

In a future lesson, you'll see more suggestions for other API projects you can build, as well as a list of resources for finding interesting APIs online.