PHP MySQL API Documentation

A RESTful API built with PHP and MySQL

Introduction

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

Authentication is handled via JWT tokens. To authenticate, you need to:

  1. Register a new user or login with existing credentials
  2. Use the token received in the response
  3. Include the token in the Authorization header for protected routes

Example: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaXNzIjoiUEhQIE15U1FMIEFQSSIsImlhdCI6MTcwMDU2NTA2MCwiZXhwIjoxNzAwNTY4NjYwfQ.3jZ6CU8K9DMb1MK

POST /api/auth/register

Register a new user

Request Body:

Parameter Type Description
name Required String User's full name
email Required String User's email address
password Required String User's password

Response:

{
  "status": "success",
  "message": "Success",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "user": {
      "id": 1,
      "name": "John Doe",
      "email": "john@example.com"
    }
  }
}

POST /api/auth/login

Login with existing credentials

Request Body:

Parameter Type Description
email Required String User's email address
password Required String User's password

Response:

{
  "status": "success",
  "message": "Success",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "user": {
      "id": 1,
      "name": "John Doe",
      "email": "john@example.com"
    }
  }
}

GET /api/auth/me Protected

Get the authenticated user's information

Headers:

Parameter Description
Authorization Required Bearer {token}

Response:

{
  "status": "success",
  "message": "Success",
  "data": {
    "user": {
      "id": 1,
      "name": "John Doe",
      "email": "john@example.com",
      "created_at": "2023-11-21 15:30:45"
    }
  }
}

Products

The following endpoints allow you to manage products.

GET /api/products

Get a list of products with pagination

Query Parameters:

Parameter Type Description
page Integer Page number (default: 1)
limit Integer Number of items per page (default: 10)

Response:

{
  "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 /api/products/{id}

Get a single product by ID

Path Parameters:

Parameter Type Description
id Required Integer Product ID

Response:

{
  "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
    }
  }
}

POST /api/products Protected

Create a new product

Headers:

Parameter Description
Authorization Required Bearer {token}

Request Body:

Parameter Type Description
name Required String Product name
description String Product description
price Required Number Product price

Response:

{
  "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
    }
  }
}

PUT /api/products/{id} Protected

Update an existing product

Headers:

Parameter Description
Authorization Required Bearer {token}

Path Parameters:

Parameter Type Description
id Required Integer Product ID

Request Body:

Parameter Type Description
name String Product name
description String Product description
price Number Product price

Response:

{
  "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 /api/products/{id} Protected

Delete a product

Headers:

Parameter Description
Authorization Required Bearer {token}

Path Parameters:

Parameter Type Description
id Required Integer Product ID

Response:

{
  "status": "success",
  "message": "Product deleted successfully",
  "data": []
}

Error Handling

When an error occurs, the API will return a JSON response with a status of "error" and additional information:

Error Response Format:

{
  "status": "error",
  "message": "Error message description",
  "errors": []  // Additional error details if available
}

Common HTTP Status Codes:

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