Projects

Social Media REST API

Social Media REST API

Overview

Social Media API is a Laravel-based REST API that provides user authentication, post management, comments, likes, and user-to-user messaging. It can be integrated into web applications, mobile applications, or frontends that require social media backend services.


Features

  • Authentication: User registration and login using JSON Web Tokens (JWT).
  • Posts: Retrieve all posts, create new posts, view post details, update posts, and delete posts.
  • Comments: Create and delete comments on posts.
  • Likes: Add and remove likes on posts.
  • Messages: Send messages, retrieve message details, delete messages, and retrieve messages by recipient user.


Technical Specifications

Property
Description
ArchitectureREST API
FrameworkLaravel
Authentication
JSON Web Token (JWT)
Data FormatJSON
API Versionv1
HTTP ProtocolHTTPS recommended


Base URL

All endpoints use the /api/v1 prefix.

http://localhost:8000/api/v1

For deployment, replace the URL above with your actual API server domain.

Example of a complete endpoint

GET
http://localhost:8000/api/v1/posts


Authentication

The API uses JWT to authenticate users. Users must register or log in to obtain an access token before accessing endpoints protected by JWT middleware.


Register

Register a new user account.


Endpoint
POST
/api/v1/register

Request Body
Parameter
Type
Required
Description
name
stringYesUser name, maximum 250 characters
email
stringYesEmail address in a valid format
password
stringYesPassword, minimum 8 characters

Example Request
{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "password123"
}

Success Response — 201 Created

{
  "user": {
    "name": "John Doe",
    "email": "john@example.com",
    "updated_at": "2026-10-21T10:00:00.000000Z",
    "created_at": "2026-10-21T10:00:00.000000Z",
    "id": 1
  },
  "token": "YOUR_JWT_TOKEN"
}

The user data and timestamp format in the example above are illustrative. The returned fields depend on the User model configuration and Laravel's serialization.


Possible Response
201 CreatedThe account was successfully created.
400 Bad RequestThe data fails validation.


Login

Authenticate a user using their email and password.


Endpoint
POST
/api/v1/login

Request Body
Parameter
Type
Required
Description
email
stringYesRegistered email address
password
stringYesAccount password

Example Request
{
  "email": "john@example.com",
  "password": "password123"
}


Success Response — 200 OK
{
  "token": "YOUR_JWT_TOKEN"
}


Error Response — 401 Unauthorized
{
  "error": "Invalid credentials"
}


Using the JWT Token

Include the token in the header of every request to protected endpoints.

Authorization: Bearer YOUR_JWT_TOKEN
Accept: application/json
Content-Type: application/json


Replace YOUR_JWT_TOKEN with the token obtained from the register or login endpoint.

The token must be sent in the Authorization header, not as a URL parameter.


Posts

All Posts endpoints require a valid JWT.


Get All Posts

Retrieve all posts along with user, comment, and like relationships.


Endpoint
GET
/api/v1/posts


Headers

Authorization: Bearer YOUR_JWT_TOKEN
Accept: application/json


Success Response — 200 OK

{
  "success": true,
  "data": [
    {
      "id": 1,
      "user_id": 1,
      "content": "Hello, this is my first post!",
      "image_url": "https://example.com/post.jpg",
      "user": {
        "id": 1,
        "name": "John Doe"
      },
      "comments": [],
      "likes": []
    }
  ]
}

The example above illustrates the conceptual data structure. The actual fields and relationship structures depend on each model and its serialization configuration.

Response: 200 OK


Create New Post

Create a new post for the currently authenticated user.


Endpoint
POST
/api/v1/posts


Request Body

ParameterTypeRequiredDescription
content
stringYesPost content, maximum 250 characters
image_url
stringNoImage URL


Example Request

{
  "content": "Hello, this is my first post!",
  "image_url": "https://example.com/post.jpg"
}


Success Response — 201 Created

{
  "success": true,
  "messages": "Berhasil membuat post baru.",
  "data": {
    "id": 1,
    "user_id": 1,
    "content": "Hello, this is my first post!",
    "image_url": "https://example.com/post.jpg"
  }
}

The user_id is determined by the authenticated user and does not need to be included in the request body.


Possible Responses
201 CreatedPost created successfully
400 Bad RequestValidation failed


Get Post by ID

Retrieve the details of a single post based on its ID.


Endpoint
GET
/api/v1/posts/{id}


Path Parameters

Parameter
Type
Description
id
integerPost ID


Example Request

GET
/api/v1/posts/1


Success Response — 200 OK

{
  "success": true,
  "data": {
    "id": 1,
    "user_id": 1,
    "content": "Hello, this is my first post!",
    "image_url": "https://example.com/post.jpg"
  }
}


