Rest endpoints

HTTP endpoints for querying historical data and game state.

Base URL

http://your-backend-domain:3001

Endpoints

GET /

Health check endpoint.

Response:

{
  "status": "ok",
  "message": "CAIVEMEN Backend"
}

Status Codes:

  • 200 OK - Service is healthy

Example:

curl http://localhost:3001/

GET /state

Returns current game state snapshot.

Response:

Status Codes:

  • 200 OK - State returned successfully

Use Case:

  • Initial page load before WebSocket connection

  • Backup if WebSocket fails

  • Server-side rendering

Example:


GET /history/events

Fetches recent events from Firestore.

Query Parameters:

Parameter
Type
Default
Description

limit

number

100

Max events to return (1-1000)

Response:

Event Types:

  • kill - Caveman killed another caveman

  • death - Caveman died (non-combat)

  • attack - Non-lethal combat

  • eat - Food consumed

  • disaster - Natural disaster

  • thought - AI thought logged

  • provocation - Taunt action

  • respawn - Caveman respawned

  • init - Simulation started

Status Codes:

  • 200 OK - Events returned

  • 500 Internal Server Error - Firestore error

Example:


GET /history/leaderboard

Returns all-time caveman statistics.

Response:

Sorting:

  • Default: By kills (descending)

  • Can be sorted client-side by any field

Status Codes:

  • 200 OK - Leaderboard returned

  • 500 Internal Server Error - Firestore error

Example:


GET /history/transactions

Returns transaction statistics.

Query Parameters:

Parameter
Type
Default
Description

period

string

"all"

Time period: "hour", "day", "week", "all"

Response:

Status Codes:

  • 200 OK - Stats returned

  • 400 Bad Request - Invalid period parameter

  • 500 Internal Server Error - Firestore error

Example:


Response Format

All endpoints return JSON with appropriate Content-Type: application/json header.

Success Response:

Error Response:

Rate Limiting

Current Limits:

  • No rate limiting implemented

  • Backend can handle ~1000 req/sec

  • Firestore has soft limits (~10k reads/sec)

Best Practices:

  • Cache responses client-side

  • Use WebSocket for real-time data

  • Only query historical endpoints when needed

CORS Configuration

Allowed Origins:

  • * (all origins allowed)

Allowed Methods:

  • GET, OPTIONS

Allowed Headers:

  • Content-Type, Authorization

Example CORS Headers:

Error Handling

HTTP Status Codes

Code
Meaning
Typical Cause

200

OK

Request successful

400

Bad Request

Invalid parameters

404

Not Found

Endpoint doesn't exist

500

Internal Server Error

Backend error

503

Service Unavailable

Backend down

Error Response Examples

Invalid Parameter:

Firestore Error:

Rate Limit (if implemented):

Client Examples

JavaScript/TypeScript

Python

cURL

Performance Considerations

Caching Strategy:

Parallel Requests:

Next Steps

  • Connect via WebSocket API for real-time updates

  • Explore Data Schema for Firestore structure

  • Learn System Architecture