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.
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.
Suppose our API Base URL is:
https://example.com/api/
And the endpoint is:
students
The complete URL becomes:
https://example.com/api/students
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.
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.
A users endpoint can be used to work with application users.
https://example.com/api/users
For example, a GET request can retrieve users.
A course management application can provide a courses endpoint.
https://example.com/api/courses
The endpoint can return course information as JSON.
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 |
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"
}
]
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"
}
A PUT request can be used to update an existing resource.
PUT /api/students/10
Here, 10 identifies the student that is being updated.
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.
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.
An endpoint can identify a single resource using an ID.
https://example.com/api/students/25
Here, 25 can represent a particular student.
An endpoint without an ID commonly represents a collection of resources.
/api/students
For example, GET can return a collection of students.
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.
An endpoint can receive query parameters for filtering or searching.
https://example.com/api/students?course=react
Here, course=react is a query parameter.
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.
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.
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.
A registration endpoint can be used to create a new application user.
POST /api/register
Example:
{
"name": "Rahul",
"email": "rahul@example.com",
"password": "123456"
}
A profile endpoint can return information about the authenticated user.
GET /api/profile
An authentication token may be required to access this endpoint.
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.
| 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 |
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.
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);
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"
}
]
}
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.
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.
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.
Question: What is the main purpose of an API endpoint?