💬 Join the DecodeAI WhatsApp Channel for more AI updates → Click here

Mastering APIs: A Simplified Guide for Product Managers (2/3)

Mastering APIs: A Simplified Guide for Product Managers (2/3)

API Saga continues...



3. Components of an API?

Imagine you're sending a message to a friend. You (the API Client) write down what you need (API Request) and give it to the messenger (API Server). The messenger takes your message to your friend (API Server processes the request). Your friend reads your message, does what you asked, and sends a reply back (API Response). Finally, the messenger brings the reply to you (API Client gets the response). So, the API Client asks for something, the API Server does it, and then the API Client gets the result.

The components of an API encompass crucial elements that facilitate communication and interaction between different software systems. The 'API Client serves' as the initiator, representing the application or system that seeks to utilize the API's capabilities.

There are four main components of APIs:

a. API Client
b. API Request
c. API Server
d. API Response

a. API client

Imagine you're a detective trying to solve a mystery. You have a trusty assistant (API client) who helps you gather clues from different sources without tipping off the suspects.

Your assistant knows how to talk to various experts (API servers) in different fields like forensics, technology, and witness testimonies. When you need information, you give instructions to your assistant, like asking for fingerprints, CCTV footage, or alibis. Your assistant then contacts the experts discreetly, collects the information you requested, and presents it to you in an organized manner.

What's fascinating is that your assistant not only gathers information but also knows how to interpret it. For example, if you ask for fingerprint analysis, your assistant doesn't just hand you a bunch of fingerprints; it highlights any matches or suspicious patterns.

In this scenario, your assistant (API client) acts as the intermediary between you (the developer) and the experts (API servers), making it easier for you to gather and make sense of the data you need to solve the mystery (build your application).

A real-world example of API Client:

A real-world example of an API client is a weather app on your smartphone.

When you open the weather app and request the current weather for your location, the app acts as the API client. It sends a request to the weather API server, asking for the latest weather data. The weather API server then processes the request and sends back a response containing the current weather conditions, such as temperature, humidity, and wind speed.

The weather app then takes this response and displays it to you in a user-friendly format, showing you the current weather information on your screen. In this scenario, the weather app serves as the API client, initiating requests to the weather API server and handling the data returned by the server to provide you with the information you need.

b. API request

An API client is responsible for sending API requests in response to user actions or external events, but what, exactly, is an API request? An API request will look and behave differently depending on the type of API. That being said, an API request to a REST API is comprised of the following components:

  1. Endpoint
  2. Parameters
  3. Request headers
  4. Methods
  5. Request body


  1. Endpoint:

    An API endpoint is a specific URL where an API client interacts with an API server to access certain data or functionality. Think of it as the address where requests are sent to retrieve or manipulate resources. For instance, in an e-commerce app, the endpoint /products handles everything related to product information. When the client needs product details, it sends a request to this URL, and the server processes it accordingly. Essentially, the endpoint acts as the connection point for communication between the client and the server.

Example: Let's take https://api.example.com as the base URL for each endpoint below:

Endpoints of Products for Online store API: 

  • GET https://api.example.com/products – Retrieve a list of products
  • POST https://api.example.com/products – Add a new product
  • GET https://api.example.com/products/:id – Retrieve a specific product
  • PUT https://api.example.com/products/:id – Update an existing product
  • DELETE https://api.example.com/products/:id – Delete a specific product

How do API endpoints work?

API endpoints work by connecting API clients and servers — and handling the transfer of data between them. A well-designed API should have clear and intuitive endpoints that provide a predictable way for clients to interact with the server’s resources.
endpoint

  1. Parameters

Parameters are the variables you pass to an API endpoint to give specific instructions on how to process your request. They can be included in the URL, query string, or request body.

For example, consider an e-commerce API with a /products endpoint. If you want to retrieve only blue products, you might use a “color” parameter. When you set this parameter to "blue," the API knows to return only products of blue color.

Parameters help customize your request, allowing you to filter, sort, or specify the data you need. They are essential for making precise queries and are typically added to the URL, query string, or request body. This way, the API knows exactly what information or action you’re asking for.

Example:

Let's consider a messaging app where users can send messages to each other. When you send a message "Hello!" to your friend "Aliya," the app sends an API request to the messaging API's endpoint (/messages) with the following parameters included in the request URL:

  • Sender: YourName
  • Recipient: Aliya
  • Message: Hello!

The API request looks like this:

POST /messages?sender=YourName&recipient=Aliya&message=Hello!

The messaging API server receives the request, processes it, and sends back a response confirming that the message was sent successfully. The response body might look like this:

{
  "status": "success",
  "message": "Your message has been sent to Aliya."
}

So, in this example:

  • API Request: POST /messages (with parameters sender, recipient, and message included in the URL)
  • API Endpoint: /messages
  • API Response: Response confirming the message was sent successfully (with response body containing status and message)

Example 2:

API Endpoint:
/search

Suppose we have a search API that allows you to search for books. This API might have parameters to customize your search, such as the title, author, or publication year.

