No description
- TypeScript 99.7%
- HTML 0.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Support optional author filtering and configurable sort order (ascending or descending by creation date). |
||
| src | ||
| .env | ||
| .gitignore | ||
| .nvmrc | ||
| chirpy.png | ||
| drizzle.config.ts | ||
| package-lock.json | ||
| package.json | ||
| README.md | ||
| server.log | ||
| tsconfig.json | ||
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
authorIdand 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.
- Polka payment webhook (
- Admin & Observability:
- Request logging and metrics counter (
/admin/metrics). - Database reset endpoint for development environment (
/admin/reset). - Health check endpoint (
/api/healthz).
- Request logging and metrics counter (
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