top of page

My First Python Code - Consume a Rest API.

#MyFirstTechSeries: My First Tech Series is a series of technical topics. It includes step by step instructions along with screenshots. All the steps beginning with software installation is mentioned in the articles. My vision is to help developers to easily follow the steps and get the tech topic working in his/her system. This working demo will save them a lot of time spent collating information from different tutorial sites and YouTube videos.

In this post, we will be installing python and PyCharm from the scratch. And, we will be creating a project that will consume a public API and parse and print the JSON output. The URL which we will be using for this example is :

There are 2 versions of python and both can be installed in your system. The python 2 series is the older version of python with limited support. The python 3 series is the newer version of python.

The only difference between 2 series and 3 series is the support and some minor syntax changes here and there.We will be installing and working with python 3.

Step 1: Installing Python:

  • Check if your system already has python installed. Goto Command Prompt and type

python --version
  • My Mac has 2.7 version of python installed out of the box.

  • We will install the latest version using brew. The command in brew is:

brew install python@3.10

It might take a couple of minutes to install.

As I mentioned earlier, system can have both 2.x and 3.x version of python installed. To check if a version is installed, use following command.

python2 --version
python3 --version
python --version


  • The below step is completely optional and for Mac only. But, if you want the default python version to be set to the 3 version, then try the following command for Mac.

echo "alias python=/usr/local/bin/python3.10" >> ~/.bashrc
source ~/.bashrc
  • Now, when you try running python --version in your terminal, the default version will be changed to python3.10


Step 2: Installing PyCharm (Python's Text Editor):

  • Install the PyCharm editor in your system. For Mac, just drag to Applications folder and you are done :)


STEP 3: Open PyCharm

  • When you open the pycharm for the first time, you will get an error message like below. Click "Open"

  • Agree to the terms and conditions.

  • Once you agree, you will get this landing page of PyCharm.

Click New Project. In the New project window that opens, give the project name in your location. I have given as "myFirstPython".

Base interpretor contains the list of all available python installations in your system. I am choosing python3.

All python filenames end with ".py". The program execution begins with the I am having the checkbox checked for "Create a welcome script"

Click on Create.

  • The project and has been successfully created.


STEP 4: Code

The next step is to write code to consume our API and print the output.

To read the API, we need to import request from url lib. Also, for parsing the json, we need to import json library.

Copy and paste this code in your

from urllib import request
import json

testName = "Nisha" #This name can be any name

api_url = ""+testName

reqres = request.urlopen(api_url)
if (reqres.getcode()):  #We get a response code of 200 - Success
    jsonResponse = json.loads(
    print(f"\nJson Response is =   {jsonResponse}")
    gender = jsonResponse["gender"]
    probability = (jsonResponse["probability"])
    print(f"\nWith {probability * 100}% probability, the name {testName} is a {gender}.")
    print (f"Invalid status code {reqres.getcode()}")

To Run the code, click the Run Symbol or goto Run --> Run 'main'.

When you run the code, you get the following output in the console.


Congratulations!! We have successfully consumed an API, parsed the JSON object and printed the response. Now you can play around with the code and build further on this. Happy exploring...!

366 views0 comments

Recent Posts

See All


Noté 0 étoile sur 5.
Pas encore de note

Ajouter une note
bottom of page