paint-brush
Working with APIs ( Concepts + Code )by@devonroll
1,709 reads
1,709 reads

Working with APIs ( Concepts + Code )

by KaranSeptember 2nd, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this post, we will discuss about how to get data from any API using any programming language. We will be using JavaScript and Python in this post but you can use any language of your choice. API’s Examples : Facebook (O Auth 2) Login API (URL address) Google Maps API (Google Maps API) Twitter data stream API (Twitter data stream) Stripe payment API (Stripe) and Stripe API (Payment API) are all open-source and free of cost.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Working with APIs ( Concepts + Code )
Karan HackerNoon profile picture

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)=>{
    fetch(url)
    .then((response)=>response.json())
    .then((data)=>console.log(data))
    .catch((err)=>console.log(err))
}

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

// # Output :

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

https://medium.com/@karan.02031993/javascript-essentials-concepts-code-for-frontend-and-backend-developers-8b0a97b525b7

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()
    print(data)

## Output

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

https://medium.com/@karan.02031993/pythonic-way-of-doing-things-ba1ef2047170

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 .

https://medium.com/r/?url=https%3A%2F%2Fhackernoon.com%2Fprobability-for-machine-learning-ed4b62e897f9