Client-Adapting APIs

Learn how to identify the client type and make the API response flexible.

Introduction

In the real world, an API endpoint gets called by numerous types of end devices (such as mobile phones, laptops, tablets, and so on). Depending on the type of end device or situation, API responses can be flexible. For example:

  • A YouTube video's resolution provided by the server is different for a mobile device compared to a desktop browser. This can be due to the width of the screen, transfer protocol, and so on.

  • Instagram stories don’t have music for some regions. After identifying the region, the system may decide to add or remove music for users.

Therefore, an API generating the same response for every client request may not be suitable for all end devices. In essence, a flexible API response refers to the flexibility in terms of the response generated and resources provided by servers to clients—mainly depending on the user and client (such as its type, version, and so on).

To generate a flexible response, we need to provide flexibility on the client- or server-side. We’ll discuss different ways to make the response flexible using various examples in this lesson.

Approaches

Adaptive responses can be achieved in the following ways:

Press + to interact
Provide flexibility at multiple places to generate a flexible response
Provide flexibility at multiple places to generate a flexible response

Client-side flexibility

Client-side flexibility makes the responses flexible on the client-side before displaying the results on the screen. We get the same response for all user scenarios or different client types, and the client tailors this response according to the requirements. All the clients receive the same number of fixed fields as a response from the server.

For example, Educative’s Careers page uses four images for small devices and eight images for large devices, as depicted in the following illustrations:

 Four images for small devices
Four images for small devices
 Eight images for large devices
Eight images for large devices

Although the response from the server remains the same (eight images are received for both device types), the frontend ...