Hands-on Lab- API Examples Random User and Fruityvice API Examples
Hands-on Lab- API Examples Random User and Fruityvice API Examples
Objectives
After completing this lab you will be able to:
The purpose of this notebook is to provide more examples on how to use simple APIs. As
you have already learned from previous videos and notebooks, API stands for Application
Programming Interface and is a software intermediary that allows two applications to talk to
each other.
Automation. Less human effort is required and workflows can be easily updated to
become faster and more
productive.
Efficiency. It allows to use the capabilities of one of the already developed APIs than to
try to independently implement some functionality from scratch.
One of the applications we will use in this notebook is Random User Generator. RandomUser
is an open-source, free API providing developers with randomly generated users to be used
as placeholders for testing purposes. This makes the tool similar to Lorem Ipsum, but is a
placeholder for people instead of text. The API can return multiple results, as well as specify
https://labs.cognitiveclass.ai/v2/tools/jupyterlab?ulid=ulid-a1e7e02885c09c0ac95800b28ad5db4ac6e74580 1/6
2/8/25, 5:40 AM PY0101EN-5 2_API_2 v2
generated user details such as gender, email, image, username, address, title, first and last
name, and more. More information on RandomUser can be found here.
Another example of simple API we will use in this notebook is Fruityvice application. The
Fruityvice API web service which provides data for all kinds of fruit! You can use Fruityvice to
find out interesting information about fruit and educate yourself. The web service is
completely free to use and contribute to.
Get Methods
get_cell()
get_city()
get_dob()
get_email()
get_first_name()
get_full_name()
get_gender()
get_id()
get_id_number()
get_id_type()
get_info()
get_last_name()
get_login_md5()
get_login_salt()
get_login_sha1()
get_login_sha256()
get_nat()
get_password()
get_phone()
get_picture()
get_postcode()
get_registered()
get_state()
get_street()
get_username()
get_zipcode()
https://labs.cognitiveclass.ai/v2/tools/jupyterlab?ulid=ulid-a1e7e02885c09c0ac95800b28ad5db4ac6e74580 2/6
2/8/25, 5:40 AM PY0101EN-5 2_API_2 v2
To start using the API you can install the randomuser library running the pip install
command.
In [ ]: r = RandomUser()
In [ ]: some_list = r.generate_users(10)
In [ ]: some_list
The "Get Methods" functions mentioned at the beginning of this notebook, can generate
the required parameters to construct a dataset. For example, to get full name, we call
get_full_name() function.
In [ ]: name = r.get_full_name()
Let's say we only need 10 users with full names and their email addresses. We can write a
"for-loop" to print these 10 users.
Exercise 1
In this Exercise, generate photos of the random 10 users.
To generate a table with information about the users, we can write a function containing all
desirable parameters. For example, name, gender, city, etc. The parameters will depend on
the requirements of the test to be performed. We call the Get Methods, listed at the
beginning of this notebook. Then, we return pandas dataframe with the users.
https://labs.cognitiveclass.ai/v2/tools/jupyterlab?ulid=ulid-a1e7e02885c09c0ac95800b28ad5db4ac6e74580 3/6
2/8/25, 5:40 AM PY0101EN-5 2_API_2 v2
In [ ]: def get_users():
users =[]
return pd.DataFrame(users)
In [ ]: get_users()
In [ ]: df1 = pd.DataFrame(get_users())
Now we have a pandas dataframe that can be used for any testing purposes that the tester
might have.
In [ ]: import requests
import json
We will obtain the fruityvice API data using requests.get("url") function. The data is in
a json format.
In [ ]: data = requests.get("https://web.archive.org/web/20240929211114/https://fruityvice.
In [ ]: results = json.loads(data.text)
In [ ]: pd.DataFrame(results)
The result is in a nested json format. The 'nutrition' column contains multiple subcolumns, so
the data needs to be 'flattened' or normalized.
In [ ]: df2 = pd.json_normalize(results)
In [ ]: df2
Let's see if we can extract some information from this dataframe. Perhaps, we need to know
the family and genus of a cherry.
https://labs.cognitiveclass.ai/v2/tools/jupyterlab?ulid=ulid-a1e7e02885c09c0ac95800b28ad5db4ac6e74580 4/6
2/8/25, 5:40 AM PY0101EN-5 2_API_2 v2
Exercise 2
In this Exercise, find out how many calories are contained in a banana.
Exercise 3
This page contains a list of free public APIs for you to practice. Let us deal with the following
example.
https://official-joke-api.appspot.com/jokes/ten
3. Convert json data into pandas data frame. Drop the type and id columns.
Author
Svitlana Kramar
Svitlana is a master’s degree Data Science and Analytics student at University of Calgary, who
enjoys travelling, learning new languages and cultures and loves spreading her passion for
Data Science.
Additional Contributor
Abhishek Gagneja
https://labs.cognitiveclass.ai/v2/tools/jupyterlab?ulid=ulid-a1e7e02885c09c0ac95800b28ad5db4ac6e74580 6/6