Introduction to API Responses

API responses are similar to receiving a reply from a friend when you ask a question. This reply, known as an API Response, is packed with useful information: the data you asked for, a status code (a mini report on how the request went), headers (like additional information about the data), and more.

Most often, the data returned by APIs is in a format called JSON (JavaScript Object Notation), an easy to use, neatly organized data format.

When fetching data, JavaScript's Fetch API offers a Promise. Once resolved, it provides a Response object. An example API and its response can be seen as follows:

The fetch function takes an API URL, then returns a Promise. The Promise then resolves to present a Response object, which is logged to the console.

Status Codes: Good Guy vs Bad Guy

API requests can return varying outcomes, leading to success, a resource being not found, a server error, and so on. These statuses of an API request are denoted by HTTP status codes, like a mini report card for our API request.

HTTP status codes are grouped into five classes:

  • 1xx (Informational): The API received the request, and the process is continuing.
  • 2xx (Successful): The request was successfully received, understood, and accepted.
  • 3xx (Redirection): Extra action must be taken to complete the request
  • 4xx (Client Error): The request has bad syntax or cannot be fulfilled.
  • 5xx (Server Error): The server failed to complete a valid request.

We can fetch the status code of an API response like this:

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal