Getting started with APIs in Python
Tags: python, beginners
Published: 2020-06-06 18:08:38

**API** stands for **A**pplication **P**rogramming **I**nterface. It is a set of standards that defines rules for accessing a web based service. APIs enable software-to-software interaction and provide an interface through which a client application can communicate with the API service using HTTP methods such as GET, POST (insert), PUT (update) and DELETE. 

In this article, let’s see how to consume an API from a Python application with an example. 

Let's say we need to display current temperature for a given city in our application. We could use an API service to get the weather information. 

<h4>Here are steps involved in consuming an API:</h4>
* Research on available API services that provide desired data. For getting weather information, let's use <a href="https://openweathermap.org/">OpenWeather</a> API service.
* API services generally require developers to sign up for their service so that they could control usage of their service. One can sign up with OpenWeather using <a href="https://openweathermap.org/guide#how
">getting started guide</a> link  
* Most API services provide "free tier" for experimental low usage and "paid subscription" for heavy use cases. <a href=" https://openweathermap.org/price">OpenWeather price</a> link provides information to pick appropriate tier and get the API key to use.
* Next step is to review API documentation to figure out how to call the service with the right parameters.Here is the <a href="https://openweathermap.org/current">API documentation</a> link for current weather information. 
* API Documentation could also be used to analyze in what format data will be returned by the API service. Most common format is JSON. 

Below listing shows an example weather module using OpenWeather API that gets current weather data for a given city argument:
```python
import os, requests
import json

def weather(city):
    api = os.getenv('WEATHER_API_KEY')
    source = requests.get(f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api}')
    parsed = json.loads(source.text)
    print(json.dumps(parsed, indent=4))

if __name__ == "__main__":
    import sys
    weather(sys.argv[1]) 
```
PS: An environment variable is used to store API key. 

<h4>What are API Wrappers?</h4>
API wrappers provide additional layer of convenience methods to use API services in a desired language. Python package `openweathermapy` provides a wrapper for working with the OpenWeather API. Below code can be used to get the current weather data using this wrapper:
```python
import openweathermapy.core as owm
data = owm.get_current("London,UK", units="metric", "APPID": 1111111111)
```   

* Here is a link to the <a href="https://libraries.io/pypi">libraries.io</a> website that maintains an exhaustive list information about open source projects, framework and tools. This website has very useful information on many open source packages and frameworks.   

* Also, <a href="https://github.com/discdiver/list-of-python-api-wrappers">Python API Wrappers</a> is a github repository that maintains a great of set of API services along with their  corresponding Python API wrappers.