DEV Community

Eric Halverson
Eric Halverson

Posted on

Beginner's Guide to using API's with Python

The purpose of this post is to show a simple example of how do get started using API's with Python. Specifically, how do you interact with a public API to request data to use in your local application and then how to extract the data received from the API server so that you can use it in your application.

In this specific project, we will create a small daily horoscope terminal app that asks a user for their sign. The user input is packaged and sent as a request to an API server which will return a JSON object with the appropriate horoscope information based on the user's sign.

We will then use the Python json module to unpack the returned JSON object into a Python dictionary so we can use the data to respond back to the user.

This article is not intended to be exhaustive on the topic of using API's and JSON objects but rather, a starting point for further exploration.

Prior to this project, I had never worked with Python API's or JSON objects so I had to figure everything out.

The 3 main parts of the puzzle are:

  1. A public API Server serving daily horoscopes (or whatever you want your API to fetch)
  2. A Python module with methods for working with API's from the client side.
  3. The Python module with methods for converting JSON objects (which are the standard for returning API called data) to Python data types so that the data can be used in the application.

I found aztro from RapidAPI which provides basic horoscope information. You need to subscribe to RapidAPI (free) in order to be issued a public key which is included in your request to the server (which you can see in the code example in the 'Headers' section.

Most API servers have documentation on how to use the API and often with programming language specific code examples.

Python API code example

As you can see in the image, this particular code is using the
Python requests module which allows you to send http requests using Python.

The requests module is not included by default so will need to be installed. You can search for specifics depending on whether you are using a particular package manager, etc... I used pip install requests.

After installing the module, you will need to import it at the top of your Python file to use it's methods in your code.

import requests

I started by creating a 'api_test.py' file added my import requests at the top and then copy/pasted the code from the API documentation into my file, saved and executed the code from the terminal python3 api_test.py.

It worked! Using the 'hard-coded' parameters in the query string, the API returned a JSON object that contained key-value pairs for a 'description', 'compatibility', 'lucky_number', and other related attributes.

The next step is to convert the JSON text object into a Python data type so the data can be more easily used in the application. For this we need the json module which is included in the standard library but needs to be imported at the top of the file where it is being used. import json.

In my code example, I used the json.loads('JSON object') method to store the API information in a python dictionary so I could use it in my application to return the daily horoscope to the user.

Here is a link to the project on GitHub so you can see the whole code example.

Have fun! and All the best in your coding journeys!

This project was created as part of the Computer Science Career Path at Codecademy.

Top comments (0)