0% found this document useful (0 votes)
15 views6 pages

Hands-on Lab- API Examples Random User and Fruityvice API Examples

This document provides a hands-on lab for using various APIs, including Random User, Fruityvice, and Open-Joke-API, with Python libraries. It outlines the objectives, advantages, and disadvantages of using APIs, along with practical examples and exercises for generating user data and fruit information. The lab is designed to enhance understanding of API integration and data manipulation using Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Hands-on Lab- API Examples Random User and Fruityvice API Examples

This document provides a hands-on lab for using various APIs, including Random User, Fruityvice, and Open-Joke-API, with Python libraries. It outlines the objectives, advantages, and disadvantages of using APIs, along with practical examples and exercises for generating user data and fruit information. The lab is designed to enhance understanding of API integration and data manipulation using Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2/8/25, 5:40 AM PY0101EN-5 2_API_2 v2

Hands-on Lab: API Examples


Random User and Fruityvice API Examples
Estimated time needed: 30 minutes

Objectives
After completing this lab you will be able to:

Load and use RandomUser API, using RandomUser() Python library


Load and use Fruityvice API, using requests Python library
Load and use Open-Joke-API, using requests Python library

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.

The advantages of using APIs:

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.

The disadvantage of using APIs:

Security. If the API is poorly integrated, it means it will be vulnerable to attacks,


resulting in data breeches or losses having financial or reputation implications.

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.

Example 1: RandomUser API


Bellow are Get Methods parameters that we can generate. For more information on the
parameters, please visit this documentation page.

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 [ ]: !pip install randomuser


!pip install pandas

Then, we will load the necessary libraries.

In [ ]: from randomuser import RandomUser


import pandas as pd

First, we will create a random user object, r.

In [ ]: r = RandomUser()

Then, using generate_users() function, we get a list of random 10 users.

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.

In [ ]: for user in some_list:


print (user.get_full_name()," ",user.get_email())

Exercise 1
In this Exercise, generate photos of the random 10 users.

In [ ]: ## Write your code here

Click here for the solution

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 =[]

for user in RandomUser.generate_users(10):


users.append({"Name":user.get_full_name(),"Gender":user.get_gender(),"City"

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.

Example 2: Fruityvice API


Another, more common way to use APIs, is through requests library. The next lab,
Requests and HTTP, will contain more information about requests.

We will start by importing all required libraries.

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.

We will retrieve results using json.loads() function.

In [ ]: results = json.loads(data.text)

We will convert our json data into pandas data frame.

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

In [ ]: cherry = df2.loc[df2["name"] == 'Cherry']


(cherry.iloc[0]['family']) , (cherry.iloc[0]['genus'])

Exercise 2
In this Exercise, find out how many calories are contained in a banana.

In [ ]: # Write your code here

Click here for the solution

Exercise 3
This page contains a list of free public APIs for you to practice. Let us deal with the following
example.

Official Joke API


This API returns random jokes from a database. The following URL can be used to retrieve 10
random jokes.

https://official-joke-api.appspot.com/jokes/ten

1. Using requests.get("url") function, load the data from the URL.

In [ ]: # Write your code here

Click here for the solution

2. Retrieve results using json.loads() function.

In [ ]: # Write your code here

Click here for the solution

3. Convert json data into pandas data frame. Drop the type and id columns.

In [ ]: # Write your code here

Click here for the solution

Congratulations! - You have completed the


lab
https://labs.cognitiveclass.ai/v2/tools/jupyterlab?ulid=ulid-a1e7e02885c09c0ac95800b28ad5db4ac6e74580 5/6
2/8/25, 5:40 AM PY0101EN-5 2_API_2 v2

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

Copyright © 2023 IBM Corporation. All rights reserved.

https://labs.cognitiveclass.ai/v2/tools/jupyterlab?ulid=ulid-a1e7e02885c09c0ac95800b28ad5db4ac6e74580 6/6

You might also like