Activity 28: Research Rest API
What is an RESTful API:
- According to AWS: RESTful API: Two computer systems can securely communicate information over the internet by using a RESTful API interface. To carry out different duties, the majority of business apps must interface with both internal and external applications. For instance, in order to automate invoicing and interact with an internal time sheet application, your internal accounts system must exchange data with your customers' banking system in order to issue monthly payslips. Because RESTful APIs adhere to safe, dependable, and effective software communication standards, they facilitate this information flow.
How Does it Work?:
A RESTful API's primary purpose is identical to that of internet browsing. When the client needs a resource, it uses the API to get in touch with the server. In the server application API documentation, API developers describe how the client should utilize the REST API. The general procedures for any REST API call are as follows:
The client sends a request to the server. The client follows the API documentation to format the request in a way that the server understands.
The server authenticates the client and confirms that the client has the right to make that request.
The server receives the request and processes it internally.
The server returns a response to the client. The response contains information that tells the client whether the request was successful. The response also includes any information that the client requested.
Key Principles:
Statelessness: All the information required to process each client request is included. The server does not save the client context in between requests.
Client-Server Separation: The user interface is managed by the client, whilst data and actions are handled by the server.
Uniform Interface: Standard HTTP methods (GET, POST, PUT, DELETE) are used for interactions, and URIs are used to identify resources in requests.
Cacheability: To increase effectiveness and performance, responses must specify if they can be cached.
Layered System: The architecture of the API permits middlemen (such as load balancers) without compromising communication between clients and servers.
Code on Demand (optional): To increase functionality, servers can provide clients with executable code.
Architecture:
Resources and URIs: The architecture treats everything as a resource, identified by a Uniform Resource Identifier (URI). Each resource is a specific entity (like a user, product, or document), accessed through its unique URI.
HTTP Methods: REST uses standard HTTP methods for operations on resources:
GET: Retrieve a resource.
POST: Create a new resource.
PUT: Update an existing resource.
DELETE: Remove a resource.
Stateless Interactions: Each request from a client must contain all necessary information (like authentication or data), as the server does not retain client context between requests.
Representation of Resources: Resources can be represented in multiple formats, typically JSON or XML. Clients and servers exchange these representations, not the actual resources.
Client-Server Decoupling: This allows for autonomous development and scalability by separating the client and server. The client controls the user interface and experience, while the server supplies the resources.
Layered Architecture: The system can include several levels of intermediaries, such as load balancers, gateways, or proxies, without compromising client-server communication.
Caching: By designating responses as cacheable, clients can reuse data without repeatedly contacting the server, increasing productivity and lowering demand.
Best Practices:
Use Meaningful Resource Names: URIs should be nouns, not actions (e.g.,
/users/123
, not/getUser/123
), and plural (e.g.,/users
, not/user
).HTTP Status Codes: Always return appropriate status codes:
200 OK
: Success.201 Created
: Resource successfully created.400 Bad Request
: Invalid request data.404 Not Found
: Resource doesn't exist.500 Internal Server Error
: Server-side issue.
Versioning: Use versioning in your API (e.g.,
/v1/users
) to avoid breaking changes when upgrading.Pagination and Filtering: When returning large datasets, implement pagination (e.g.,
limit
andoffset
parameters) and allow filtering (e.g.,/users?status=active
).Make sure that
PUT
,DELETE
, andPATCH
requests are idempotent, which means that they may be made repeatedly without producing different outcomes.Security: To safeguard endpoints, use authentication (such as OAuth2 or API keys) and encrypt data while it's in transit using HTTPS.
Consistent Naming Conventions: Use hyphens and lowercase letters in URIs (
/orders/{order-id}
) and name them according to a standard pattern.Error Handling: Give meaningful error messages in a standardized format (for example, JSON containing error data).
Some example uses of RESTful API:
User Authentication: Apps like Facebook and Google use REST APIs to authenticate users via OAuth, allowing secure login without storing passwords.
E-Commerce: Online stores use REST APIs to manage product catalogs, process orders, and update inventory. For example, a mobile shopping app can retrieve product details from the server using a REST API.
Social Media Integration: Apps like Twitter and Instagram provide REST APIs for fetching posts, publishing content, or managing followers from third-party apps.
Cloud Storage: Services like Dropbox and Google Drive use REST APIs to let users upload, download, or manage files in the cloud directly from apps.
Weather or Map Services: Apps like Google Maps or weather apps use REST APIs to fetch real-time location data, weather forecasts, or geolocation information.
Subscribe to my newsletter
Read articles from froilan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by