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 |
| Architecture | REST API |
| Framework | Laravel |
| Authentication | |
| Data Format | JSON |
| API Version | v1 |
| HTTP Protocol | HTTPS 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
Request Body
Parameter | Type | Required | Description |
name | string | Yes | User name, maximum 250 characters |
email | string | Yes | Email address in a valid format |
password | string | Yes | Password, 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 Created | The account was successfully created. |
| 400 Bad Request | The data fails validation. |
Login
Authenticate a user using their email and password.
Endpoint
Request Body
Parameter | Type | Required | Description |
email | string | Yes | Registered email address |
password | string | Yes | Account 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
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
Request Body
| Parameter | Type | Required | Description |
content | string | Yes | Post content, maximum 250 characters |
image_url | string | No | Image 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 Created | Post created successfully |
| 400 Bad Request | Validation failed |
Get Post by ID
Retrieve the details of a single post based on its ID.
Endpoint
Path Parameters
Parameter | Type | Description |
id | integer | Post ID |
Example Request
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
Path Parameters
Parameter | Type | Description |
id | integer | ID of the post to be updated |
Request Body
| Parameter | Type | Required | Description |
content | string | Yes | New post content, maximum 250 characters |
image_url | string | No | New 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 OK | Post successfully updated |
| 400 Bad Request | Validation failed |
| 404 Not Found | Post not found |
Delete Post
Delete a post based on its ID.
Endpoint
Path Parameters
Parameter | Type | Description |
id | integer | Post ID |
Example Request
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
Request Body
Parameter | Type | Required | Description |
post_id | integer | Yes | ID of the post to be commented on |
content | string | Yes | Comment 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 Created | Comment successfully created |
| 400 Bad Request | Validation 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 | integer | Comment ID |
Example Request
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
Request Body
Parameter | Type | Required | Description |
post_id | integer | Yes | ID of the post to be liked |
Example Request
Success Response — 201 Created
{ "success": true, "message": "Berhasil menambahkan like.", "data": { "id": 1, "user_id": 1, "post_id": 1 } } |
Possible Responses
| 201 Created | Like successfully created. |
| 400 Bad Request | Post ID does not meet the validation requirements. |
Delete Like
Delete a like based on its like ID.
Endpoint
Path Parameters
Parameter | Type | Required | Description |
id | integer | Yes | ID of the like to be deleted |
Example Request
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
Request Body
| Parameter | Type | Required | Description |
receiver_id | integer | Yes | ID of the receiving user |
message_content | string | Yes | Message 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 Created | Message successfully sent |
| 400 Bad Request | Validation failed. |
Get Message Detail
Retrieve message details based on its ID.
Endpoint
Path Parameters
Parameter | Type | Description |
id | integer | Message ID |
Example Request
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 | integer | ID of the message to be deleted |
Example Request
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 | integer | ID 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 OK | Request successful | Retrieving, updating, or deleting data |
| 201 Created | Resource created | Registering, creating a post, comment, like, or message |
| 400 Bad Request | Invalid request | Input validation failed |
| 401 Unauthorized | Authentication failed | Incorrect login credentials or JWT authentication rejected by the middleware |
| 404 Not Found | Resource not found | Resource not found on an endpoint that performs the corresponding check |
| 500 Internal Server Error | Server error | Unhandled 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 user | No |
POST | /api/v1/login | Login user | No |
GET | /api/v1/posts | Get all posts | JWT |
POST | /api/v1/posts | Create post | JWT |
GET | /api/v1/posts/{id} | Get post detail | JWT |
PUT | /api/v1/posts/{id} | Update post | JWT |
DELETE | /api/v1/posts/{id} | Delete post | JWT |
POST | /api/v1/comments | Create comment | JWT |
DELETE | /api/v1/comments/{id} | Delete comment | JWT |
POST | /api/v1/likes | Create like | JWT |
DELETE | /api/v1/likes/{id} | Delete like | JWT |
POST | /api/v1/messages | Send message | JWT |
GET | /api/v1/messages/{id} | Get message detail | JWT |
GET | /api/v1/messages/getMessages/{user_id} | Get received messages by user | JWT |
DELETE | /api/v1/messages/{id} | Delete message | JWT |