Lesson 22 of 158 – API Endpoints
22%

API Endpoints

An API endpoint is a specific URL through which a client application can access a particular resource or perform an API operation.

In a React Native application, endpoints are used to communicate with the backend server. For example, one endpoint can return students, another can create a student, and another can return a particular student.

Note: The API Base URL identifies the common API location, while an endpoint identifies a specific resource or operation.

1. What is an API Endpoint?

An API endpoint is a specific URL used to communicate with a particular resource of an API.

Example:


https://example.com/api/students

Here, students is the resource endpoint.

2. Base URL vs Endpoint

Suppose our API Base URL is:


https://example.com/api/

And the endpoint is:


students

The complete URL becomes:


https://example.com/api/students

3. Resource in an API Endpoint

An endpoint commonly represents a resource such as students, users, courses, products, or orders.


/students
/users
/courses
/products
/orders

Each resource can have different operations.

4. Students Endpoint

A students endpoint can be used to work with student records.


https://example.com/api/students

Depending on the HTTP method, this endpoint can be used to retrieve, create, update, or delete student records.

5. Users Endpoint

A users endpoint can be used to work with application users.


https://example.com/api/users

For example, a GET request can retrieve users.

6. Courses Endpoint

A course management application can provide a courses endpoint.


https://example.com/api/courses

The endpoint can return course information as JSON.

7. Endpoint and HTTP Method

The same endpoint can perform different operations depending on the HTTP method.

Method Purpose
GET Retrieve data
POST Create data
PUT Update data
PATCH Partially update data
DELETE Delete data

8. GET Endpoint

A GET request can retrieve resources from an endpoint.


GET /api/students

The server may return a JSON array of students.


[
    {
        "id": 1,
        "name": "Rahul"
    },
    {
        "id": 2,
        "name": "Amit"
    }
]

9. POST Endpoint

A POST request can send data to an endpoint to create a new resource.


POST /api/students

Example request body:


{
    "name": "Rahul",
    "course": "React Native"
}

10. PUT Endpoint

A PUT request can be used to update an existing resource.


PUT /api/students/10

Here, 10 identifies the student that is being updated.

11. PATCH Endpoint

PATCH can be used when only part of a resource needs to be updated.


PATCH /api/students/10

Example:


{
    "course": "TypeScript"
}

Only the specified information needs to be changed.

12. DELETE Endpoint

A DELETE request can be used to remove a resource.


DELETE /api/students/10

Here, the student with ID 10 is the resource being targeted.

13. Endpoint for a Single Resource

An endpoint can identify a single resource using an ID.


https://example.com/api/students/25

Here, 25 can represent a particular student.

14. Collection Endpoint

An endpoint without an ID commonly represents a collection of resources.


/api/students

For example, GET can return a collection of students.

15. Single Resource Endpoint

Adding an ID can identify a single resource.


/api/students/25

This endpoint can be used to retrieve or modify student 25, depending on the HTTP method.

16. Endpoint with Query Parameters

An endpoint can receive query parameters for filtering or searching.


https://example.com/api/students?course=react

Here, course=react is a query parameter.

17. Endpoint with Multiple Query Parameters

Multiple query parameters can be included in one endpoint URL.


https://example.com/api/students
?course=react&city=patna

The parameters can be used to filter the returned data.

18. Endpoint with Path Parameters

A path parameter can identify a specific resource.


/api/students/25

Here, 25 is part of the URL path and can represent a student ID.

19. Login Endpoint

Authentication systems commonly have a login endpoint.


POST /api/login

Example JSON request:


{
    "email": "rahul@example.com",
    "password": "123456"
}

The server can validate the credentials and return an authentication response.

20. Registration Endpoint

A registration endpoint can be used to create a new application user.


POST /api/register

Example:


{
    "name": "Rahul",
    "email": "rahul@example.com",
    "password": "123456"
}

21. Profile Endpoint

A profile endpoint can return information about the authenticated user.


GET /api/profile

An authentication token may be required to access this endpoint.

22. API Endpoint Naming

Clear endpoint names make an API easier to understand and maintain.

Examples:


/api/students
/api/users
/api/courses
/api/products
/api/orders

Resource names are commonly written as nouns.

23. RESTful Endpoint Examples

Operation Method Endpoint
Get all students GET /api/students
Get one student GET /api/students/10
Create student POST /api/students
Update student PUT /api/students/10
Delete student DELETE /api/students/10

24. Calling an Endpoint with Fetch

React Native can call an API endpoint using the Fetch API.


const response = await fetch(
    "https://example.com/api/students"
);

const data = await response.json();

console.log(data);

The complete URL points to the students endpoint.

25. Calling an Endpoint with Axios

Axios can also be used to call API endpoints.


import axios from "axios";

const response = await axios.get(
    "https://example.com/api/students"
);

console.log(response.data);

26. Endpoint Response

An endpoint normally returns an HTTP response containing a status code and possibly JSON data.


HTTP/1.1 200 OK
Content-Type: application/json

{
    "success": true,
    "students": [
        {
            "id": 1,
            "name": "Rahul"
        }
    ]
}

27. Endpoint and PHP REST API

A PHP REST API can have separate PHP files or routing logic for different endpoints.

For example:


/api/students.php
/api/users.php
/api/login.php

These can provide different API operations depending on the project structure.

28. Common Endpoint Mistakes

  • Using the wrong endpoint URL.
  • Using the wrong HTTP method.
  • Misspelling the endpoint name.
  • Forgetting a resource ID.
  • Using the wrong API version.
  • Sending incorrect query parameters.
  • Calling an endpoint that does not exist.
  • Using a development URL in the wrong environment.

29. Complete API Endpoint Flow


React Native App
       ↓
Base URL
       ↓
Endpoint
       ↓
HTTP Method
       ↓
API Server
       ↓
PHP REST API
       ↓
MySQL Database
       ↓
JSON Response
       ↓
React Native App

The endpoint tells the server which resource or API operation the mobile application wants to access.

30. Complete API Endpoint Concept

API endpoints are the specific communication points between a client application and a backend API.


Base URL:
https://example.com/api/

Endpoint:
students

Complete URL:
https://example.com/api/students

The same resource endpoint can support different operations through HTTP methods such as GET, POST, PUT, PATCH, and DELETE.

In the next lessons, we will learn how query parameters and path parameters are used to send additional information to API endpoints.

📌 Key Points

  • An API endpoint is a specific URL used to access an API resource.
  • The base URL provides the common API location.
  • An endpoint identifies a resource or operation.
  • Endpoints can represent collections such as students or users.
  • An ID can identify a single resource.
  • GET is commonly used to retrieve data.
  • POST is commonly used to create data.
  • PUT and PATCH are used for updating data.
  • DELETE is used to delete data.
  • Query parameters can provide filtering information.
  • Path parameters can identify specific resources.
  • React Native can call endpoints using Fetch or Axios.
  • PHP can provide REST API endpoints.
  • Endpoints normally return HTTP responses and often JSON data.

🧠 Quick Quiz

Question: What is the main purpose of an API endpoint?