DEV Community

Cover image for Beginner's Guide to Requests and APIs
Elijah Jeremiah L. Barba
Elijah Jeremiah L. Barba

Posted on

Beginner's Guide to Requests and APIs

The requests module is your portal to the open web.🀯 Basically any API that you have access to, you can pull data from it (Though your mileage may vary).

Getting Started ✊

Installing requests needs pipenv, so go ahead and install that first if you don't have it. If you do, simply run this command:

pipenv install requests
Enter fullscreen mode Exit fullscreen mode

Let's go ahead and pick an API to use.πŸ€”

Here is a great resource to public APIs:

GitHub logo public-apis / public-apis

A collective list of free APIs

Public APIs

<i>A collective list of free APIs for use in software and web development</i>
Enter fullscreen mode Exit fullscreen mode


The Project
Contributing Guide β€’ API for this project β€’ Issues β€’ Pull Requests β€’ License
Alternative sites for the project (unofficials)
Free APIs β€’ Dev Resources β€’ Public APIs Site β€’ Apihouse β€’ Collective APIs



[ Become a sponsor and support Public APIs and their maintainers ]

Special thanks to:

APILayer Logo

The fastest way to integrate APIs into any product

Explore, discover and consume public APIs as simpler programmable building blocks all on one platform for a 10x developer experience.
APILayer Logo

The fastest way to integrate APIs into any product

Explore, discover and consume public APIs as simpler programmable building blocks all on one platform for a 10x developer experience.



Index





Let's use the kanye.rest API for this quick example: https://kanye.rest/

Diving In πŸ™Œ

Let's import the all mighty:

import requests
Enter fullscreen mode Exit fullscreen mode

And get-ting the data is pretty straightforward:

kanye_url = requests.get('https://api.kanye.rest')
Enter fullscreen mode Exit fullscreen mode

Print it in JSON format:

print(kanye_url.json())
# gets a random quote
# {'quote': 'Tweeting is legal and also therapeutic'}
Enter fullscreen mode Exit fullscreen mode

It is important to read the docs of each API because each API is unique 🧐

Let's say we want to get a Chuck Norris joke from this API:
From the docs it uses a different URL, so let's go ahead and code that:

chuck_url = requests.get('https://api.chucknorris.io/jokes/random')
print(chuck_url.json())
Enter fullscreen mode Exit fullscreen mode

... will output something like this:

{'categories': [], 'created_at': '2016-05-01 10:51:41.584544', 'icon_url': 'https://assets.chucknorris.host/img/avatar/chuck-norris.png', 'id': 'DLqW_fuXQnO1LtveTTAWRg', 'updated_at': '2016-05-01 10:51:41.584544', 'url': 'https://api.chucknorris.io/jokes/DLqW_fuXQnO1LtveTTAWRg', 'value': 'Chuck Norris Lost his virginity before his Dad...'}
Enter fullscreen mode Exit fullscreen mode

...not exactly pretty to look at, so let's pretty-print it:πŸ’…

# πŸ‘‡ Add this import below at the beginning of your file    
# import json

print(json.dumps(chuck_url.json(), indent=2))
Enter fullscreen mode Exit fullscreen mode

Now it looks like this:

{
  "categories": [
    "science"
  ],
  "created_at": "2016-05-01 10:51:41.584544",
  "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
  "id": "izjeqnjzteeqms8l8xgdhw",
  "updated_at": "2016-05-01 10:51:41.584544",
  "url": "https://api.chucknorris.io/jokes/izjeqnjzteeqms8l8xgdhw",
  "value": "Chuck Norris knows the last digit of pi."
}
Enter fullscreen mode Exit fullscreen mode

Now that's much more readable! πŸ’ͺπŸ’ͺπŸ’ͺ

Sources:
🐍 https://3.python-requests.org/
πŸ“– https://github.com/public-apis/public-apis
🌊 https://kanye.rest/
🀠 https://api.chucknorris.io/

Top comments (1)

Collapse
 
thamaragerigr profile image
Thamara Gerig

Awesome!!!! Thanks!!!