Site icon Full-Stack

How to Integrate APIs in a Full Stack Web App (With Example)

Users expect smart features and real-time data in every application in the digital age. Application Programming Interfaces, or APIs, are useful in this situation. APIs power a plethora of functions in contemporary online programs, ranging from payment gateways to weather updates.

This blog post will teach you how to include an API into a full stack application using a straightforward, useful example that uses Node.js with Express for the backend and React for the frontend.

🚀 What is API Integration?

Connecting your application to external services in order to send or retrieve data is known as API integration. It enables developers to increase an application’s capability without having to start from scratch when constructing additions.

🧱 Tech Stack Overview

💡 Why Use APIs in Web Apps?


📘 Real-World Example: Weather App with API Integration

Let’s create a basic weather app. When a user inputs the name of a city, the app uses a full stack technique to retrieve real-time meteorological data from an API.


🔧 Step 1: Backend Setup (Node.js + Express)

🔹 Initialize the Project

npm init -y

npm install express axios cors

🔹 Create server.js

tconst express = require(“express”);

const axios = require(“axios”);

const cors = require(“cors”);

const app = express();

app.use(cors());

const API_KEY = “your_api_key_here”;

app.get(“/api/weather/:city”, async (req, res) => {

  const city = req.params.city;

  try {

    const response = await axios.get(

      `https://api.example.com/weather?q=${city}&appid=${API_KEY}&units=metric`

    );

    res.json(response.data);

  } catch (err) {

    res.status(500).json({ error: “Failed to fetch data.” });

  }

});

app.listen(5000, () => {

  console.log(“Backend is running on port 5000”);

});


🖥️ Step 2: Frontend Setup (React)

🔹 Create React App and Install Axios

npx create-react-app weather-client

cd weather-client

npm install axios

🔹 Modify App.js

import React, { useState } from “react”;

import axios from “axios”;

function App() {

  const [city, setCity] = useState(“”);

  const [weather, setWeather] = useState(null);

  const fetchWeather = async () => {

    try {

      const res = await axios.get(`http://localhost:5000/api/weather/${city}`);

      setWeather(res.data);

    } catch (err) {

      alert(“Could not fetch weather data.”);

    }

  };

  return (

    <div style={{ padding: “2rem”, textAlign: “center” }}>

      <h2>Simple Weather App</h2>

      <input

        type=”text”

        placeholder=”Enter city name”

        onChange={(e) => setCity(e.target.value)}

      />

      <button onClick={fetchWeather}>Get Weather</button>

      {weather && (

        <div style={{ marginTop: “1rem” }}>

          <h3>{weather.name}</h3>

          <p>{weather.weather[0].description}</p>

          <p>{weather.main.temp}°C</p>

        </div>

      )}

    </div>

  );

}

export default App;


▶️ Step 3: Run Your Application

node server.js

bash

CopyEdit

npm start

Now open your browser, enter a city name, and get live weather updates!


🧠 Best Practices for API Integration


📌 More Real-World Use Cases


✅ Conclusion

A key competency in full stack development is API integration. It enables your app to integrate with strong third-party services, improving functionality and user experience.

Your web apps can reach their maximum potential with just a few lines of code.

You might be like this:-

Top 10 JavaScript Frameworks to Learn in 2025 

What Is Artificial Intelligence? A Beginner’s Guide

What is MySQL?

Exit mobile version