What is an API? | Lecture 9


What is an API? 

API (Application Programming Interface) is a way for two applications to communicate with each other.

Example:

  • Your Node.js app asks a server: “Give me weather data”

  • Server replies: “Here is the weather data”

This request–response process is called API integration.


Why We Use APIs in Node.js

  • To fetch live data (weather, news, stock prices)

  • To send data (login, signup, payments)

  • To connect with external services like Google, YouTube, WhatsApp, payment gateways


Types of APIs

  1. REST API – Most common, uses JSON

  2. SOAP API – Older, XML based

Node.js applications mostly work with REST APIs.


Tools Needed for API Integration in Node.js

  1. Node.js installed

  2. axios or fetch library


Install Required Package

Using Axios

npm install axios

Important API Terms

  • Endpoint – API URL

  • Method – GET, POST, PUT, DELETE

  • Headers – Extra information (API key, token)

  • Body – Data sent to server

  • Response – Data returned by API


Step 1: Import Axios

const axios = require('axios');

Step 2: GET API (Fetch Data)

Example: Fetch Users from Free API

API URL:

https://jsonplaceholder.typicode.com/users

Node.js Code

const axios = require('axios');

axios.get('https://jsonplaceholder.typicode.com/users')
  .then(response => {
    console.log(response.status);
    console.log(response.data);
  })
  .catch(error => {
    console.log(error.message);
  });


Step 3: Reading API Data

axios.get('https://jsonplaceholder.typicode.com/users')
  .then(response => {
    response.data.forEach(user => {
      console.log(user.name, user.email);
    });
  });


Step 4: GET API with Parameters

axios.get('https://jsonplaceholder.typicode.com/posts', {
  params: {
    userId: 1
  }
})
.then(response => {
  console.log(response.data);
});


Step 5: POST API (Send Data)

Example: Add New Post

axios.post('https://jsonplaceholder.typicode.com/posts', {
  title: 'Node.js API',
  body: 'Learning API integration',
  userId: 1
})
.then(response => {
  console.log(response.data);
});


Step 6: PUT API (Update Data)

axios.put('https://jsonplaceholder.typicode.com/posts/1', {
  title: 'Updated Title'
})
.then(response => {
  console.log(response.data);
});


Step 7: DELETE API

axios.delete('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => {
    console.log(response.status);
  });


Step 8: API with Headers (API Key Example)

axios.get('https://api.example.com/data', {
  headers: {
    Authorization: 'Bearer YOUR_API_KEY'
  }
})
.then(response => {
  console.log(response.data);
});


Step 9: Handle Errors Properly

axios.get('https://jsonplaceholder.typicode.com/users')
  .then(response => {
    console.log('Success');
  })
  .catch(error => {
    console.log('Error:', error.response.status);
  });


Step 10: Real-Life Example – Weather API

axios.get('https://api.openweathermap.org/data/2.5/weather', {
  params: {
    q: 'Delhi',
    appid: 'YOUR_API_KEY',
    units: 'metric'
  }
})
.then(response => {
  console.log('Temperature:', response.data.main.temp);
});


Mini Project Idea 

Student Attendance API System

  • GET – Fetch students

  • POST – Mark attendance

  • PUT – Update attendance

  • DELETE – Remove student


Comments

Popular posts from this blog

Introduction to Node.js | Lecture 1

Introduction to NPM | Lecture 2

Modules in Node.js | Lecture 4