# Learn about Apis (Beginners)

An API (Application Programming Interface) is a set of protocols, routines, and tools for building software and applications. It specifies how software components should interact, allowing for communication between different systems, and enabling the retrieval of data or the execution of specific functions.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674997998361/b91d20c0-3c8e-4d53-ab43-76aece720825.png align="center")

### How do Apis work?

APIs work by defining a set of rules and protocols for communication between different systems. When a client application wants to access data or functionality provided by a server, it sends a request to the server's API. The request specifies the desired action and any parameters required to perform that action.

The server processes the request performs the specified action (such as retrieving data from a database) and returns a response to the client application. The response typically contains the results of the action, formatted according to the API's specifications.

APIs often use HTTP (Hypertext Transfer Protocol) for communication, with the client application sending an HTTP request to the API endpoint and the API returning an HTTP response. Some APIs use other protocols or a combination of protocols.

Most APIs use JSON as the data format for requests and responses. It's important to know how to parse and read JSON data.

APIs also often include specifications for authentication, allowing the server to identify the client application and authorize or restrict access to certain data or functionality. This helps ensure that only authorized client applications can access the server's data and functionality.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674998285500/e075dcdc-f7d6-450d-88eb-1212117bfae6.png align="center")

### Request in an API

A request in an API is a message sent by a client application to the API endpoint, asking the server to perform a specific action, such as retrieving data or executing a function. The request includes information about the desired action and any parameters required to perform that action.

A request to an API typically has the following components:

1. Endpoint: The URL of the API endpoint to which the request is sent.
    
2. Method: The HTTP method (such as GET, POST, PUT, DELETE) is used to send the request, which determines the type of action being requested.
    
3. Headers: Additional information about the request, such as the format of the data being sent, the accepted response format, and authentication details.
    
4. Body: The payload of the request, which contains data or other information relevant to the requested action.
    

Once the request is received by the server, the API processes the request performs the desired action and returns a response to the client application. The format of the response will be defined by the API specification.

***Http Status Codes:***

1. [Informational responses](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#information_responses) (`100` – `199`)
    
2. [Successful responses](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#successful_responses) (`200` – `299`)
    
3. [Redirection messages](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages) (`300` – `399`)
    
4. [Client error responses](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses) (`400` – `499`)
    
5. [Server error responses](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) (`500` – `599`)
    

Here's a code to perform a GET request to an API using the `fetch` API in JavaScript:

```javascript
// Define the API endpoint
const endpoint = 'https://api.example.com/data';

// Make a GET request to the endpoint
fetch(endpoint)
  .then(response => {
    // Check if the response was successful
    if (!response.ok) {
      throw new Error(`Request failed with status code: ${response.status}`);
    }
    // Parse the response as JSON
    return response.json();
  })
  .then(data => {
    // Do something with the data
    console.log(data);
  })
  .catch(error => {
    // Handle the error
    console.error(error);
  });
```

This code sends a GET request to the API endpoint at [`https://api.example.com/data`](https://api.example.com/data) using the `fetch` function. The response is returned as a Promise, which can be resolved using the `then` method. In this example, we check if the response was successful by checking the `ok` property of the response object. If the response was not successful, we throw an error. If the response was successful, we parse the response as JSON using the `JSON` method. Finally, we access the response data and log it to the console. If there was an error, we log the error to the console using the `catch` method.

Here's a code to perform a GET request to an API using Nodejs and the `Axios` library:

```javascript
const axios = require('axios');

// Define the API endpoint
const endpoint = 'https://api.example.com/data';

// Make a GET request to the endpoint
axios.get(endpoint)
  .then(response => {
    // Access the response data
    const data = response.data;
    // Do something with the data
    console.log(data);
  })
  .catch(error => {
    // Handle the error
    console.error(error);
  });
```

This code sends a GET request to the API endpoint at [`https://api.example.com/data`](https://api.example.com/data) using the `axios.get` function. The response is returned as a Promise, which can be resolved using the `then` method. In this example, we access the response data using the `data` property and log it to the console. If there was an error, the `catch` method will be called, and we log the error to the console.

Similarly, we can perform more requests like POST, PUT, DELETE, etc.

*Thanks for reading!*
