Unlocking the Power of APIs with Python: A Comprehensive Guide and Practical Example

Application Programming Interfaces (APIs) are a powerful tool that allows developers to access data and functionality from other applications or services. APIs are available for a wide range of applications, including social media platforms, e-commerce sites, and financial services. In this article, we will explore how to use APIs with Python.

The first step in using an API with Python is to choose an API that provides the data or functionality you need. There are a variety of APIs available online that you can use for free or for a fee. Some popular APIs include:

  • OpenWeatherMap API for weather data
  • Google Maps API for location data

There are so many websites that offers a suite of powerful and easy-to-use APIs for developers. These APIs provide access to a wide range of data and services, including currency exchange rates, weather data, IP geolocation, email validation, and more.

Here are the general steps for Using APIs with Python :

  1. Choose an API: Identify an API that provides the data or functionality you need. You can find APIs by searching online or consulting API directories.
  2. Read API documentation: Once you have identified an API, read the documentation to understand how to use it. The documentation should provide information on the API endpoints, parameters, and authentication methods.
  3. Install required modules: Install any required Python modules to make HTTP requests and parse JSON responses. The most commonly used module for this is requests.
  4. Make API requests: Use the requests module to make HTTP requests to the API endpoints. Provide any required parameters in the request and include any authentication tokens in the request headers.
  5. Parse JSON responses: After making an API request, parse the JSON response using Python’s built-in json module.
  6. Process data: Process the data from the API response as required. This may involve filtering, sorting, or transforming the data to fit your needs.

We will use an API of apilayer.com to get the data for converting currency from USD to INR. apilayer.com provides API products with clear documentation, code examples, and SDKs for popular programming languages such as Python, PHP, Java, and Ruby. The APIs also offer flexible pricing plans, including free plans for testing and small-scale projects.

Python Code to convert USD to INR using API

First, You need to install the requests module, which is used to make HTTP requests in Python. To install the requests module, run the following command in your terminal.

Unlocking the Power of APIs with Python: A Comprehensive Guide and Practical Example

Below given is a python code to convert currency from USD to INR using sample API, You need to replace apikey with your one (You can get API key from apilayer.com by subscribing for free).

Python Code

import requests

 

def convert_usd_to_inr(amount):

from_currency = ‘usd’

to_currency = ‘inr’

url = “https://api.apilayer.com/exchangerates_data/convert?to={0}&from={1}&amount={2}”.format(to_currency,from_currency,amount)

payload = {}

headers= {

“apikey”: “<Your API Key>”

}

response = requests.request(“GET”, url, headers=headers, data = payload)

print(response.text)

status_code = response.status_code

if status_code==200:

response_json = response.json()

print(response_json)

result = round(response_json[‘result’],2)

else:

print(“API request failed”)

res=”{0} USD = {1} INR”.format(amount,result)

print(res)

 

if __name__ == “__main__”:

 

print(“PYTHON CODE TO CONVERT USD TO INR USING API”)

print(“——————————————-“)

 

amount = float(input(“Enter amount in USD ($) : “))

convert_usd_to_inr(amount)

Github Link to Python Code :

https://gist.github.com/harsh546/3a22639a0bac7aa784254fa33cbcf4da

Output

Unlocking the Power of APIs with Python: A Comprehensive Guide and Practical Example

 

API will return a json answer like this which contains desired result in key-value pair, we can fetch required data from it.

 

Unlocking the Power of APIs with Python: A Comprehensive Guide and Practical Example

By : Harch Verma

Tech community  Threws


Warning: Division by zero in /home/u411621557/domains/threws.com/public_html/wp-includes/comment-template.php on line 1518

Leave a Comment

Your email address will not be published. Required fields are marked *