Skip to content

Samrood-dev/API-Key-Usage-Tracker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

API Key Usage Tracker

A full-stack API usage tracking application built with NestJS, Next.js, Prisma, and PostgreSQL.

The application allows users to generate and use API keys, track api usage, adds token limits, and monitor API activity through a responsive dashboard.


Features

Authentication

  • User signup
  • User login
  • JWT-based authentication

API Key System

  • Unique API key generated for every user
  • Protected API access using x-api-key

Protected API

  • Accepts text/context input
  • token counting using word count
  • Tracks API usage
  • Enforces token usage limits
  • Rate limiting using NestJS Throttler

Dashboard

  • View API key
  • Total API calls
  • Tokens used
  • Remaining token limit
  • Recent API usage history

Tech Stack

Backend

  • NestJS
  • Prisma ORM
  • PostgreSQL
  • JWT Authentication
  • NestJS Throttler

Frontend

  • Next.js
  • TypeScript
  • Tailwind CSS
  • Axios

Installation

Backend Setup

Install dependencies

npm install

Create .env

DATABASE_URL="postgresql://postgres:password@localhost:5432/api_tracker"
JWT_SECRET="your_secret"
PORT=8000

Run Prisma migrations

npx prisma migrate dev

Start backend server

npm run start:dev

Backend runs on:

http://localhost:8000

Frontend Setup

Install dependencies

npm install

Create .env.local

NEXT_PUBLIC_API_URL=http://localhost:8000

Start frontend server

npm run dev

Frontend runs on:

http://localhost:3000

API Endpoints

Register

POST /api/auth/register

Request:

{
  "name": "Samrood",
  "email": "sam@example.com",
  "password": "123456"
}

Login

POST /api/auth/login

Request:

{
  "email": "sam@example.com",
  "password": "123456"
}

Protected API

POST /protected/process

Headers:

x-api-key: YOUR_API_KEY

Request:

{
  "content": "Hello this is a test request"
}

Response:

{
  "success": true,
  "tokensUsed": 6
}

Token Counting Logic

Token usage is simulated by counting words in the provided content.

Example:

"Hello this is a test"

Token count:

5

Rate Limiting

Protected API endpoints are rate limited using NestJS Throttler.

Current configuration:

  • 5 requests per minute

Dashboard Features

The dashboard displays:

  • User API key
  • Total API calls
  • Total tokens used
  • Remaining token limit
  • Recent API usage history

Assumptions

  • Token limits do not reset automatically.
  • API usage history stores successful requests only.
  • Authentication uses JWT tokens.

Screenshots

Dashboard

![Dashboard](./screenshots/dashboard.png)

About

API key usage tracking system built with NestJS, Next.js, Prisma, and PostgreSQL.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors