Files

4.3 KiB

newzy - API Documentation

This document describes the API endpoints for the newzy news web application.

Base URL

  • Development: http://localhost:3000/api
  • Production: <your-domain>/api

Authentication

Most endpoints require authentication via Bearer token. Include in headers:

Authorization: Bearer <your-jwt-token>
Content-Type: application/json

Common Response Formats

Success Response

{
  "success": true,
  "data": {},
  "message": "Operation completed successfully"
}

Error Response

{
  "success": false,
  "error": {
    "code": "ERROR_CODE",
    "message": "Human readable error message"
  }
}

Authentication Endpoints

POST /auth/register

Register a new user.

Request Body:

{
  "email": "user@example.com",
  "password": "securePassword123",
  "name": "John Doe"
}

Response:

{
  "success": true,
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "user": {
      "id": 1,
      "email": "user@example.com",
      "name": "John Doe"
    }
  },
  "message": "User registered successfully"
}

POST /auth/login

Login and get authentication token.

Request Body:

{
  "email": "user@example.com",
  "password": "securePassword123"
}

Response:

{
  "success": true,
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "user": {
      "id": 1,
      "email": "user@example.com",
      "name": "John Doe"
    }
  },
  "message": "Login successful"
}

POST /auth/logout

Logout and invalidate token.

Request: No body required

Response:

{
  "success": true,
  "message": "Logged out successfully"
}

Articles Endpoints

GET /articles

List all articles with optional filtering.

Query Parameters:

  • category: Filter by category (optional)
  • page: Page number for pagination (default: 1)
  • limit: Items per page (default: 10, max: 50)
  • search: Search keyword (optional)

Example:

GET /articles?category=technology&page=1&limit=20&search=ai

Response:

{
  "success": true,
  "data": {
    "articles": [
      {
        "id": 1,
        "title": "AI in Modern Technology",
        "content": "...",
        "author": "Jane Smith",
        "category": "technology",
        "publishedAt": "2024-01-15T10:30:00Z",
        "views": 1523
      }
    ],
    "pagination": {
      "currentPage": 1,
      "totalPages": 5,
      "totalItems": 100
    }
  }
}

GET /articles/:id

Get a single article by ID.

Response:

{
  "success": true,
  "data": {
    "article": {
      "id": 1,
      "title": "AI in Modern Technology",
      "content": "...",
      "author": "Jane Smith",
      "category": "technology",
      "publishedAt": "2024-01-15T10:30:00Z",
      "views": 1523,
      "imageUrl": "https://example.com/image.jpg"
    }
  }
}

POST /articles

Create a new article (authenticated only).

Request Body:

{
  "title": "New Article Title",
  "content": "Article content here...",
  "category": "technology",
  "author": "Author Name",
  "imageUrl": "https://example.com/image.jpg"
}

PUT /articles/:id

Update an existing article (authenticated only).

Request Body:

{
  "title": "Updated Title",
  "content": "Updated content...",
  "category": "technology"
}

DELETE /articles/:id

Delete an article (authenticated with admin role only).

Categories Endpoints

GET /categories

List all categories.

Response:

{
  "success": true,
  "data": {
    "categories": [
      { "id": 1, "name": "Technology", "slug": "technology" },
      { "id": 2, "name": "Sports", "slug": "sports" }
    ]
  }
}

GET /categories/:id

Get a single category with its articles.

Error Codes

Code Description HTTP Status
INVALID_CREDENTIALS Invalid email or password 401
TOKEN_EXPIRED JWT token has expired 401
UNAUTHORIZED Missing or invalid token 401
NOT_FOUND Resource not found 404
INTERNAL_ERROR Server-side error 500

Rate Limiting

API endpoints are rate limited to prevent abuse:

  • 100 requests per minute per IP address
  • Headers include remaining quota:
    • X-RateLimit-Limit: 100
    • X-RateLimit-Remaining: 95
    • X-RateLimit-Reset: <timestamp>