🌐 Introduction to APIs in JavaScript
An API (Application Programming Interface) allows different software applications to talk to each other. In JavaScript, APIs are commonly used to fetch data from external services like weather, news, or databases.
🔗 What is an API?
Think of an API like a waiter in a restaurant. You (the client) ask for something (like food), and the waiter (the API) takes your request to the kitchen (server) and brings back your response (data).
- Client: Your browser or app
- API: The messenger that sends and receives data
- Server: The system that processes the request
📥 What You Can Do With APIs
- Get weather forecasts, news headlines, stock prices
- Send or receive messages from databases
- Process payments, log in with Google/Facebook
- Build interactive dashboards and data visualizations
📦 Common API Response Format: JSON
APIs often send data in JSON (JavaScript Object Notation)
format:
{
"name": "John",
"age": 30,
"city": "New York"
}
JavaScript can easily parse and use JSON data using:
const jsonData = '{"name":"John","age":30}';
const obj = JSON.parse(jsonData);
console.log(obj.name); // "John"
⚙️ Tools You’ll Use with APIs
fetch()
– Built-in method to request dataXMLHttpRequest
– Older method for AJAX- Libraries like
Axios
for simplified requests
✅ Summary
- APIs help JavaScript communicate with external services
- They return data, usually in JSON format
- You can fetch data using built-in tools like
fetch()
- APIs power modern web apps — from weather to login to search