API Reference

Complete API documentation for the TSTP system

Introduction

The TSTP API provides programmatic access to the TSTP platform, allowing you to integrate tutorials, analytics, and user management into your applications.

Base URL

https://system.tstp.xyz/api/

Content Type

All API requests and responses use application/json content type.

Authentication

The TSTP API uses session-based authentication. Include your session cookie in requests to access protected endpoints.

Example Request

curl -X GET "https://system.tstp.xyz/api/users" \
  -H "Cookie: PHPSESSID=your_session_id" \
  -H "Content-Type: application/json"

Rate Limiting

API requests are rate limited to prevent abuse and ensure fair usage:

  • Authenticated users: 1000 requests per hour
  • Unauthenticated users: 100 requests per hour

Error Handling

The API uses standard HTTP status codes and returns error details in JSON format:

Error Response Format

{
  "success": false,
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Invalid input data",
    "details": {
      "field": "email",
      "reason": "Invalid email format"
    }
  }
}

Authentication Endpoints

POST /api/auth/login

Authenticate a user and create a session.

Request Body

{
  "username": "user@example.com",
  "password": "password123"
}

Response

{
  "success": true,
  "data": {
    "user": {
      "id": "123",
      "username": "user@example.com",
      "role": "user"
    },
    "session_id": "abc123"
  }
}

POST /api/auth/logout

Log out the current user and destroy the session.

Response

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

User Endpoints

GET /api/users

Get a list of users (admin only).

Query Parameters

  • page - Page number (default: 1)
  • limit - Items per page (default: 25)
  • role - Filter by user role

Response

{
  "success": true,
  "data": {
    "users": [
      {
        "id": "123",
        "username": "user@example.com",
        "role": "user",
        "created_at": "2024-01-01T00:00:00Z"
      }
    ],
    "pagination": {
      "page": 1,
      "limit": 25,
      "total": 100
    }
  }
}

Tutorial Endpoints

GET /api/tutorials

Get a list of tutorials.

Query Parameters

  • category - Filter by category
  • status - Filter by status (published, draft)
  • search - Search in title and content

Response

{
  "success": true,
  "data": [
    {
      "id": "1",
      "title": "Getting Started with HTML",
      "description": "Learn the fundamentals of HTML",
      "category": "web-development",
      "status": "published",
      "created_at": "2024-01-01T00:00:00Z"
    }
  ]
}

POST /api/tutorials

Create a new tutorial (authenticated users only).

Request Body

{
  "title": "New Tutorial",
  "description": "Tutorial description",
  "content": "Tutorial content",
  "category": "web-development",
  "tags": ["html", "css"]
}

Analytics Endpoints

GET /api/analytics

Get analytics data (admin only).

Query Parameters

  • action - Analytics action (user_stats, tutorial_stats, system_stats)
  • period - Time period (day, week, month, year)

Response

{
  "success": true,
  "data": {
    "total_users": 1000,
    "active_users": 750,
    "total_tutorials": 50,
    "page_views": 10000
  }
}

Theme Endpoints

POST /api/switch

Switch the current user's theme.

Request Body

{
  "theme": "dark"
}

Response

{
  "success": true,
  "theme": "dark",
  "message": "Theme updated successfully"
}

Code Examples

JavaScript

// Fetch tutorials
fetch('/api/tutorials')
  .then(response => response.json())
  .then(data => {
    if (data.success) {
      console.log('Tutorials:', data.data);
    }
  })
  .catch(error => {
    console.error('Error:', error);
  });

PHP

// Create tutorial
$data = [
    'title' => 'New Tutorial',
    'description' => 'Tutorial description',
    'content' => 'Tutorial content'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://system.tstp.xyz/api/tutorials');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Cookie: PHPSESSID=' . $session_id
]);

$response = curl_exec($ch);
curl_close($ch);

API Changelog

Version 1.0.0 - 2024-01-01

  • Initial API release
  • Authentication endpoints
  • User management endpoints
  • Tutorial CRUD operations
  • Analytics endpoints
  • Theme switching