A RESTful API built with PHP and MySQL
This is a RESTful API built with PHP and MySQL. It provides endpoints for user authentication and product management.
The API uses JWT (JSON Web Tokens) for authentication and follows RESTful conventions for endpoint design.
All responses are in JSON format and include a status field indicating success or error.
Authentication is handled via JWT tokens. To authenticate, you need to:
Example: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaXNzIjoiUEhQIE15U1FMIEFQSSIsImlhdCI6MTcwMDU2NTA2MCwiZXhwIjoxNzAwNTY4NjYwfQ.3jZ6CU8K9DMb1MK
Register a new user
| Parameter | Type | Description |
|---|---|---|
| name Required | String | User's full name |
| email Required | String | User's email address |
| password Required | String | User's password |
{
"status": "success",
"message": "Success",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
}
}
Login with existing credentials
| Parameter | Type | Description |
|---|---|---|
| email Required | String | User's email address |
| password Required | String | User's password |
{
"status": "success",
"message": "Success",
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}
}
}
Get the authenticated user's information
| Parameter | Description |
|---|---|
| Authorization Required | Bearer {token} |
{
"status": "success",
"message": "Success",
"data": {
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"created_at": "2023-11-21 15:30:45"
}
}
}
The following endpoints allow you to manage products.
Get a list of products with pagination
| Parameter | Type | Description |
|---|---|---|
| page | Integer | Page number (default: 1) |
| limit | Integer | Number of items per page (default: 10) |
{
"status": "success",
"message": "Success",
"data": {
"products": [
{
"id": 1,
"user_id": 1,
"name": "Smartphone",
"description": "Latest model with great camera",
"price": "599.99",
"created_at": "2023-11-21 16:30:00",
"updated_at": null
},
{
"id": 2,
"user_id": 1,
"name": "Laptop",
"description": "Powerful laptop for developers",
"price": "1299.99",
"created_at": "2023-11-21 16:35:00",
"updated_at": null
}
],
"pagination": {
"total": 2,
"page": 1,
"limit": 10,
"pages": 1
}
}
}
Get a single product by ID
| Parameter | Type | Description |
|---|---|---|
| id Required | Integer | Product ID |
{
"status": "success",
"message": "Success",
"data": {
"product": {
"id": 1,
"user_id": 1,
"name": "Smartphone",
"description": "Latest model with great camera",
"price": "599.99",
"created_at": "2023-11-21 16:30:00",
"updated_at": null
}
}
}
Create a new product
| Parameter | Description |
|---|---|
| Authorization Required | Bearer {token} |
| Parameter | Type | Description |
|---|---|---|
| name Required | String | Product name |
| description | String | Product description |
| price Required | Number | Product price |
{
"status": "success",
"message": "Product created successfully",
"data": {
"product": {
"id": 3,
"user_id": 1,
"name": "Headphones",
"description": "Noise cancelling headphones",
"price": "199.99",
"created_at": "2023-11-21 17:00:00",
"updated_at": null
}
}
}
Update an existing product
| Parameter | Description |
|---|---|
| Authorization Required | Bearer {token} |
| Parameter | Type | Description |
|---|---|---|
| id Required | Integer | Product ID |
| Parameter | Type | Description |
|---|---|---|
| name | String | Product name |
| description | String | Product description |
| price | Number | Product price |
{
"status": "success",
"message": "Success",
"data": {
"product": {
"id": 1,
"user_id": 1,
"name": "Smartphone Pro",
"description": "Latest model with great camera and improved battery",
"price": "699.99",
"created_at": "2023-11-21 16:30:00",
"updated_at": "2023-11-21 17:30:00"
}
}
}
Delete a product
| Parameter | Description |
|---|---|
| Authorization Required | Bearer {token} |
| Parameter | Type | Description |
|---|---|---|
| id Required | Integer | Product ID |
{
"status": "success",
"message": "Product deleted successfully",
"data": []
}
When an error occurs, the API will return a JSON response with a status of "error" and additional information:
{
"status": "error",
"message": "Error message description",
"errors": [] // Additional error details if available
}
| Status Code | Description |
|---|---|
| 200 | Success |
| 201 | Resource created successfully |
| 400 | Bad request (invalid input) |
| 401 | Unauthorized (missing or invalid authentication) |
| 403 | Forbidden (insufficient permissions) |
| 404 | Resource not found |
| 422 | Validation error |
| 500 | Server error |