Error Response — 404 Not Found

{
  "success": false,
  "messages": "Post tidak ditemukan"
}


This endpoint returns the post directly, without eager loading relationships as in the Get All Posts endpoint.


Update Post

Update the post content and image based on its ID.


Endpoint
PUT
/api/v1/posts/{id}


Path Parameters

Parameter
Type
Description
id
integerID of the post to be updated


Request Body

ParameterTypeRequiredDescription
content
stringYesNew post content, maximum 250 characters
image_url
stringNoNew image URL; may be null


Example Request

{
  "content": "Updated post content",
  "image_url": "https://example.com/new-post.jpg"
}


Success Response — 200 OK

{
  "success": true,
  "message": "Berhasil memperbarui post.",
  "data": {
    "id": 1,
    "user_id": 1,
    "content": "Updated post content",
    "image_url": "https://example.com/new-post.jpg"
  }
}


Possible Responses

200 OKPost successfully updated
400 Bad RequestValidation failed
404 Not FoundPost not found


Delete Post

Delete a post based on its ID.


Endpoint
DELETE
/api/v1/posts/{id}

Path Parameters

Parameter
Type
Description
id
integerPost ID


Example Request

DELETE
/api/v1/posts/1


Success Response — 200 OK

{
  "success": true,
  "messages": "Berhasil menghapus post."
}


Error Response — 404 Not Found

{
  "success": false,
  "messages": "Post tidak ditemukan"
}


Comments

All Comments endpoints require a valid JWT.


Create New Comment

Add a comment to an available post.


Endpoint
POST
/api/v1/comments

Request Body
Parameter
Type
Required
Description
post_id
integerYesID of the post to be commented on
content
stringYesComment content, maximum 250 characters

Example Request
{
  "post_id": 1,
  "content": "Great post!"
}

Success Response — 201 Created
{
  "success": true,
  "message": "Berhasil menambahkan comment.",
  "data": {
    "id": 1,
    "user_id": 1,
    "post_id": 1,
    "content": "Great post!"
  }
}

Possible Responses
201 CreatedComment successfully created
400 Bad RequestValidation failed, including when the post does not exist or has already been deleted.

The user_id is determined from the JWT of the currently logged-in user.


Delete Comment

Delete a comment based on its ID.


Endpoint
DELETE
/api/v1/comments/{id}

Path Parameters
Parameter
Type
Description
id
integerComment ID

Example Request
DELETE
/api/v1/comments/1

Success Response — 200 OK
{
  "success": true,
  "messages": "Berhasil menghapus comment."
}

Error Response — 404 Not Found
{
  "success": false,
  "messages": "Comment tidak ditemukan"
}


Likes

All Likes endpoints require a valid JWT.


Create New Like

Add a like to a post.


Endpoint
POST
/api/v1/likes


Request Body

Parameter
Type
Required
Description
post_id
integerYesID of the post to be liked


Example Request

{
  "post_id": 1
}


Success Response — 201 Created

{
  "success": true,
  "message": "Berhasil menambahkan like.",
  "data": {
    "id": 1,
    "user_id": 1,
    "post_id": 1
  }
}


Possible Responses

201 CreatedLike successfully created.
400 Bad RequestPost ID does not meet the validation requirements.


Delete Like

Delete a like based on its like ID.


Endpoint
DELETE
/api/v1/likes/{id}


Path Parameters

Parameter
Type
Required
Description
id
integerYesID of the like to be deleted

Example Request
DELETE
/api/v1/likes/1


Response — 200 OK

{
  "success": true,
  "messages": "Berhasil menghapus like."
}

The current implementation uses Like::destroy($id) without checking the deletion result. Therefore, a success response may also be returned when the specified ID is not found.


Messages

All Messages endpoints require a valid JWT.


Send a Message

Send a message to another user.


Endpoint
POST
/api/v1/messages


Request Body

ParameterTypeRequiredDescription
receiver_id
integerYesID of the receiving user
message_content
stringYesMessage content, maximum 250 character

Example Request
{
  "receiver_id": 2,
  "message_content": "Hello, how are you?"
}

Success Response — 201 Created
{
  "success": true,
  "message": "Pesan berhasil dikirim",
  "data": {
    "id": 1,
    "sender_id": 1,
    "receiver_id": 2,
    "message_content": "Hello, how are you?"
  }
}


The sender_id is obtained from the authenticated user. The receiver_id must refer to an existing user.


Possible Responses
201 CreatedMessage successfully sent
400 Bad RequestValidation failed.


Get Message Detail

Retrieve message details based on its ID.


Endpoint
GET
/api/v1/messages/{id}

