DevOps Engineer. Solutions Architect

Blog 9 Spring 2021: APIs and Python

APIs (Application programming interface) can be interpreted as a composition of rules that enables developers and users to access an external service on web through our systems. Thus, an API sets certain formats which can access the service and the data to and from a model.

Through the use of Python, one can access this API data source through specific libraries.

Types of API Requests

Command Description
GET It enables the users to fetch the data from the APIs onto their system in a specific format that is usually JSON
POST This command enables us to add data to the API i.e. to the service on web.
DELETE It enables us to delete certain information from the API service on web.
PUT Using PUT command, we can update an existing data or information in the API service on web.

Status/Response Codes

Status Description
200 OK. It means we have a healthy connection with the API on web.
204 It depicts that we can successfully made a connection to the API, but did not return any data from the service.
401 Authentication failed!
403 Access is forbidden by the API service.
404 The requested API service is not found on the server/web.
500 Internal Server Error has occurred.

Connecting to an API with Python

  1. Import proper libraries
    import requests
    
  2. Perform an action to connect to the API and print response code
    response_API = requests.get('https://www.askpython.com/')
    print(response_API.status_code)