This guide provides examples for testing all API endpoints using various tools.
-
Install Dependencies
npm install
-
Set up PostgreSQL Database
CREATE DATABASE social_media_db;
-
Configure Environment Variables
- Copy
.env.exampleto.env - Update database credentials
- Copy
-
Start the Server
npm run dev
You can use any of the following:
- cURL (command line)
- Postman (GUI)
- Thunder Client (VS Code extension)
- REST Client (VS Code extension)
cURL:
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d "{
\"username\": \"johndoe\",
\"email\": \"john@example.com\",
\"password\": \"SecurePass123!\",
\"fullName\": \"John Doe\",
\"bio\": \"Software developer\"
}"Response:
{
"success": true,
"message": "User registered successfully",
"data": {
"user": {
"id": 1,
"username": "johndoe",
"email": "john@example.com",
"fullName": "John Doe",
"bio": "Software developer"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}cURL:
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d "{
\"email\": \"john@example.com\",
\"password\": \"SecurePass123!\"
}"Response:
{
"success": true,
"message": "Login successful",
"data": {
"user": {
"id": 1,
"username": "johndoe",
"email": "john@example.com",
"fullName": "John Doe"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
}cURL:
curl -X GET http://localhost:3000/api/auth/me \
-H "Authorization: Bearer YOUR_TOKEN_HERE"cURL:
curl -X GET "http://localhost:3000/api/posts?page=1&limit=10"cURL:
curl -X GET http://localhost:3000/api/posts/1cURL:
curl -X POST http://localhost:3000/api/posts \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d "{
\"title\": \"My First Post\",
\"content\": \"This is the content of my first post. It's great!\"
}"cURL:
curl -X PUT http://localhost:3000/api/posts/1 \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d "{
\"title\": \"Updated Title\",
\"content\": \"Updated content\"
}"cURL:
curl -X DELETE http://localhost:3000/api/posts/1 \
-H "Authorization: Bearer YOUR_TOKEN_HERE"cURL:
curl -X GET "http://localhost:3000/api/posts/user/1?page=1&limit=10"cURL:
curl -X GET "http://localhost:3000/api/comments/post/1?page=1&limit=20"cURL:
curl -X POST http://localhost:3000/api/comments/post/1 \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d "{
\"content\": \"Great post! Really enjoyed reading it.\"
}"cURL:
curl -X PUT http://localhost:3000/api/comments/1 \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d "{
\"content\": \"Updated comment text\"
}"cURL:
curl -X DELETE http://localhost:3000/api/comments/1 \
-H "Authorization: Bearer YOUR_TOKEN_HERE"cURL:
curl -X POST http://localhost:3000/api/likes/post/1 \
-H "Authorization: Bearer YOUR_TOKEN_HERE"cURL:
curl -X DELETE http://localhost:3000/api/likes/post/1 \
-H "Authorization: Bearer YOUR_TOKEN_HERE"cURL:
curl -X GET "http://localhost:3000/api/likes/post/1?page=1&limit=20"cURL:
curl -X GET "http://localhost:3000/api/likes/user/1?page=1&limit=10"-
Create a new collection
-
Add environment variables:
base_url:http://localhost:3000token: (will be set after login)
-
For authenticated requests, add this to Headers:
Authorization: Bearer {{token}}
{
"info": {
"name": "Social Media API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Auth",
"item": [
{
"name": "Register",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"username\": \"johndoe\",\n \"email\": \"john@example.com\",\n \"password\": \"SecurePass123!\",\n \"fullName\": \"John Doe\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{base_url}}/api/auth/register",
"host": ["{{base_url}}"],
"path": ["api", "auth", "register"]
}
}
}
]
}
]
}# 1. Register a user
TOKEN=$(curl -s -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","email":"test@example.com","password":"Test123!","fullName":"Test User"}' \
| jq -r '.data.token')
echo "Token: $TOKEN"
# 2. Create a post
POST_ID=$(curl -s -X POST http://localhost:3000/api/posts \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"title":"Test Post","content":"This is a test post content"}' \
| jq -r '.data.post.id')
echo "Post ID: $POST_ID"
# 3. Add a comment
curl -X POST http://localhost:3000/api/comments/post/$POST_ID \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"content":"Great post!"}'
# 4. Like the post
curl -X POST http://localhost:3000/api/likes/post/$POST_ID \
-H "Authorization: Bearer $TOKEN"
# 5. Get the post with all data
curl -X GET http://localhost:3000/api/posts/$POST_ID{
"success": false,
"message": "Validation failed",
"errors": [
{
"field": "email",
"message": "Must be a valid email address",
"value": "invalid-email"
}
]
}{
"success": false,
"message": "Access denied. No token provided."
}{
"success": false,
"message": "You are not authorized to update this post"
}{
"success": false,
"message": "Post not found"
}The API implements rate limiting:
- Window: 15 minutes
- Max Requests: 100 per IP
If you exceed the limit:
{
"success": false,
"message": "Too many requests from this IP, please try again later."
}-
Save your token after login/register for authenticated requests
-
Use environment variables in Postman for easier testing
-
Check response status codes:
- 200: Success
- 201: Created
- 400: Bad Request
- 401: Unauthorized
- 403: Forbidden
- 404: Not Found
- 500: Server Error
-
Pagination: Always use page and limit parameters for large datasets
-
Token expiry: Tokens expire after 7 days (configurable in .env)