In the programming world, an API (Application Programming Interface) acts as an "interface" defining how one program communicates with another. APIs are used to exchange data between different systems. But the term API is very broad and has different types. In this article, we'll explain API, REST API and RESTful API in detail, clarify the differences between them and touch on other API types.
1. API (Application Programming Interface): What Is It?
An API is an interface designed for exchanging data between two programs. In other words, it's a set of rules defining how one program "talks" to another.
How Does It Work?
An API allows one program to call another program's functions. For example:
- Rules a mobile app uses to get data from a server.
- A website showing a map using the Google Maps API.
API Types
APIs can be classified by the technology used:
- Web APIs: APIs using the HTTP protocol. E.g.: REST API, GraphQL API.
- Library/API Frameworks: Ready-made function collections that simplify programmers' code. E.g.: jQuery, React.
- Operating System APIs: APIs for accessing operating system functions. E.g.: Windows API.
- Database APIs: APIs used to connect to databases. E.g.: JDBC, ODBC.
Example
An API for a bookstore:
GET /books– Gets the list of books.POST /books– Adds a new book.
2. REST API: What Is It?
A REST (Representational State Transfer) API is an architecture for exchanging data using the HTTP protocol. REST APIs work with resources (data) using HTTP methods (GET, POST, PUT, DELETE).
Core Principles of REST
- Statelessness: Every request is completely independent. The server stores no information about previous requests. Example: When a user logs in, login credentials must be sent every time.
- Client-Server Architecture: Client and server are separate. The client gets data from or sends data to the server.
- Uniform Interface: Resources are represented with URLs.
Example:
/users– user list,/users/1– the user with ID 1. - Cacheability: Responses sent by the server can be cached. This improves performance.
- Layered System: The system may have multiple layers (proxy, gateway, etc.).
- Code on Demand: The server can dynamically send needed code (JavaScript etc.) to the client.
HTTP Methods
REST APIs work with resources using HTTP methods:
GET: To get data.POST: To create new data.PUT: To update existing data.DELETE: To delete data.
Example
A REST API for a bookstore:
GET /books– Gets the list of books.POST /books– Adds a new book.PUT /books/1– Updates the book with ID 1.DELETE /books/1– Deletes the book with ID 1.
3. RESTful API: What Is It?
A RESTful API means an API built in full compliance with REST principles. Not every REST API is automatically RESTful. For an API to be RESTful, it must follow all REST principles.
REST vs RESTful Difference
- REST API: An API using REST principles.
- RESTful API: An API built in full compliance with REST principles.
Key Characteristics of RESTful APIs
- Statelessness: Every request is completely independent.
- Client-Server Architecture: Client and server are separate.
- Cacheability: Server responses can be cached.
- Uniform Interface: A standard interface for resources (e.g.
/usersand/users/1). - Layered System: The system may have multiple layers (proxy, gateway, etc.).
Example
- REST API: May use URLs like
/getUsersand/createUser(not fully REST-compliant). - RESTful API: Uses standard URLs like
/users(GET),/users(POST),/users/{id}(PUT),/users/{id}(DELETE).
4. Other API Types
1. SOAP API
SOAP (Simple Object Access Protocol) is an XML-based API protocol. Unlike REST, SOAP is a more structured and heavy protocol. It's typically used in banking systems and enterprise-level applications.
Characteristics:
- Transfers data in XML format.
- Uses the WS-Security standard for security.
- Can work stateful and stateless.
2. GraphQL API
GraphQL is an API query language developed by Facebook. Unlike REST, the client can request only the data it needs.
Characteristics:
- The client can select needed data.
- Single Endpoint: All requests go through the same URL.
- Supports WebSocket for real-time data.
3. RPC API
RPC (Remote Procedure Call) is an API type that lets one program call a function in another program. Typically used in JSON-RPC or XML-RPC formats.
Characteristics:
- Simple and minimal.
- Unlike REST, works on functions.
5. REST API vs SOAP API vs GraphQL API
- REST API: HTTP-based, simple and widely used.
- SOAP API: XML-based, structured and ideal for security.
- GraphQL API: Client-oriented, flexible and supports real-time.
Conclusion
- API: A general concept defining how any program communicates with another.
- REST API: A type of API using the HTTP protocol and REST principles.
- RESTful API: An API built in full compliance with REST principles.
- Other API Types: APIs like SOAP, GraphQL and RPC also exist. Each has its own advantages and limitations.
Good luck! 🚀
