Building & Deploying your own API : A Hands-On Tutorial for Beginners

Madhu Sri Sushmitha Chowdary
4 min readJun 10, 2024

--

From retrieving live weather updates to checking stock prices and even enabling chat functionalities on websites, APIs make it all possible. In this hands-on tutorial, we will guide you through the process of building and deploying your own API, while exploring real-world examples of different APIs in action. Whether you’re a student, hobbyist, or professional, this guide will provide you with the foundational knowledge and practical skills needed to build and deploy your own API.

Getting Started

Before we dive in, it’s helpful to have a basic understanding of programming and web development. Familiarity with languages like JavaScript, Python, or HTML will be beneficial. However, our step-by-step approach ensures that even those new to programming will find the tutorial accessible and easy to follow.

Let’s embark on this exciting journey together. By the end of this tutorial, you will have created and deployed a fully functional API, ready to be integrated into your projects or showcased to potential employers. APIs are a powerful tool in a developer’s arsenal, and mastering them opens up a world of possibilities. So, let’s get started and unlock the potential of APIs!

Let’s consider how APIs facilitate communication between different systems.

  • Example: The OpenWeather API provides real-time weather data for any location in the world, which can be integrated into web and mobile apps.
Endpoint: https://api.openweathermap.org/data/2.5/weather
Method: GET
Headers: None required for this basic request
Parameters: q={city name}&appid={API_KEY}

Request:

GET https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY

Reponse:

{
"weather": [{
"description": "clear sky"
}],
"main": {
"temp": 282.55,
"feels_like": 281.86,
"temp_min": 280.37,
"temp_max": 284.26,
"pressure": 1023,
"humidity": 100
},
"name": "London"
}

Integrating APIs into a Website

Google Maps API Integration

Step 1: Get your API key from Google Cloud Platform.

Step 2: Add the following script in the HTML <head> section:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Maps Example</title>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" async defer></script>
<style>
#map {
height: 500px;
width: 100%;
}
</style>
</head>
<body>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap() {
var location = { lat: -34.397, lng: 150.644 };
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: location
});
var marker = new google.maps.Marker({
position: location,
map: map
});
}
</script>
</body>
</html>

Displaying Stock Data on a Website

Example: Using Alpha Vantage API.

Step 1: Get your API key from Alpha Vantage.

Step 2: Use the following JavaScript to fetch and display stock data.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stock Data</title>
<script>
async function getStockData() {
const apiKey = 'YOUR_API_KEY';
const symbol = 'AAPL';
const response = await fetch(`https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${symbol}&interval=5min&apikey=${apiKey}`);
const data = await response.json();
document.getElementById('stock').innerText = JSON.stringify(data, null, 2);
}
window.onload = getStockData;
</script>
</head>
<body>
<h3>Live Stock Data</h3>
<pre id="stock"></pre>
</body>
</html>

Weather-Based Theme Change

Example: Change the website theme based on the weather using OpenWeatherMap API.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather-Based Theme</title>
<style>
body.day {
background-color: lightyellow;
color: black;
}
body.night {
background-color: darkblue;
color: white;
}
</style>
<script>
async function changeThemeBasedOnWeather() {
const apiKey = 'YOUR_API_KEY';
const city = 'London';
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`);
const data = await response.json();
const currentTime = new Date().getHours();
const isDayTime = currentTime >= 6 && currentTime < 18;
if (isDayTime) {
document.body.className = 'day';
} else {
document.body.className = 'night';
}
}
window.onload = changeThemeBasedOnWeather;
</script>
</head>
<body>
<h3>Weather-Based Theme Change</h3>
</body>
</html>

Additional API Integrations

Integrating WhatsApp Chat

For integrating WhatsApp chat into your website, you can use the WhatsApp API for Business. This usually requires setting up through Facebook’s WhatsApp Business API.

Example: Basic WhatsApp chat link.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WhatsApp Chat</title>
</head>
<body>
<a href="https://wa.me/YOUR_PHONE_NUMBER" target="_blank">Chat with us on WhatsApp</a>
</body>
</html>

Mood-Based Audio Playlist

Example: Using Spotify’s API to fetch playlists based on mood.

Step 1: Get your API key from Spotify Developers Dashboard.

Step 2: Use the following JavaScript to fetch and display playlists.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mood-Based Playlist</title>
<script>
async function getMoodPlaylist(mood) {
const token = 'YOUR_SPOTIFY_ACCESS_TOKEN';
const response = await fetch(`https://api.spotify.com/v1/browse/categories/${mood}/playlists`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
const data = await response.json();
const playlist = data.playlists.items[0];
document.getElementById('playlist').innerText = `Playlist: ${playlist.name}`;
}
window.onload = () => getMoodPlaylist('chill');
</script>
</head>
<body>
<h3>Mood-Based Playlist</h3>
<div id="playlist"></div>
</body>
</html>

Start Creating Your Own APIs Today

Congratulations on reaching the end of this hands-on tutorial! Remember, the tech landscape is ever-evolving, and staying updated with the latest trends and best practices in API development will keep you ahead of the curve. Join developer communities, contribute to open-source projects, and keep experimenting.

So, roll up your sleeves and start creating your own APIs today. The world of API development is vast and full of opportunities — your journey has just begun!

Happy Developing!!! 😁😁

--

--

No responses yet