Skip to content

Ahmad-shopify-dev/api-learning-with-api-key

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“š Stack Library API

πŸ“– Project Overview

Stack Library API is a RESTful API built using Node.js and Express.js that demonstrates modern backend development practices.

This project includes:

  • API Key Authentication
  • HATEOAS Implementation
  • CRUD Operations
  • Pagination
  • Rate Limiting
  • Logging
  • Usage Tracking
  • Modular Architecture
  • Global Error Handling

This API simulates a real-world production-ready backend system using a dummy JSON-based database.


πŸš€ Features

πŸ” Authentication

  • Login-based access
  • API Key generation
  • Header-based API authentication

Header format:

x-api-key: stack_xxxxxxxxx

πŸ”— HATEOAS Support

After login, the API returns navigational links such as:

  • Generate API Key
  • Get Books
  • Access Resources

πŸ“š CRUD Operations

Supports:

  • Create books
  • Read books
  • Pagination support
  • Resource access control

πŸ“„ Pagination

Supported query parameters:

?page=1
&limit=5

Example:

GET /api/books?page=1&limit=5

⚑ Rate Limiting

Limits:

5 Requests per minute

Prevents:

  • Server overload
  • API abuse

🧾 Request Logging

Logs include:

  • HTTP Method
  • Route
  • Status Code
  • Response Time

πŸ“Š API Usage Tracking

Tracks:

  • Request count
  • Last used timestamp

Example:

"usage": {
    "requestCount": 12,
    "lastUsed": "2026-04-16T08:50:00Z"
}

❌ Global Error Handling

Centralized error responses.

Example:

{
  "success": false,
  "error": "Route not found"
}

πŸ—οΈ Folder Structure

project-root
β”‚
β”œβ”€β”€ config
β”‚   └── db.js
β”‚
β”œβ”€β”€ controllers
β”‚   β”œβ”€β”€ authController.js
β”‚   β”œβ”€β”€ bookController.js
β”‚   β”œβ”€β”€ usageController.js
β”‚   └── docsController.js
β”‚
β”œβ”€β”€ middleware
β”‚   β”œβ”€β”€ apiKeyMiddleware.js
β”‚   β”œβ”€β”€ rateLimiter.js
β”‚   β”œβ”€β”€ errorHandler.js
β”‚
β”œβ”€β”€ routes
β”‚   β”œβ”€β”€ authRoutes.js
β”‚   β”œβ”€β”€ bookRoutes.js
β”‚   β”œβ”€β”€ usageRoutes.js
β”‚   └── docsRoutes.js
β”‚
β”œβ”€β”€ data
β”‚   β”œβ”€β”€ users.json
β”‚   └── books.json
β”‚
β”œβ”€β”€ utils
β”‚   └── generateApiKey.js
β”‚
β”œβ”€β”€ .env
β”œβ”€β”€ app.js
β”œβ”€β”€ package.json
└── README.md

βš™οΈ Installation Guide

Step 1 β€” Clone Project

git clone <repository-url>

Step 2 β€” Install Dependencies

npm install

Step 3 β€” Create .env File

Example:

PORT=5000

API_KEY_HEADER=x-api-key

RATE_LIMIT_MAX=5

RATE_LIMIT_WINDOW=60000

Step 4 β€” Start Server

npm start

or

node app.js

Server runs at:

http://localhost:5000

πŸ”‘ Authentication Flow

Step 1 β€” Login

POST /api/login

Body:

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

Response:

{
  "success": true,
  "apiKey": "stack_xxxxx",
  "links": [...]
}

Step 2 β€” Use API Key

Add header:

x-api-key: stack_xxxxx

Then access:

GET /api/books

πŸ“‘ API Endpoints


πŸ” Login User

POST /api/login

Body:

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

πŸ”‘ Generate API Key

POST /api/generate-api-key

πŸ“š Get Books

GET /api/books

Optional:

?page=1
&limit=5

βž• Create Book

POST /api/books

Body:

{
  "title": "Book Title",
  "author": "Author Name"
}

πŸ“Š Get Usage Stats

GET /api/usage

πŸ“„ API Documentation

GET /api/docs

πŸ› οΈ Technologies Used

  • Node.js
  • Express.js
  • UUID
  • Morgan (Logger)
  • Express Rate Limit
  • dotenv
  • JSON File Database

🎯 Learning Goals Achieved

This project demonstrates:

  • RESTful API design
  • API Key authentication
  • HATEOAS implementation
  • Middleware architecture
  • Pagination logic
  • Rate limiting
  • Logging system
  • Usage analytics
  • Modular backend structure

πŸ“Œ Future Improvements

Possible upgrades:

  • Database Integration (MongoDB / PostgreSQL)
  • JWT Authentication
  • Swagger Documentation
  • Role-Based Access Control
  • API Versioning

πŸ‘¨β€πŸ’» Author

StackWiseDev Developer


πŸ“œ License

This project is for learning and educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors