Path parameters are values included directly inside the URL path of an API endpoint. They are commonly used to identify a specific resource such as a particular student, user, product, or course.
For example, in https://example.com/api/students/25, the number 25 can represent the ID of a specific student.
A path parameter is a dynamic value placed inside an API URL path to identify a particular resource.
/api/students/25
Here, 25 can be the ID of the student.
A common path parameter structure is:
/resource/{id}
For example:
/students/25
Here, 25 is the actual value of the ID parameter.
Suppose the API contains a students resource.
https://example.com/api/students/10
The value 10 identifies a particular student.
| Path Parameter | Query Parameter |
|---|---|
| /students/25 | /students?id=25 |
| Part of URL path | Added after ? |
| Often identifies a resource | Often filters or modifies a request |
A student ID is a common example of a path parameter.
GET /api/students/25
The API can use 25 to find the student whose ID is 25.
User APIs can use a user ID in the URL.
GET /api/users/15
The value 15 can identify one user.
Product APIs can use product IDs in their URLs.
GET /api/products/100
Here, 100 identifies a particular product.
A course management API can identify a course using its ID.
GET /api/courses/5
The API can return information about course 5.
GET is commonly used with path parameters to retrieve one resource.
GET /api/students/25
The server can return the student identified by ID 25.
A PUT request can use a path parameter to identify the resource that should be updated.
PUT /api/students/25
The request body can contain the new student information.
{
"name": "Rahul Kumar",
"course": "TypeScript"
}
PATCH can use a path parameter when only part of a resource needs to be updated.
PATCH /api/students/25
Example request body:
{
"course": "React Native"
}
DELETE commonly uses a path parameter to identify the resource that should be deleted.
DELETE /api/students/25
Here, the API can identify student 25 and delete that record.
An API can have more than one path parameter when its resource structure requires it.
/api/students/25/courses/10
Here:
Path parameters can be useful for nested resources.
/api/students/25/assignments
This can represent assignments belonging to student 25.
React Native can dynamically add an ID to a Fetch URL.
const studentId = 25;
const response = await fetch(
`https://example.com/api/students/${studentId}`
);
const data = await response.json();
console.log(data);
Axios can also use a dynamic ID in the endpoint.
const studentId = 25;
const response = await axios.get(
`https://example.com/api/students/${studentId}`
);
console.log(response.data);
The exact method for reading a path parameter in PHP depends on how the API routing is implemented.
For a simple PHP API, the URL path can be processed to extract the ID.
$url = $_SERVER['REQUEST_URI'];
$parts = explode('/', trim($url, '/'));
$id = end($parts);
For larger applications, a dedicated routing structure is usually more appropriate.
A path parameter should be validated before it is used by the API.
$id = filter_var(
$id,
FILTER_VALIDATE_INT
);
if ($id === false) {
// Invalid ID
}
This helps ensure that the API receives the expected type of value.
After validating an ID, a PHP REST API can use it in a prepared SQL statement.
$sql = "SELECT * FROM students
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'id' => $id
]);
$student = $stmt->fetch();
Prepared statements help prevent SQL injection.
A requested resource may not exist.
GET /api/students/9999
If student 9999 does not exist, the API can return an appropriate error response, commonly using HTTP status code 404 Not Found.
{
"success": false,
"message": "Student not found"
}
Clear resource-oriented URLs make APIs easier to understand.
/api/students
/api/students/25
/api/courses
/api/courses/10
The first URL represents a collection, while the second identifies a specific student.
Path parameter:
/api/students/25
This can identify student 25.
Query parameter:
/api/students?course=react
This can filter students by course.
Path parameters and search parameters have different purposes.
/api/students/25
This identifies a specific resource.
/api/students?search=rahul
This can search a collection for matching records.
Postman can be used to test API endpoints containing path parameters.
Example:
GET https://example.com/api/students/25
A student list can provide an ID when the user selects a student.
const openStudent = async (studentId) => {
const response = await fetch(
`https://example.com/api/students/${studentId}`
);
const data = await response.json();
console.log(data);
};
The selected ID becomes part of the API URL.
A protected API can use a path parameter together with an authentication token.
GET /api/students/25
Authorization: Bearer YOUR_TOKEN
The path parameter identifies the resource while the authentication information controls access.
React Native
↓
Select Student
↓
Student ID
↓
/api/students/25
↓
GET Request
↓
PHP REST API
↓
Validate ID
↓
MySQL Query
↓
JSON Response
↓
React Native
The path parameter carries the resource identifier from the client to the API.
Suppose a React Native application wants to display student number 25.
const studentId = 25;
const url =
`https://example.com/api/students/${studentId}`;
const response = await fetch(url);
const student = await response.json();
console.log(student);
The server receives the request for the resource identified by 25.
Path parameters are an important part of REST API design. They allow an API to identify specific resources directly from the URL path.
Base URL:
https://example.com/api/
Endpoint:
students
Path Parameter:
25
Complete URL:
https://example.com/api/students/25
In a React Native application, the path parameter can be generated dynamically from the selected item's ID and sent to the PHP REST API. The backend can validate the ID, query MySQL, and return the requested resource as JSON.
Question: In the URL https://example.com/api/students/25, what does 25 typically represent?