API Model for Search Service
Explore how to model a search API to meet key functional requirements, including endpoint definitions, HTTP methods, and data structures for client-server communication. Understand private API interactions with recommendation and ads services, plus error handling for a scalable search system.
In this lesson, we model the search API to meet the functional requirements we identified in the first lesson. We also focus on essential data entities to be exchanged in the request and response while targeting various endpoints.
Let's start the discussion by defining the base URL for our search API.
Base URL and API endpoints
The base URL can be defined as the common prefix for all the endpoints of API. We defined the following base URL for the search API:
The following illustration represents the URLs and their HTTP methods used by the search service.
The details of query string parameters that we mentioned in the URLs above are given below:
Search: The search keywords are passed as a
queryparameter along with the base URL.Sort: The
orderdefines the sequence of the records (such as descending or ascending). Additionally, thesortparameter sorts the results based on the field provided in the query parameter—for example, sort by thepriceordatefield.Pagination: This endpoint takes various parameters based on the type of pagination we aim to perform. For example, using the bucket pattern requires a
skipparameter in the query.
The message format for API endpoints
This section presents data entities, requests, and response structures for the messages exchanged between client and server to meet different functional requirements. Let’s start our discussion with the data entities required, as given below.
Search data entities
Data entities are the key-value pairs sent in the request and response messages between the client and endpoints to exchange data. We have listed the data entities we considered essential for our API. In reality, they can greatly vary in number based on the features the service offers.
The search query
The search query is sent through the URL in a call to the search API. We divide the search query into multiple parts as explained below:
HTTP method: Since we aim to retrieve data based on a query string, the
GETmethod is a suitable choice. TheGETrequest doesn't carry the body, so the search query is sent in the URL.
Can we use the POST method to request a search query?
- Request format: We’ve mentioned some essential headers, such as
HOST,X-API-KEY,Accept, and so on. The Accept header indicates that the client can accept data only in the JSON format. For authentication, we define API keys in the custom header, which isX-API-KEY. Also, theGETmethod has no body, and all the required data entities are added to the URL as parameters.
Note: If the request contains user credentials by passing the
Authorization: Bearer <JWT>header, the search will return personalized ads, recommendations, and results.
- Response format: The response header will contain some necessary information, like
Content-Type, whereas the response body could contain multiple data entities, such asresults,pageNumber, and so on.
Recommendation service
The recommendation service is only accessible internally, and its API calls are private to the organization. To understand the interaction between the API gateway and the recommendation service, we’ll discuss the request-response model of these calls here.
HTTP method: Since we need to fetch the recommendations based on the search query and user's query history, the
GETmethod is the obvious choice.Request format: The following request format is used to get recommendations.
Response format: If the request is successfully executed, the list of recommendations in the response body is given below. The
querytitlerepresents the title of the suggested query, andlinkrepresents the link of the suggested query.
Ads service
The ads service is also a private API whose calls are limited to the organization's internal use. Let's look at the request-response structure below to understand the interaction between the API gateway and the ads service.
HTTP method: The
GETmethod is used to fetch the advertisements from the ads service.Request format: The request format for the ads service is given below.
Response format: The response format includes a list of advertisements, as given below.
Failed requests
The common errors that the API may return in response to a request are listed in the following table.
Error Responses
Status Code | Phrase | Description |
| Bad Request | When a request is missing parameters or missing headers |
| Unauthorized | Provided access token is invalid |
| Forbidden | The server listens to the request but does not fulfill it |
| Not Found | Requested query is invalid |
| Too Many Requests | Too many requests are sent in a specific time period |
| Server Error | Indicates problem on the server side |
Summary
We familiarized ourselves with the API endpoints for different functionalities and understood the request and response structure of the messages exchanged between the client and server. The following table provides a summary of what we have learned so far.
Operations | Endpoints | Data Entities | Response (Status Code, Body) |
Search |
| Parameters are passed in the URL |
|
Sort |
| Parameters are passed in the URL |
|
Pagination |
| Parameters are passed in the URL |
|
Recommendation |
| Parameters are passed in the URL |
|
Advertisement |
| Parameters are passed in the URL |
|