DEV Community


Posted on

Getting started with APIs in Python

API stands for Application Programming Interface. 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.

Here are steps involved in consuming an API:

  • Research on available API services that provide desired data. For getting weather information, let's use OpenWeather 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 getting started guide link
  • Most API services provide "free tier" for experimental low usage and "paid subscription" for heavy use cases. OpenWeather price 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 API documentation 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:

import os, requests
import json

def weather(city):
    api = os.getenv('WEATHER_API_KEY')
    source = requests.get(f'{city}&appid={api}')
    parsed = json.loads(source.text)
    print(json.dumps(parsed, indent=4))

if __name__ == "__main__":
    import sys
Enter fullscreen mode Exit fullscreen mode

PS: An environment variable is used to store API key.

What are API Wrappers?

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:

import openweathermapy.core as owm
data = owm.get_current("London,UK", units="metric", "APPID": 1111111111)
Enter fullscreen mode Exit fullscreen mode
  • Here is a link to the 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, Python API Wrappers is a github repository that maintains a great of set of API services along with their corresponding Python API wrappers.

Top comments (2)

pretzellogix profile image

If you wish to learn how to write your own API Wrapper in Python, I wrote a 15-part guide that walks you thru the steps from start to finish:

phatdang profile image

Nice to help from your Post.