CLAUDE LABJP
MCP — The July 28 MCP spec release candidate drops the Mcp-Session-Id header and goes stateless, so remote MCP servers no longer need sticky sessionsAPPS — The same release adds MCP Apps for server-rendered UI and a Tasks extension for long-running workMEMORY — The Python 0.116.0, TypeScript 0.110.0, and Go 1.56.0 SDKs now send agent-memory-2026-07-22 on every memory store callSPILL — Output from agent_toolset and MCP tools past 100K characters now spills to a file in the sandbox, with the model receiving a truncated preview it can expandBG — MCP tool calls running past two minutes move to the background automatically, keeping the session usable; tune it with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSRESUME — Typing /resume in the agent view opens a picker of past sessions and brings your pick back as a background sessionMCP — The July 28 MCP spec release candidate drops the Mcp-Session-Id header and goes stateless, so remote MCP servers no longer need sticky sessionsAPPS — The same release adds MCP Apps for server-rendered UI and a Tasks extension for long-running workMEMORY — The Python 0.116.0, TypeScript 0.110.0, and Go 1.56.0 SDKs now send agent-memory-2026-07-22 on every memory store callSPILL — Output from agent_toolset and MCP tools past 100K characters now spills to a file in the sandbox, with the model receiving a truncated preview it can expandBG — MCP tool calls running past two minutes move to the background automatically, keeping the session usable; tune it with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSRESUME — Typing /resume in the agent view opens a picker of past sessions and brings your pick back as a background session
Articles/API & SDK
API & SDK/2026-04-13Advanced

Building Enterprise AI Backends with Claude API and NestJS: Production

A complete production guide to integrating Claude API into NestJS using dependency injection, TypeORM, SSE streaming, JWT auth, and Bull queues—with working code you can deploy today.

NestJSTypeScript24Claude API115TypeORMEnterprise4Backend4SSE4Docker5

Premium Article

There's a specific moment in every backend codebase when things start going wrong. The Express /chat endpoint that started at 50 lines gradually absorbs auth logic, conversation history management, streaming, and rate limiting until it becomes a 1,000-line file that nobody wants to touch. New team members don't know where to add things. Tests are hard to write because dependencies are implicit. Everyone works around the problem instead of through it.

NestJS was designed to prevent exactly this. Its dependency injection system, module boundaries, and decorator-based patterns force the kind of structure that makes large codebases navigable. When you integrate Claude API into this structure, you end up with code that's easier to test, easier to hand off, and easier to extend.

This guide walks through building a production-grade Claude API backend with NestJS from first principles—covering every layer from the DI container to Docker Compose deployment.

Why NestJS Over Express or Hono

Hono and Express remain excellent choices for lightweight APIs, edge workers, and rapid prototypes. The case for NestJS is more specific: it pays off when teams grow and codebases need to be maintained long-term.

The cross-cutting concern problem: In Express, where to put middleware for auth, logging, and validation is an implicit convention that new team members have to learn by reading existing code. NestJS Guards, Interceptors, and Pipes have explicit, documented roles. Code review conversations shift from "where does this go?" to "is this the right implementation?"

Claude API client instance management: Calling new Anthropic() in multiple files means configuration changes have to be made in multiple places and mocking in tests becomes difficult. Registering the client in NestJS's DI container means every service gets the same configured instance, and tests can swap it out with a single provider override.

Extensibility: Adding a Bull queue, WebSocket gateway, or gRPC service to a NestJS app means creating a new module. The existing code doesn't change. In an unstructured Express app, the same additions often require refactoring existing files.

A practical decision framework: choose NestJS when your team is 5 or more people, when you need testable code, and when the service will be maintained for more than a year. For edge deployments, single-purpose microservices, or prototypes, Hono or Express remains the right call.

Project Architecture: Domain-Oriented Module Structure

src/
├── app.module.ts
├── main.ts
├── config/
│   └── anthropic.config.ts
├── ai/
│   ├── ai.module.ts
│   ├── ai.service.ts
│   ├── ai.controller.ts
│   └── dto/
│       ├── chat.dto.ts
│       └── stream-chat.dto.ts
├── conversation/
│   ├── conversation.module.ts
│   ├── conversation.service.ts
│   └── entities/
│       ├── conversation.entity.ts
│       └── message.entity.ts
├── auth/
│   ├── auth.module.ts
│   ├── auth.guard.ts
│   └── current-user.decorator.ts
└── health/
    └── health.controller.ts

The key architectural decision here is the direction of dependencies: ai/ depends on conversation/, but not the reverse. Claude API call logic is contained in ai.service.ts. When you switch models or providers in the future, the blast radius is limited to that one service.

Install dependencies:

npm i -g @nestjs/cli
nest new claude-enterprise-api
cd claude-enterprise-api
npm install @anthropic-ai/sdk @nestjs/config @nestjs/typeorm typeorm pg
npm install @nestjs/bull bull @nestjs/jwt @nestjs/throttler @nestjs/terminus
npm install -D @types/bull

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
If you've been managing Claude API integrations in Express and finding them increasingly hard to maintain as your team grows, you'll come away with a NestJS module structure that makes ownership and testing obvious from day one
You'll get copy-paste-ready implementations for TypeORM conversation history, SSE streaming with proper disconnect handling, and Bull queue-based async processing—all wired together and explained
You'll leave with a deployable Docker Compose stack including JWT auth, throttling, and health checks that you can adapt directly to your own product
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API & SDK2026-04-13
Implementing Claude API SSE Streaming in Next.js App Router
Learn how to implement Server-Sent Events streaming from the Claude API in Next.js App Router. Covers ReadableStream, React hooks, cancellation, and error handling with production-ready code.
API & SDK2026-07-09
When the RAG Started Being Confidently Wrong — Field Notes on Measuring Retrieval Misses With Groundedness
In a Claude API RAG, the answers stay fluent while the facts drift. Often the cause is a silent recall decay on the retrieval side, missing the document that holds the answer. Field notes on measuring groundedness and retrieval hit rate and walking the system back, with working code and real numbers.
API & SDK2026-07-08
Contract-Test Every Tool Before You Submit or Automate an MCP Connector
A connector that works once in a chat can still break silently in an unattended job through misread response shapes or double-fired writes. Here is a small harness that machine-checks tool descriptions, response contracts, idempotency, and latency, with measured numbers.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →