Online IDE Repl.it

When I intially began programing, I did not focus on the user’s interaction with my program. I worked with accepting the user’s input and writing my program around the user. However, I soon discovered how powerful APIs are.

API stands for Application Program Interface and is a tool that can be used to obtain data from other websites. This tool is highly important because it allows a program to sync and retrieve data from a web server, which increases the functionality of the program.

API’s also prevent the programmer from heavily focusing on the user’s interaction because the information the user might need will be sent to the corresponding API. This increases the user’s interaction with the program.

The Requests Library

The Python requests library does exactly what is sounds like. It simply sends python requests to any application of your choice.

These requests are known as API requests. API requests have different types such as: GET, POST, PUT, and many others. In this article, we will focus more on the GET API request which requests and retreives data from a server. It is also important to note that after making these requests, the data will be received in JSON(JavaScript Object Notation) format.

Using the GET request, I wrote some code on Repl.it, that asks the user for their zip code and country and returns the current weather associated with the zip code. I will be using the OpenWeatherMap’s API.

Click to view

The code above imports the requests library in order to retrieve the data. The API address seen in the second line is made from the OpenWeatherMap url that can be found HERE, coupled with a unique API key that I received by signing up to OpenWeatherMap, and an endpoint(zip). The endpoint directs the API to where it can access the specific data that the user wants to retrieve, which is the zip code in this case.

The API address also takes in parameters which is the user’s input. This is why I created the updated address variable. The variable adds the user’s input to the API address. When I use the GET method in line 7, it retrieves data by accessing the API address.

In order to test the code, I input 94040, the zip code for Mountain view and the country and I get the temperature in Kelvins of the city.

Conclusion

This short tutorial should assist you when working with API’s on Repl.it and on other platforms. For more practice, you can click the text below:

Using Weather API in Python in 6 Lines of code by Siddhesh Kadam

APIs are very important and as a programmer, you might find yourself wanting to obtain data from a webserver for your program. I suggest that you get a lot of practice working with APIs. Good luck!