make an api call in python

To make an API call in Python, you can use the requests library. The steps to make an API call in Python are:

  1. Import the requests library
main.py
import requests
16 chars
2 lines
  1. Send a GET request to the desired URL
main.py
response = requests.get(url)
29 chars
2 lines
  1. Retrieve the response data in the desired format (e.g. JSON)
main.py
data = response.json()
23 chars
2 lines

Here's an example of making an API call to the GitHub API:

main.py
import requests

response = requests.get('https://api.github.com/repos/requests/requests')
data = response.json()

print(data['full_name'])
140 chars
7 lines

This will print the full name of the requests repository on GitHub (which is 'requests/requests').

related categories

gistlibby LogSnag