No description
  • TypeScript 99.7%
  • HTML 0.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
akib-cdda 866ba82050 Add filtering and sorting to getAllChirps query
Support optional author filtering and configurable sort order
(ascending or descending by creation date).
2026-09-06 17:32:43 +06:00
src Add filtering and sorting to getAllChirps query 2026-09-06 17:32:43 +06:00
.env ch9-webhooks 2026-09-06 17:24:14 +06:00
.gitignore CH-6: Storage 2026-09-06 14:24:37 +06:00
.nvmrc CH-6: Storage 2026-09-06 14:24:37 +06:00
chirpy.png CH-6: Storage 2026-09-06 14:24:37 +06:00
drizzle.config.ts CH-6: Storage 2026-09-06 14:24:37 +06:00
package-lock.json ch7-Authentication 2026-09-06 17:14:52 +06:00
package.json ch7-Authentication 2026-09-06 17:14:52 +06:00
README.md Add filtering and sorting to getAllChirps query 2026-09-06 17:32:43 +06:00
server.log CH-6: Storage 2026-09-06 14:24:37 +06:00
tsconfig.json CH-6: Storage 2026-09-06 14:24:37 +06:00

Chirpy Backend API

A RESTful backend API for Chirpy (a Twitter/X clone) built with TypeScript, Node.js, Express, Drizzle ORM, and PostgreSQL.


Features

  • Authentication & Security:
    • Password hashing with Argon2id.
    • Stateless JWT Access Tokens (1-hour expiry).
    • Database-backed Refresh Tokens (60-day expiry, revocation support).
    • API Key authentication for external webhooks.
  • User Management:
    • Create user accounts.
    • Update user profile (email & password) with token authentication.
    • "Chirpy Red" premium membership upgrade.
  • Chirps (Posts):
    • Create chirps (140 character limit, tied to authenticated user).
    • Retrieve all chirps with filtering by authorId and sorting (asc / desc).
    • Retrieve single chirp by ID.
    • Delete chirp (author authorization check).
  • Webhooks:
    • Polka payment webhook (POST /api/polka/webhooks) for automatic Chirpy Red membership upgrades.
  • Admin & Observability:
    • Request logging and metrics counter (/admin/metrics).
    • Database reset endpoint for development environment (/admin/reset).
    • Health check endpoint (/api/healthz).

Tech Stack

  • Runtime: Node.js (ES Modules)
  • Language: TypeScript
  • Framework: Express.js
  • Database: PostgreSQL
  • ORM: Drizzle ORM & Drizzle Kit
  • Auth & Cryptography: Argon2, JSONWebToken, Crypto
  • Validation: Zod
  • Testing: Vitest

Getting Started

Prerequisites

  • Node.js (v20+ recommended)
  • PostgreSQL database

1. Environment Setup

Create a .env file in the root directory:

DB_URL="postgres://postgres:password@localhost:5432/chirpy?sslmode=disable"
PLATFORM="dev"
SECRET="your-jwt-secret-key"
POLKA_KEY="your-polka-webhook-api-key"

2. Install Dependencies

npm install

3. Run Database Migrations

Migrations run automatically on server start, or you can generate/push migrations with Drizzle Kit:

npx drizzle-kit generate

4. Start the Server

Development mode with hot reloading:

npm run dev

Production build and start:

npm run build
npm start

API Reference

Auth & Users

Method Endpoint Description Auth Required
POST /api/users Register a new user No
PUT /api/users Update user email & password Bearer JWT
POST /api/login Login and receive access + refresh tokens No
POST /api/refresh Get a new access token using refresh token Bearer Refresh Token
POST /api/revoke Revoke a refresh token (status 204) Bearer Refresh Token

Chirps

Method Endpoint Description Auth Required
POST /api/chirps Create a chirp ({ "body": "..." }) Bearer JWT
GET /api/chirps List chirps (?authorId=<id>&sort=asc|desc) No
GET /api/chirps/:chirpId Get chirp by ID No
DELETE /api/chirps/:chirpId Delete chirp (author only) Bearer JWT

Webhooks

Method Endpoint Description Auth Required
POST /api/polka/webhooks Upgrade user to Chirpy Red ApiKey <POLKA_KEY>

Admin & Health

Method Endpoint Description Auth Required
GET /api/healthz Readiness health check No
GET /admin/metrics View file server hit metrics No
POST /admin/reset Reset users (dev platform only) No

Testing

Run unit tests with Vitest:

npm test # or npx vitest run