Learning about APIs and how to request APIs with Python


API stands for Application Programming Interface. It is usually a URL on the internet which allows us to call some other application from our own application in order to accomplish a task. Today we are going to see how we can use an API to obtain some information from a third-party application somewhere on the Internet.

What do we need

We shall be using the coinlayer API to fetch the rates of cryptocurrencies against the Indian rupee. This API requires us to create an account on their site and obtain an API key, which can authorize us when we make a request to the API.

So go ahead and create an account -> coinlayer.com

Get started with the code

Install libraries

Run the following in your command prompt or terminal

pip install requests
pip install rich

The library rich helps us to create pretty user interfaces inside the command line.

Read more here

Basic setup

Create a file called api_key.txt and put the API key that you copied inside this file.

The following snippet imports the libraries, reads in the key and defines the base URL that we are going to call

import requests
from rich import print as rprint
from rich.panel import Panel
from rich.text import Text
from rich.prompt import Prompt

with open("api_key.txt","r") as file:
    KEY = file.read().strip()

BASE_URL = "http://api.coinlayer.com/api/live"

Function to get the exchange rate

def get_exchange_rate(ticker):
    returns the exchange rate for the given ticker to INR
    request_url = f"{BASE_URL}?access_key={KEY}&target=INR&symbols={ticker}"
    data = requests.get(request_url).json()

    return data["rates"][ticker]

The above function accepts a ticker symbol which is a shortened form of the cryptocurrency name (like BTC for Bitcoin, ETH for Etherium, etc.). It constructs the request URL by appending the API key and the ticker symbol to the base URL. Notice that the target is INR. You can change this if you want the exchange rate in some other currency system. Finally it makes a request to the URL with the requests library and returns a particular key in the data corresponding to the ticker.

Build the UI and call the get_exchange_rate function

We use the rich library to ask the user for a ticker symbol. Then call the function with that symbol. Finally we display the rate to the user.

panel = Panel(Text("Crypto to Rupee Converter", justify="center"))
rprint("[yellow]Please provide a crypto ticker symbol[/yellow]")
ticker = Prompt.ask("[red]>[/red][green]>[/green][blue]>[/blue]", default="BTC")
rprint(f"\n\n[bold][magenta]Fetching exchange rate of [/magenta][purple]{ticker} to Rupee[/purple][/bold] :rocket:")
rate = get_exchange_rate(ticker)
rprint(f"[italic green]The rate is {rate}[/italic green]")

Example to show the working script



In this article, we learned how to request an API from Python, handle the API response and display it in a pretty UI.

You can find the full code in this GitHub repo -> github.com/Rishav-12/crypto-to-rupee

Happy coding!