ORICA GAME API Documentation

API Documentation

Dokumentasi lengkap untuk RESTful API ORICA GAME. Gunakan API ini untuk mengintegrasikan aplikasi game Anda.

Base URL

http://localhost:8000/api

Autentikasi

API ini menggunakan Laravel Sanctum untuk autentikasi. Setelah login atau register, Anda akan mendapatkan token akses yang harus disertakan di header setiap request yang memerlukan autentikasi.

Request Headers:

Accept: application/json
Content-Type: application/json
Authorization: Bearer {token}

Catatan:

  • Accept: application/json - Wajib disertakan di semua request
  • Content-Type: application/json - Wajib untuk request dengan body (POST, PUT, PATCH)
  • Authorization: Bearer {token} - Wajib untuk endpoint yang memerlukan autentikasi
  • Token berlaku selama 24 jam setelah dibuat

Rate Limiting:

  • Semua endpoint memiliki rate limiting: 10 request per menit per IP address
  • Jika limit tercapai, API akan mengembalikan status 429 Too Many Requests

Endpoints

POST /api/register Rate Limit: 100/min

Mendaftarkan pengguna baru.

Request Body:

{
  "name": "John Doe",
  "username": "johndoe",
  "password": "password123"
}

Validasi:

  • name: required, string, max 255 karakter
  • username: required, string, unique
  • password: required, string, min 8 karakter

Response Success (201):

{
  "token": "1|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "user": {
    "id": 1,
    "name": "John Doe",
    "username": "johndoe",
    "created_at": "2026-01-06T10:00:00.000000Z",
    "updated_at": "2026-01-06T10:00:00.000000Z"
  }
}

Request Headers:

Accept: application/json
Content-Type: application/json

cURL Example:

curl -X POST http://localhost:8000/api/register \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "username": "johndoe",
    "password": "password123"
  }'
POST /api/login Rate Limit: 100/min

Login dengan email dan password.

Request Body:

{
  "username": "johndoe",
  "password": "password123"
}

Request Headers:

Accept: application/json
Content-Type: application/json

Response Success (200):

{
  "message": "Login berhasil",
  "token": "1|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "id_user": 1,
  "name": "John Doe",
  "last_page": "page-3",
  "completed": false,
  "answer": []
}

Response Error (400):

{
  "message": "Username atau password salah"
}

cURL Example:

curl -X POST http://localhost:8000/api/login \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
    "username": "johndoe",
    "password": "password123"
  }'
POST /api/logout Auth Required

Logout dan menghapus token akses saat ini.

Request Headers:

Accept: application/json
Content-Type: application/json
Authorization: Bearer {token}

Response Success (200):

{
  "message": "Logged out"
}

cURL Example:

curl -X POST http://localhost:8000/api/logout \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {token}"
GET /api/user Auth Required

Mendapatkan informasi pengguna yang sedang login.

Request Headers:

Accept: application/json
Authorization: Bearer {token}

Response Success (200):

{
  "id": 1,
  "name": "John Doe",
  "username": "johndoe",
  "created_at": "2026-01-06T10:00:00.000000Z",
  "updated_at": "2026-01-06T10:00:00.000000Z"
}

cURL Example:

curl -X GET http://localhost:8000/api/user \
  -H "Accept: application/json" \
  -H "Authorization: Bearer {token}"
POST /api/response-game/save Auth Required

Menyimpan response game dari pengguna.

Request Headers:

Accept: application/json
Content-Type: application/json
Authorization: Bearer {token}

Request Body:

{
  "type_response_id": 1,
  "question_id": 5,
  "user_id": 1,
  "phase": 1,
  "answer": {"key": "value"},
  "score": 100,
  "page": "page-3",
  "completed": false
}

Validasi:

  • type_response_id: required, exists di tabel type_responses
  • question_id: required, integer
  • user_id: required, exists di tabel users
  • phase: required, integer
  • answer: required, json
  • score: required, integer
  • page: required, string
  • completed: required, boolean

Response Error (422) - Validasi Gagal:

{
  "message": "The given data was invalid.",
  "errors": {
    "type_response_id": ["The type response id field is required.", "The selected type response id is invalid."],
    "user_id": ["The user id field is required.", "The selected user id is invalid."],
    "answer": ["The answer field must be a valid JSON string."]
  }
}

Response Success (201):

{
  "message": "Response game saved successfully",
  "data": {
    "id": 1,
    "type_response_id": 1,
    "question_id": 5,
    "user_id": 1,
    "phase": 1,
    "answer": {"key": "value"},
    "score": 100,
    "page": "page-3",
    "completed": false
  }
}

cURL Example:

