API === ![api](https://i.imgur.com/GOfnQdF.png) ## Table of Contents [TOC] ## What is an API? API (**A**pplication **P**rogramming **I**nterface) it's just a way to connect apps and represent them as one piece for a client. In other words, API is a **messenger** that takes your **request** to the server to process it and get it back to you with the needed information ![how api works ](https://i.imgur.com/jYFqnY5.png) Rest API vs API --- An **API**, or application programming interface, is a set of rules that define how applications or devices can connect to and communicate with each other. A **REST API** is an API that **conforms to the design principles** of the REST, or **representational state transfer** architectural style. For this reason, REST APIs are sometimes referred to as RESTful APIs> ![restful server](https://i.imgur.com/6tzSORT.png) REST design principles --- 1. **Uniform interface**. All API requests for the same resource should look the same, no matter where the request comes from. The REST API should ensure that the same piece of data, such as the name or email address of a user, belongs to only one uniform resource identifier (URI). Resources shouldn’t be too large but should contain every piece of information that the client might need. 2. **Client-server decoupling**. In REST API design, client and server applications must be completely independent of each other. The only information the client application should know is the URI of the requested resource; it can't interact with the server application in any other way. Similarly, a server application shouldn't modify the client application other than passing it to the requested data via HTTP. 3. **Statelessness**. REST APIs are stateless, meaning that each request needs to include all the information necessary for processing it. In other words, REST APIs do not require any server-side sessions. Server applications aren’t allowed to store any data related to a client request. 4. **Cacheability**. When possible, resources should be cacheable on the client or server side. Server responses also need to contain information about whether caching is allowed for the delivered resource. The goal is to improve performance on the client side while increasing scalability on the server side. 5. **Layered system** architecture. In REST APIs, the calls and responses go through different layers. As a rule of thumb, don’t assume that the client and server applications connect directly to each other. There may be several different intermediaries in the communication loop. REST APIs need to be designed so that neither the client nor the server can tell whether it communicates with the end application or an intermediary. 6. **Code on demand** (optional). REST APIs usually send static resources, but in certain cases, responses can also contain executable code (such as Java applets). In these cases, the code should only run on demand. ## CRUD operations ![](https://i.imgur.com/D1vEiZG.png) CRUD refers to the four basic operations a software application should be able to perform – **Create, Read, Update, and Delete.** You can think about CRUD in this way: - You create a social account and fill in your information - **CREATE** - You get access to the information you entered and people can search for you – **READ** - You get a new job at Google and changed your employment status to employed – **UPDATE** - You get tired of social media toxicity and delete your account - **DELETE** ## Real-world example ![](https://i.imgur.com/RDmZxL7.png) The **URI** of an API contains different parts - **connection protocol**. such as HTTP or HTTPS or TCP... - **Host name**. such as Google, Facebook, etc - **Endpoint type**. sometimes it's better to put a definition for the endpoint type (either its an API endpoint or not) - **Version**. for a good design for an endpoint we must add a version of an API so that when we update an API endpoint the services that rely on our API do not shut down let us take an example of a fly booking company that uses our **V1** API endpoint if we directly update the endpoint **without updating** the **client** code the service will no longer work - **base path**. this comes after the version of the API and contains the name of the service the client wants to access - **query**. comes after the base path but it is used by adding **"?"** instead of " / " so that we pass parameters for the endpoint :::Warning ### https://www.google.com/search?q=api this is a real-world example of google's API that contains the part that I explained above. ::: ## what is next? you should watch these videos :) - https://www.youtube.com/watch?v=lsMQRaeKNDk - https://www.youtube.com/watch?v=hWRRdICvMNs - https://www.youtube.com/watch?v=ZtTIuhyRqvw :::info HOPE THAT HELPS :") :::