Logo
Live
2024Backend · AI API

TypeScript AI API

Production-ready Fastify REST API in TypeScript, integrated with the OpenAI API to deliver intelligent-assistant capabilities behind clean, typed endpoints.

FastifyTypeScriptOpenAI APINode.jsZodVitest

Screens

TypeScript AI API screenshot 1

The Business Problem

Teams integrating an LLM into a product need a small, fast, well-tested API layer in front of OpenAI — with auth, rate limiting, and validation already handled — rather than wiring raw SDK calls into every service.

My Responsibilities

Built the API solo: route design, the AiService wrapper around OpenAI, the auth middleware, and the full Vitest test suite.

Main Workflows

  1. 1Client authenticates every AI request with an X-API-Key header
  2. 2POST /ai/chat proxies a multi-turn conversation to an OpenAI GPT model and returns the assistant's reply plus token usage
  3. 3POST /ai/summarize condenses long text into a paragraph or bullet-point summary
  4. 4GET /health reports API and AI service status for uptime checks

Tech Stack & Architecture

Client App
Fastify REST API (TypeScript)
OpenAI API

Backend API service — no UI to screenshot. See the source for full endpoint documentation.

Fastify chosen over Express for 2–3x throughput, native TypeScript support, and schema-based validation
Zod schemas validate every request body at runtime, mirroring the TypeScript types with clearer error messages than raw JSON Schema
Service layer pattern: AiService is injected into routes, so it's mockable in tests without real HTTP calls to OpenAI
App factory pattern (buildApp()) returns a fresh Fastify instance per test, avoiding port conflicts in the suite

Security & Deployment

X-API-Key header required on every AI route
60 requests/min per IP via @fastify/rate-limit
Security headers via @fastify/helmet
Multi-stage Dockerfile + docker-compose for containerized deployment; auto-generated Swagger docs at /docs

Results

Full unit + integration test suite via Vitest, covering routes, the service layer, and error handling for 400/401/403/404/500.