API Endpoint: /search

Parameters:

  • title (to specify the title of the book)
  • author (to specify the author of the book)
  • year (to specify the publication year of the book)
  • GET /search?title=Harry%20Potter&author=J.K.%20Rowling&year=2000

In this example, you’re using the /search endpoint to look for books. The parameters are included in the request:

{
"endpoint": "/search",
"method": "GET",
"parameters": {
"title": "Harry Potter",
"author": "J.K. Rowling",
"year": 2000
}
}

  • title=Harry%20Potter specifies that you're looking for books with "Harry Potter" in the title.
  • author=J.K.%20Rowling specifies that you want books written by J.K. Rowling.
  • year=2000 specifies that you're interested in books published in the year 2000.

The API processes this request and returns a response containing books that match your search criteria:

[ { "title": "Harry Potter and the Goblet of Fire", "author": "J.K. Rowling", "year": 2000 }, ]

In this way, API parameters allow you to tailor your request, making it more specific and getting the relevant data you’re interested in.

3. Request headers:

Request headers are like special instructions attached to an API request. They provide additional information about the request to the API server. For example, the "Accept" header tells the server what kind of response format the client prefers, while the "Authorization" header includes credentials for authentication.

Example Request with Headers (in JSON):

{
  "endpoint": "/articles",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "Authorization": "Bearer your_access_token"
  },
  "body": {
    "title": "New Product Launch",
    "description": "Exciting announcement about our latest product!",
    "price": 99.99
  }
}

In this example:

  • Endpoint: /articles
  • Method: POST
  • Headers:
    • Content-Type: application/json
      • This header tells the server the format of the data in the request body. Here, it indicates that the data is in JSON format.
    • Authorization: Bearer your_access_token
      • In the header Authorization: Bearer your_access_token, the term "Bearer" is a type of authentication method. It indicates to the server how the client (the entity making the request) is authenticating itself. The "your_access_token" part is where you would typically include an actual access token.

        An access token is a unique identifier that grants access to certain resources or actions on the server. It serves as proof of identity for the client. For example, when you log into a website using your username and password, the server may issue you an access token. This token is then sent with subsequent requests to prove that you are authorized to access certain features or data.
  • Body: Contains the actual data needed to create a new article. In this case, it includes the title, description, and price of the product being launched.

Once the client sends the request to the appropriate endpoint, the API server authenticates it, validates the input, retrieves or manipulates the relevant data, and returns the response to the client. The response typically includes a status code, which indicates the result of the request, as well as a body, which contains the actual data that the client requested (if the request was successfully executed).

  1. Methods

    In a REST API, several HTTP methods are commonly used to perform different operations on resources. Here are the main ones:

Methods used in a REST API:

  1. GET: This method is used to retrieve data from the server. It's like asking for information. For example, you might use a GET request to retrieve a list of all users or a specific user's details.
  2. POST: This method is used to send data to the server to create a new resource. It's like submitting a form. For example, you might use a POST request to create a new user or add a new item to a shopping cart.
  3. PUT: This method is used to send data to the server to update an existing resource. It's like editing an existing document. For example, you might use a PUT request to update a user's information or modify a product's details.
  4. DELETE: This method is used to request the removal of a resource from the server. It's like deleting a file. For example, you might use a DELETE request to remove a user from the system or delete a specific item from a list.
  5. PATCH: This method is used to apply partial modifications to a resource. It's like making minor edits. For example, you might use a PATCH request to update only certain fields of a user's profile without affecting the rest.
  6. OPTIONS: This method is used to request information about the communication options available for a resource or server. It's like asking the server what methods are supported for a particular resource. This can be useful for clients to understand what actions they can perform on a resource.
  7. HEAD: This method is similar to a GET request, but it only retrieves the headers of the response, not the actual content. It's like peeking at the top of a package without opening it. This can be useful for clients to check the status or metadata of a resource without downloading the entire payload.

These HTTP methods provide a standardized way to interact with resources in a RESTful API, allowing clients to perform various CRUD (Create, Read, Update, Delete) operations efficiently and effectively.


5. Request Body:

You're ordering a pizza online. When you place your order, you provide specific details about the pizza you want, such as the type of crust, toppings, and size. In this scenario, the order details you provide are similar to a request body in an API request.

Example:

Let's say you're using an API to create a new user account on a website. You need to provide some information about the user, such as their username, email address, and password.

In this case, the request body would contain the data needed to create the user account. Here's how it might look in JSON format:

{
  "username": "example_user",
  "email": "user@example.com",
  "password": "secure_password123"
}

In this example:

  • Request Body: Contains the actual data (username, email, password) required to create the user account in JSON format.

So, when you send this request to the API endpoint responsible for creating user accounts, the server will use the data in the request body to create a new user with the provided details.

Story continues...

💬 Join the DecodeAI WhatsApp Channel
Get AI guides, bite-sized tips & weekly updates delivered where it’s easiest – WhatsApp.
👉 Join Now