Understanding Web API

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Understanding

Web API

Muhammad Rafaqat
linkedin.com/in/muhammad-rafaqat-ali/ @codewithrafaqat
API
What is an API ?

A web API is a set of protocols that defines rules for


communication between software systems, enabling
them to send and receive data.

Think of it as a contract that specifies how two


applications can talk to each other.

Example:
When you log in to a website using Google, an API is used
to securely retrieve your account information.

let’s look at some of the most commonly used APIs


Types of Web APIs

APIs aren’t all the same and come in various forms, each serving
distinct purposes and audiences. Here’s a breakdown of the main
categories of APIs, focusing on access levels, architecture, and
protocols.

1. Based on Access Levels

Public APIs
Private APIs
Partner APIs

2. Based on Protocols

REST APIs
GraphQL APIs
SOAP

3. Based on Architecture

Monolithic APIs
Microservices APIs

M.Rafaqat
linkedin.com/in/muhammad-rafaqat-ali/
Types of Web APIs
Browser API

Browser APIs consist of a collection of Web APIs offered by web browsers,


enabling web developers to engage with various browser features and
functionalities.

Examples: Console.log, Fetch( ), GeoLocation API, and setTimeout( ).

Server API

A server API serves as an interface that enables client applications—such as


web browsers or mobile apps to communicate with a server.

It acts like waitstaff in a restaurant, receive requests for data from client and
relay those requests to the server. Once the server replies, the API channels
that information back to your app.

Examples: REST and GraphQL.

Third-Party APIs

A third-party API is an interface offered by companies to integrate their


features or data into your applications.

Instead of building services from scratch for functions like payments or social
media posting, developers can use these APIs for easy access.

Examples: Google Maps, Stripe, OpenWeatherMap, Twitter, and others.


Authentication and Security
APIs often require authentication to protect data. Common methods include API keys,
OAuth, and JWT (JSON Web Tokens)
Based on Architecture

1. Monolithic APIs

Monolithic APIs are built as a single, unified application. This architecture


simplifies deployment and management but can become unwieldy as
applications grow, leading to challenges in scalability and maintenance.

Example: A traditional e-commerce site where all functionalities—product


listings, checkout, and user management—are part of a single codebase.

2. Microservices APIs

Microservices APIs are designed as a collection of loosely coupled services,


each responsible for a specific function. This architecture allows for greater
flexibility, scalability, and easier updates, as each service can be developed
and deployed independently.

Example: A large application where user authentication, product catalog,


and payment processing are handled by separate microservices.
Based on Access Levels

1. Public APIs

Also known as open APIs, public APIs are accessible to anyone. They are
designed to be used by third-party developers, allowing for broader
integration and innovation. Examples include social media APIs like Twitter and
Facebook, which enable external applications to interact with their platforms.

Example: A weather service providing an API for developers to access real-


time weather data for their apps.

2. Private APIs

Private APIs, or internal APIs, are restricted for use within an organization.
They facilitate communication between internal systems and services,
improving efficiency and integration among internal applications.

Example: An organization’s internal API that connects its inventory


management system with the sales platform.

3. Partner APIs

These APIs are shared with specific partners or clients, allowing controlled
access to certain functionalities or data. They are often used in B2B scenarios
where companies collaborate and share resources.

Example: An e-commerce platform providing API access to a select group


of retail partners for inventory management.
API Endpoints

An API endpoint is a specific URL where an API can be accessed by a


client application. Each endpoint corresponds to a specific function
or resource.

Structure of an API Endpoint

Typically, an API endpoint consists of:

Base URL: The root address of the API (e.g., https://api.example.com).


Path: The specific resource or action you want to access (e.g., /users or
/orders/123).
Query Parameters: Optional parameters that can modify the request (e.g., ?
sort=asc).

M.Rafaqat
linkedin.com/in/muhammad-rafaqat-ali/
Based on Protocols

1. REST APIs

Representational State Transfer (REST) APIs are based on standard HTTP


methods (GET, POST, PUT, DELETE) and are commonly used for web services.
They focus on resources and use stateless communication.

Use Case: A blog API that allows clients to fetch, create, or update posts
using standard HTTP requests

2. GraphQL APIs

GraphQL is a query language for APIs that enables clients to request only the
data they need. This flexibility allows for more efficient data retrieval and
reduces over-fetching.

Use Case: A social media platform that allows users to query for specific
data about friends, posts, and interactions.

3. SOAP APIs

Simple Object Access Protocol (SOAP) APIs use XML for message formatting
and are known for their strict standards and protocols. They are often used in
enterprise-level applications requiring high security and reliability.

Use Case: A banking system that needs to ensure secure and structured
data exchange between services.
REST API Example
A simple API for managing books.

Endpoints:

GET /api/books: Retrieve a list of all books.


GET /api/books/{id}: Retrieve a specific book by its ID.
POST /api/books: Add a new book

Request (GET all books):

Response

M.Rafaqat
linkedin.com/in/muhammad-rafaqat-ali/
GraphQL Example
The same book management system, but using GraphQL.

Query

Response

Key Differences

REST: You access specific endpoints to retrieve or manipulate resources. Each request is
independent and may return a fixed structure.

GraphQL: You query a single endpoint with a flexible structure, specifying exactly what data
you need. This reduces over-fetching and under-fetching issues.
Fetch API Example
In the below example, we Fetch API to access list of books from a given URL.

The fetch() method returns a promise that we handle using the “then” block.
First, we convert the data into the JSON format. After that, logs the result to
the console.

thank

you

You might also like