---
# System prepended metadata

title: API

---

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 toghter and represent it as one pice for client.
In another word 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 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 ways. 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 a number of 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 of 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 (ethier 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 relay on our api does not shut down
    lets take an example of a fly booking company that uses our **V1** API endpoint if we directly update the endpoint **without updating** the **clinet** code the service will no longer works 
- **base path**. this comes after the version of the api and contains the name of service the clinet want to access
- **query**. comes after the base path but it used with adding **"?"** instead of " / "  so that we pass a 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 :")
 :::

