Hackernoon logoWorking with APIs ( Concepts + Code ) by@karan.02031993

Working with APIs ( Concepts + Code )

Author profile picture

@karan.02031993Karan Jagota

| Software Engineer | Python | Javascript | Auto-Ml Enthusiast

Hi Folks !! Hope you all computer geeks are doing well . In this post, we will discuss about how to get data from any API using any programming language . Yes ! You read it right !! Well, i will be using JavaScript and Python in here but you can use any language of your choice. 

So the first question you may ask is what is an API ? 

API stands for application programming interface. In simple terms, it’s just a function that can be used to work with another application over the internet. And an application is made up of [ Frontend+backend+database ]. I hope you know that . 

Some API’s Examples : 

1. Facebook (O Auth 2) Login API  

2. Google Maps API

3. Twitter data stream API 

4. Stripe payment API etc … 

Trust me ! Each and Every tech organisation has a set of open API these days . 

But Why do we need API’s ? 

Well ! Suppose you want to develop an app that require users to login into your app via their Facebook account. Now the problem is that you don’t have any access to Facebook code or FB user’s database. So, In that case — you will send a request to Facebook (and I mean it literally ) to their public Facebook O Auth 2 Login API (URL address) and then FB will check and confirms the user details in their database and will send you an 'OK' or 'ERROR' response so that user of your app can login to your app via their FB account . 

In general, this is how companies work .. they create app/software , API’s or SDK’s and then distribute it to their users/customers. Each one has its own pros and cons but more of that in future posts. 

Now let’s look at some code —  

There are 2 things that you need to get data from an API . Let’s look at them first

1. URL : you will send a request to this URL address from your app via code . 

2. Special Function : In Almost every programming language, their is always a standard way to send a request to a URL address.

a. In JavaScript this function is fetch()

b. In Python this function is request()

c. In Android it’s okhttp() or retrofit()

[Remember : it’s not a special function , it’s just a library that you can use (open source and completely free of cost) because deep down, it’s just a boiler plated code ]. Check that special function out in your programming language.

Well, Talk is cheap ! But a good quality Code is not. Let us now see a working code in action- 

We will be using NASA Open Data API for this coding example . Get your API KEY from the link https://api.nasa.gov/#getting-started and replace it in below code .

JavaScript Code 

// importing library 
const fetch = require("node-fetch");

// Defining constants ... 
const BASE_URL = 'https://api.nasa.gov/planetary/apod?api_key=';
const API_KEY = 'xxxxxxxxxxxxxxxx';

// Creating functions ... 
const create_url = ()=>{
    return BASE_URL + API_KEY;    
const send_request = (url)=>{

// Sending request  .. 
const request_url = create_url();

// # Output :

You can also view my post on JavaScript by clicking on the link below .


Python Code 

import requests

# defining constants ..
BASE_URL = 'https://api.nasa.gov/planetary/apod?api_key='
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'

# // Creating functions ...
def create_url():
    return BASE_URL + API_KEY

def send_request(url):
    return requests.get(url)

if __name__ == '__main__':
    # sending request and getting data ...
    url = create_url()
    data = send_request(url).json()

## Output

You can view my other post on Python By clicking on the link below . 


That’s it ! This is how you get data from an API using fetch() and request() Method in JavaScript and Python. 

I hope you liked my post. If yes, then please give it a clap. It would encourage me to write more. You can also read my post on Probability for Machine learning by clicking on the link below .



Join Hacker Noon

Create your free account to unlock your custom reading experience.