curl -X POST http://localhost:8000/api/response-game/save \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {token}" \
  -d '{
    "type_response_id": 1,
    "question_id": 5,
    "user_id": 1,
    "phase": 1,
    "answer": {"key": "value"},
    "score": 100,
    "page": "page-3",
    "completed": false
  }'
GET /api/response-game/get Auth Required

Mendapatkan semua response game dengan pagination.

Query Parameters:

  • page: Nomor halaman (optional, default: 1)
  • per_page: Jumlah data per halaman (optional, default: 10)

Request Headers:

Accept: application/json
Authorization: Bearer {token}

Response Success (200):

{
  "message": "List of response games",
  "data": [
    {
      "id": 1,
      "type_response_id": 1,
      "question_id": 5,
      "user_id": 1,
      "phase": 1,
      "answer": {"key": "value"},
      "score": 100,
      "page": "page-3",
      "completed": false
    }
  ],
  "pagination": {
    "current_page": 1,
    "last_page": 5,
    "per_page": 10,
    "total": 50
  }
}

Field Response:

  • message: Pesan sukses (string)
  • data: Array response games (array of objects)
  • pagination: Informasi pagination (object)
    • current_page: Halaman saat ini (integer)
    • last_page: Halaman terakhir (integer)
    • per_page: Jumlah data per halaman (integer)
    • total: Total data (integer)

cURL Example:

# Get first page with default 10 items per page
curl -X GET "http://localhost:8000/api/response-game/get" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer {token}"

# Get page 2 with 20 items per page
curl -X GET "http://localhost:8000/api/response-game/get?page=2&per_page=20" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer {token}"
GET /api/response-game/{id} Auth Required

Mendapatkan response game terbaru berdasarkan user ID.

URL Parameters:

  • id: User ID (integer, required, must exist in users table)

Validasi:

  • id: required, integer, exists di tabel users

Request Headers:

Accept: application/json
Authorization: Bearer {token}

Response Success (200):

{
  "id_user": 1,
  "name": "John Doe",
  "last_page": "page-3",
  "completed": false,
  "answer": {"key": "value"}
}

Response Error (404):

{
  "message": "User not found"
}

Response Error (422) - Validasi Gagal:

{
  "message": "The given data was invalid.",
  "errors": {
    "id": ["User ID must be an integer", "User not found"]
  }
}

cURL Example:

curl -X GET http://localhost:8000/api/response-game/1 \
  -H "Accept: application/json" \
  -H "Authorization: Bearer {token}"
GET /api/response-game/all/{id} Auth Required

Mendapatkan semua response game berdasarkan user ID.

URL Parameters:

  • id: User ID (integer, required, must exist in users table)

Validasi:

  • id: required, integer, exists di tabel users

Request Headers:

Accept: application/json
Authorization: Bearer {token}

Response Success (200):

{
  "message": "All response games for user found",
  "user": {
    "id": 1,
    "name": "John Doe"
  },
  "responses": [
    {
      "id": 1,
      "type_response_id": 1,
      "question_id": 5,
      "user_id": 1,
      "phase": 1,
      "answer": {"key": "value"},
      "score": 100,
      "page": "page-3",
      "completed": false,
      "created_at": "2026-01-06T10:00:00.000000Z",
      "updated_at": "2026-01-06T10:00:00.000000Z"
    },
    {
      "id": 2,
      "type_response_id": 2,
      "question_id": 6,
      "user_id": 1,
      "phase": 2,
      "answer": {"key": "value2"},
      "score": 200,
      "page": "page-4",
      "completed": true,
      "created_at": "2026-01-06T11:00:00.000000Z",
      "updated_at": "2026-01-06T11:00:00.000000Z"
    }
  ]
}

Field Response:

  • message: Pesan sukses (string)
  • user: Informasi user (object)
    • id: ID pengguna (integer)
    • name: Nama pengguna (string)
  • responses: Array semua response games untuk user tersebut (array of objects)
    • id: ID response game (integer)
    • type_response_id: ID tipe response (integer)
    • question_id: ID pertanyaan (integer)
    • user_id: ID pengguna (integer)
    • phase: Nomor phase (integer)
    • answer: Jawaban dalam format array (array)
    • score: Skor (integer)
    • page: Halaman (string)
    • completed: Status selesai (boolean)
    • created_at: Waktu dibuat (string)
    • updated_at: Waktu diupdate (string)

Response Error (404):

{
  "message": "User not found"
}

Response Error (422) - Validasi Gagal:

{
  "message": "The given data was invalid.",
  "errors": {
    "id": ["User ID must be an integer", "User not found"]
  }
}

cURL Example:

curl -X GET http://localhost:8000/api/response-game/all/1 \
  -H "Accept: application/json" \
  -H "Authorization: Bearer {token}"
