symmetrical-robot

Quest Tracker API - Activity 11 - Discovery Challenge

Welcome to your advanced Express.js API project! This template helps you build a production-ready REST API for a gamified quest management system with authentication, rate limiting, and comprehensive error handling.

🎯 Learning Objectives

By completing this activity, you will:

πŸš€ Getting Started

⚑ Quick Start (See Results in 30 Seconds!)

IMPORTANT: This template includes WORKING API ENDPOINTS! Test them immediately:

  1. Navigate to this folder in your terminal/command prompt
  2. Install dependencies (REQUIRED - one-time setup):
    npm install
    
  3. Start the Express server:
    npm start
    

    Server runs on: http://localhost:3000

  4. Open test.html in your browser to interact with the API
  5. Try the working endpoints - they return real data immediately!
    • Get all quests (with filtering)
    • Get player profiles and leaderboards
    • Browse quest categories
    • Complete quests and earn XP

🎯 What’s Already Working

70% of the API is implemented for you:

πŸ“ Your Learning Tasks

  1. First, test the working endpoints using test.html or curl commands
  2. Study the existing route patterns in routes/quests.js, routes/players.js
  3. Complete the TODO sections following Express.js best practices
  4. Add your own creative API features

πŸ“ Project Structure

activity-11-quest-api/
β”œβ”€β”€ server.js              # Main Express server (middleware setup)
β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ quests.js          # Quest CRUD endpoints
β”‚   β”œβ”€β”€ players.js         # Player profiles and leaderboards
β”‚   └── categories.js      # Quest categories
β”œβ”€β”€ middleware/            # Authentication & logging (TODO section)
β”œβ”€β”€ test.html             # Interactive API testing interface
β”œβ”€β”€ package.json          # Dependencies and npm scripts
└── README.md            # This file

πŸ“‹ Tasks to Complete

TODO 1: Quest Completion Endpoint (Medium)

Complete the POST /api/quests/:id/complete endpoint to mark quests as completed and calculate XP rewards.

API Endpoint: POST /api/quests/:id/complete

Success Criteria:

Hint: Look at the enrichQuestData() function in routes/quests.js for the XP calculation pattern.

TODO 2: Quest Creation Endpoint (Medium)

Implement the POST /api/quests endpoint to create new quests with validation.

API Endpoint: POST /api/quests

Required Fields:

Success Criteria:

TODO 3: Custom Authentication Middleware (Challenge)

Create custom authentication middleware in the middleware/ folder to verify API keys.

File to create: middleware/auth.js

Requirements:

Success Criteria:

TODO 4: Advanced Quest Filtering (Challenge)

Enhance the GET /api/quests endpoint with advanced filtering capabilities.

Features to implement:

Success Criteria:

TODO 5: Player Statistics Enhancement (Easy)

Add calculated statistics to the player profile endpoint.

Endpoint to enhance: GET /api/players/:username

Statistics to add:

Success Criteria:

πŸ› οΈ API Reference

πŸ† Quest Endpoints

GET /api/quests (Working βœ…)

Get all quests with filtering and sorting options.

Query Parameters:

Example:

curl "http://localhost:3000/api/quests?status=pending&priority=high&api_key=demo_key_12345"

GET /api/quests/:id (Working βœ…)

Get detailed information about a specific quest with enriched data.

Features included:

POST /api/quests/:id/complete (TODO ⚠️)

Mark a quest as completed and calculate XP rewards.

POST /api/quests (TODO ⚠️)

Create a new quest with custom parameters.

πŸ‘€ Player Endpoints

GET /api/players/:username (Working βœ…)

Get comprehensive player profile and statistics.

Available Players:

Query Parameters:

GET /api/players (Working βœ…)

Get player leaderboard with rankings and activity scores.

Query Parameters:

πŸ“‚ Category Endpoints

GET /api/categories (Working βœ…)

Get all quest categories with optional statistics.

Available Categories:

Query Parameters:

GET /api/categories/:id (Working βœ…)

Get detailed category information with tips and suggestions.

πŸ” Authentication

API Key System

All /api/* endpoints require authentication via API key.

Demo API Keys (use for testing):

Authentication Methods:

  1. Header Method (Recommended):
    curl -H "X-API-Key: demo_key_12345" http://localhost:3000/api/quests
    
  2. Query Parameter Method:
    curl "http://localhost:3000/api/quests?api_key=demo_key_12345"
    

Security Features:

πŸ§ͺ Testing Your API

Open test.html in your browser for a comprehensive testing interface:

Command Line Testing

Quick test commands:

# 1. Get all quests (test basic endpoint)
curl "http://localhost:3000/api/quests?api_key=demo_key_12345"

# 2. Get specific quest (test parameterized route)
curl "http://localhost:3000/api/quests/1?api_key=demo_key_12345"

# 3. Get player profile (test data enrichment)
curl "http://localhost:3000/api/players/alex?api_key=demo_key_12345"

# 4. Filter quests by priority (test query parameters)
curl "http://localhost:3000/api/quests?priority=high&api_key=demo_key_12345"

# 5. Get categories with stats
curl "http://localhost:3000/api/categories?include_stats=true&api_key=demo_key_12345"

# 6. Test authentication (should fail without API key)
curl http://localhost:3000/api/quests

Manual Testing Checklist

Debugging Tips

  1. Check server console for request logs (Morgan middleware)
  2. Use browser DevTools Network tab when testing via test.html
  3. Add console.log() statements in route handlers
  4. Test with curl to isolate frontend/backend issues
  5. Check /health endpoint to verify server status

πŸš€ Extension Challenges

Ready to take your API skills further? Try these bonus features:

Beginner Extensions

Intermediate Extensions

Advanced Extensions

Creative Extensions

πŸŽ“ Learning Resources

Express.js & REST APIs

Authentication & Security

Testing & Debugging

πŸ† Success Criteria

Your project is complete when:

πŸŽ‰ Congratulations!

Once you complete this project, you’ll have:

This foundation will serve you well in building real-world APIs, microservices, and full-stack applications!


Need Help?

Common Issues:

Happy coding!