Exposing an API Elements of an API

Learning objective: By the end of this lesson, students will be able to identify and explain the main components of an API – endpoints, requests, and responses.

Elements of an API

In this lesson, we’re going to explore the essential elements that make up an API. Understanding these components is critical in building and interacting with APIs effectively.

Building an API involves three key concepts:

Each of these elements plays a specific role in how an API functions, from defining access points to handling data exchange.

Endpoints

Endpoints are specific paths or routes defined in your API that determine how it interacts with the outside world. Each endpoint will map to a function in our application that will handle the request and send back a response. This could be as simple as sending back a string or as complex as sending back a JSON object.

When designing an API, it’s important to organize your endpoints in a clear and logical manner. One effective way to do this is by using a base URL. The base URL acts as a consistent starting point for all the endpoints in your API, and it’s typically named after the primary resource or service your API provides.

For example, if we create an API for a calculator, we could use the base URL of /calculator. This means all our endpoints will start with /calculator.

Best Practices:

Requests

A request is what a client sends to your API when it wants to interact with it.

Each request consists of:

As we build an API, we must determine what data we are expecting from the requester. This will help us decide what data to send back as a response.

During development, we will use a tool called Postman for interacting with our API. Postman allows us to both make requests and see the responses that are sent back.

Responses

A response is what your API sends back to the client after processing a request.

It includes:

Here is an example of a JSON object:

{
  "name": "Kira Nerys",
  "species": "Bajoran",
  "rank": "Colonel",
  "isOnDeepSpaceNine": true,
  "numberOfMissions": 173,
  "knownFor": ["Bajoran Militia Officer", "Resistance Fighter"],
  "relationships": {
    "closeFriend": "Odo",
    "mentor": null
  },
  "quote": "I'm a soldier, Major. I'm not used to relaxing."
}

JSON is an ideal format for APIs due to its readability and compatibility with various programming languages.