GET /api/response-game/certificate/{id} Auth Required

Mendapatkan data sertifikat berdasarkan user ID. Endpoint ini mengambil seluruh response user dengan type_response_id = 3 dan mengelompokkan skor per phase.

URL Parameters:

  • id: User ID (integer, required, must exist in users table)

Validasi:

  • id: required, integer, exists di tabel users

Request Headers:

Accept: application/json
Authorization: Bearer {token}

Response Success (200):

{
  "id_user": 1,
  "name": "John Doe",
  "score": [
    {
      "phase": 1,
      "total_score": 150
    },
    {
      "phase": 2,
      "total_score": 200
    },
    {
      "phase": 3,
      "total_score": 180
    }
  ]
}

Field Response:

  • id_user: ID pengguna (number)
  • name: Nama pengguna (string)
  • score: Array yang berisi skor total per phase (array of objects)
    • phase: Nomor phase (integer)
    • total_score: Total skor untuk phase tersebut (integer)

Response Error (404):

{
  "message": "User not found"
}

Response Error (422) - Validasi Gagal:

{
  "message": "The given data was invalid.",
  "errors": {
    "id": ["User ID must be an integer", "User not found"]
  }
}

cURL Example:

curl -X GET http://localhost:8000/api/response-game/certificate/1 \
  -H "Accept: application/json" \
  -H "Authorization: Bearer {token}"

Error Handling

Status Code Description
200 OK - Request berhasil
201 Created - Resource berhasil dibuat
400 Bad Request - Request tidak valid
401 Unauthorized - Tidak terautentikasi
404 Not Found - Resource tidak ditemukan
422 Unprocessable Entity - Validasi gagal
429 Too Many Requests - Rate limit terlampaui

Contoh Penggunaan

JavaScript (Fetch API)

// Login
const login = async () => {
  const response = await fetch('http://localhost:8000/api/login', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      username: 'johndoe',
      password: 'password123'
    })
  });

  const data = await response.json();
  if (response.ok) {
    localStorage.setItem('token', data.token);
    console.log('User:', {
      id_user: data.id_user,
      name: data.name,
      last_page: data.last_page,
      completed: data.completed,
      answer: data.answer
    });
  } else {
    console.error('Error:', data.message);
  }
};

// Get Current User
const getCurrentUser = async () => {
  const token = localStorage.getItem('token');
  const response = await fetch('http://localhost:8000/api/user', {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Authorization': `Bearer ${token}`,
    }
  });

  const data = await response.json();
  if (response.ok) {
    console.log('Current User:', data);
  } else {
    console.error('Error:', data.message);
  }
};

// Save Response Game
const saveResponseGame = async () => {
  const token = localStorage.getItem('token');
  const response = await fetch('http://localhost:8000/api/response-game/save', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
    },
    body: JSON.stringify({
      type_response_id: 1,
      question_id: 5,
      user_id: 1,
      phase: 1,
      answer: { key: 'value' },
      score: 100,
      page: 'page-3'
    })
  });

  const data = await response.json();
  if (response.ok) {
    console.log('Response saved:', data.data);
  } else {
    console.error('Error:', data.message);
  }
};

// Get Response Game by User ID
const getResponseGameByUserId = async (userId) => {
  const token = localStorage.getItem('token');
  const response = await fetch(`http://localhost:8000/api/response-game/${userId}`, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Authorization': `Bearer ${token}`,
    }
  });

  const data = await response.json();
  if (response.ok) {
    console.log('Response Game:', data);
  } else {
    console.error('Error:', data.message);
  }
};

// Get All Response Games by User ID
const getAllResponseGamesByUserId = async (userId) => {
  const token = localStorage.getItem('token');
  const response = await fetch(`http://localhost:8000/api/response-game/all/${userId}`, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Authorization': `Bearer ${token}`,
    }
  });

  const data = await response.json();
  if (response.ok) {
    console.log('All Response Games:', data);
    console.log('User:', data.user);
    console.log('Total Responses:', data.responses.length);
    data.responses.forEach((response, index) => {
      console.log(`Response ${index + 1}:`, response);
    });
  } else {
    console.error('Error:', data.message);
  }
};

// Get Certificate Data
const getCertificateData = async (userId) => {
  const token = localStorage.getItem('token');
  const response = await fetch(`http://localhost:8000/api/response-game/certificate/${userId}`, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Authorization': `Bearer ${token}`,
    }
  });

  const data = await response.json();
  if (response.ok) {
    console.log('Certificate Data:', data);
    console.log('User:', data.name);
    console.log('Scores by Phase:', data.score);
  } else {
    console.error('Error:', data.message);
  }
};