DEV Community

Cover image for API in JavaScript: Connecting Your Code to the World
Karthick Karthick
Karthick Karthick

Posted on

API in JavaScript: Connecting Your Code to the World

An API (Application Programming Interface) in JavaScript is a way for your code to communicate with other software, services, or systems.

In simple words:
API = Bridge between your JavaScript and external data/services

Real-Time Example

In Pharmacy E-Commerce :

User clicks “Buy Now”
JS sends request to payment API (like Razorpay)
API processes payment
JS gets response → shows Success / Failed

API Methods in JavaScript

GET (Read Data)
Used to fetch data from server

fetch("https://api.example.com/products")
  .then(res => res.json())
  .then(data => console.log(data));
Enter fullscreen mode Exit fullscreen mode

POST (Create Data)
Used to send new data to server

fetch("https://api.example.com/products", {
  method: "POST",
  body: JSON.stringify({ name: "Tablet", price: 100 }),
  headers: { "Content-Type": "application/json" }
});
Enter fullscreen mode Exit fullscreen mode

PUT (Update Full Data)
Used to update entire data

fetch("https://api.example.com/products/1", {
  method: "PUT",
  body: JSON.stringify({ name: "Tablet", price: 120 }),
  headers: { "Content-Type": "application/json" }
});
Enter fullscreen mode Exit fullscreen mode

PATCH (Update Partial Data)
Used to update only specific fields

fetch("https://api.example.com/products/1", {
  method: "PATCH",
  body: JSON.stringify({ price: 120 }),
  headers: { "Content-Type": "application/json" }
});
Enter fullscreen mode Exit fullscreen mode

DELETE (Remove Data)
Used to delete data

fetch("https://api.example.com/products/1", {
  method: "DELETE"
});
Enter fullscreen mode Exit fullscreen mode

Types of APIs in JavaScript (Quick View)

  • Browser APIs → DOM, Geolocation
  • Third-party APIs → Payment (Razorpay), Maps
  • Custom APIs → Your backend (Python/Node)

Top comments (0)