Path Parameters
Parameter
Type
Description
id
integerMessage ID

Example Request
GET
/api/v1/messages/1

Success Response — 200 OK
{
  "success": true,
  "message": "Pesan berhasil diambil",
  "data": {
    "id": 1,
    "sender_id": 1,
    "receiver_id": 2,
    "message_content": "Hello, how are you?"
  }
}

Error Response — 404 Not Found
{
  "success": false,
  "messages": "Pesan tidak ditemukan"
}


Delete a Message

Delete a message based on its ID.


Endpoint
DELETE
/api/v1/messages/{id}

Path Parameters
Parameter
Type
Description
id
integerID of the message to be deleted

Example Request
DELETE
/api/v1/messages/1

Response — 200 OK
{
  "success": true,
  "message": "Pesan berhasil dihapus"
}


The current implementation uses Message::destroy($id) without checking whether the message actually exists.


Get Messages by User

Retrieve messages addressed to a specific user based on receiver_id.


Endpoint
GET
/api/v1/messages/getMessages/{user_id}

Path Parameters
Parameter
Type
Description
user_id
integerID of the user receiving the messages

Example Request
GET
/api/v1/messages/getMessages/2

Success Response — 200 OK
{
  "success": true,
  "message": "Berhasil mengambil semua pesan berdasarkan id user",
  "data": [
    {
      "id": 1,
      "sender_id": 1,
      "receiver_id": 2,
      "message_content": "Hello, how are you?"
    },
    {
      "id": 2,
      "sender_id": 3,
      "receiver_id": 2,
      "message_content": "Are you available today?"
    }
  ]
}


The data response is an array. If there are no messages for the specified recipient, the query may return an empty array.

This endpoint only retrieves messages with the corresponding receiver_id, not the entire two-way conversation.


HTTP Status Codes

Status
Meaning
Usage
200 OKRequest successfulRetrieving, updating, or deleting data
201 CreatedResource createdRegistering, creating a post, comment, like, or message
400 Bad RequestInvalid requestInput validation failed
401 UnauthorizedAuthentication failedIncorrect login credentials or JWT authentication rejected by the middleware
404 Not FoundResource not foundResource not found on an endpoint that performs the corresponding check
500 Internal Server ErrorServer errorUnhandled internal error

The 500 status code is a standard possible server response and is not a response explicitly defined in the controllers described above.


General Request Guidelines


Headers

For requests with a JSON body:

Accept: application/json
Content-Type: application/json
Authorization: Bearer YOUR_JWT_TOKEN

The Authorization header is required for endpoints protected by JWT.


Request Format

  • Use the appropriate HTTP method for each endpoint.
  • Send the request body in JSON format for create and update operations.
  • Use the correct resource ID in the path parameter.
  • Do not send passwords or JWT tokens through the URL.


Validation

The validation rules currently defined in the controllers are:

Field
Validation
name
Required, string, max 250
email
Required, string, valid email, max 250
password
Required, string, min 8
content (post)
Required, string, max 250
content (comment)
Required, string, max 250
post_id
Required, numeric, post must exist and not be deleted
receiver_id
Required, numeric, user must exist
message_content
Required, string, max 250
image_url
Nullable; does not yet have URL or string type validation rules


Security Considerations

  • Store JWTs securely and avoid exposing tokens in logs or URLs.
  • Use HTTPS in production environments.
  • Implement ownership-based authorization before allowing users to update or delete resources.
  • Restrict access to messages so that only authorized parties can read them.
  • Consider applying a unique validation rule to email addresses to prevent repeated registration using the same address.
  • Consider adding a unique constraint for the combination of user_id and post_id in likes if each user should only be able to like a post once.
  • Add pagination to the Get All Posts and Get Messages by User endpoints as the amount of data increases.


API Endpoint Summary

Method
Endpoint
Description
Authentication
POST
/api/v1/register
Register userNo
POST
/api/v1/login
Login userNo
GET
/api/v1/posts
Get all postsJWT
POST
/api/v1/posts
Create postJWT
GET
/api/v1/posts/{id}
Get post detailJWT
PUT
/api/v1/posts/{id}
Update postJWT
DELETE
/api/v1/posts/{id}
Delete postJWT
POST
/api/v1/comments
Create commentJWT
DELETE
/api/v1/comments/{id}
Delete commentJWT
POST
/api/v1/likes
Create likeJWT
DELETE
/api/v1/likes/{id}
Delete likeJWT
POST
/api/v1/messages
Send messageJWT
GET
/api/v1/messages/{id}
Get message detailJWT
GET
/api/v1/messages/getMessages/{user_id}
Get received messages by userJWT
DELETE
/api/v1/messages/{id}
Delete messageJWT