Backend Architecture
Relevant source files
The following files were used as context for generating this wiki page:
- packages/twenty-server/src/app.module.ts
- packages/twenty-server/src/engine/twenty-orm/twenty-orm.module.ts
- packages/twenty-server/src/engine/api/graphql/workspace-schema.factory.ts
- CLAUDE.md
- README.md
- packages/twenty-server/src/engine/core-modules/open-api/utils/base-schema.utils.ts
- packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/constants/agent-system-prompts.const.ts
- packages/twenty-server/src/engine/metadata-modules/page-layout/services/tests/page-layout.service.spec.ts
The Twenty backend is a modern, modular application built on the NestJS framework. It serves as the core engine for the CRM, handling all business logic, data persistence, and API communications. The architecture is designed to be scalable and maintainable, leveraging a robust tech stack that includes TypeScript, PostgreSQL for the primary database, Redis for caching and session management, and BullMQ for background job processing.
A key aspect of the architecture is its dual-API approach, offering both a powerful, dynamically-generated GraphQL API for flexible data querying and a standard REST API with OpenAPI documentation for straightforward integrations. The system is built with multi-tenancy in mind, featuring a custom ORM layer that provides workspace-isolated data access and a GraphQL schema factory that generates unique schemas per workspace.
High-Level Overview
The backend system is composed of several key services and technologies that work together to power the Twenty CRM.
This diagram shows the main components of the backend stack and their interactions.
Technology Stack
The backend leverages a curated set of modern technologies to ensure performance, reliability, and developer productivity.
| Technology | Purpose |
|---|---|
| NestJS | The core application framework, providing a modular and scalable structure. |
| TypeORM | Object-Relational Mapper (ORM) for interacting with the PostgreSQL database. |
| PostgreSQL | The primary relational database for data persistence. |
| GraphQL (Yoga) | A flexible query language for the API, enabling clients to request exactly the data they need. |
| Redis | In-memory data store used for caching and session management. |
| BullMQ | A robust queue system for handling background jobs and asynchronous tasks. |
Sources: CLAUDE.md:65-71, README.md:104-105
Modular Structure
The backend is organized into NestJS modules to encapsulate different domains of functionality. The main AppModule serves as the root, importing feature-specific modules. This promotes separation of concerns and makes the codebase easier to navigate and extend.
Sources: packages/twenty-server/src/app.module.ts
Key Modules
| Module | Description | Source File |
|---|---|---|
CoreModule | Contains core business logic and entities like Companies, Contacts, and Opportunities. | packages/twenty-server/src/app.module.ts:119 |
MetadataModule | Manages the CRM's data model, including custom objects, fields, and page layouts. | packages/twenty-server/src/app.module.ts:120 |
TwentyOrmModule | A custom module that provides workspace-scoped TypeORM repositories. | packages/twenty-server/src/app.module.ts:101 |
ApiModule | Exposes the GraphQL and REST APIs. | packages/twenty-server/src/app.module.ts:100 |
QueueModule | Configures and manages the BullMQ message queues for background processing. | packages/twenty-server/src/app.module.ts:107 |
AiModule | Contains logic for AI-powered features, including the agent system. | packages/twenty-server/src/engine/metadata-modules/ai/ai.module.ts |
The diagram below illustrates how the AppModule orchestrates the various sub-modules.
Database and ORM
The backend uses TypeORM to interact with a PostgreSQL database. To handle multi-tenancy, a custom TwentyOrmModule is implemented. This module is responsible for providing workspace-specific repository instances, ensuring that data operations are always isolated to the correct workspace.
Sources: packages/twenty-server/src/engine/twenty-orm/twenty-orm.module.ts, CLAUDE.md:67
TwentyORM Global Manager
The TwentyORMGlobalManager is a central piece of this multi-tenant strategy. Services can inject this manager to request a repository for a specific entity within a given workspace context. This prevents data leakage between workspaces.
The following sequence diagram shows how a service retrieves a workspace-specific repository.
This pattern is visible in service tests, where the TwentyORMGlobalManager is mocked to provide repository instances.
Sources: packages/twenty-server/src/engine/metadata-modules/page-layout/services/tests/page-layout.service.spec.ts:37-42, packages/twenty-server/src/engine/twenty-orm/twenty-orm.module.ts
API Layer
The backend exposes two distinct APIs: GraphQL for rich, flexible client-side data fetching, and a REST API for standard integrations and webhooks.
GraphQL API
The GraphQL API is built using a code-first approach with GraphQL Yoga. A standout feature is its dynamic, per-workspace schema generation.
Workspace Schema Factory
The WorkspaceSchemaFactory is responsible for building a unique GraphQL schema for each workspace. This is crucial for supporting custom objects and fields. When a request comes in, the factory:
- Retrieves the metadata (objects, fields) for the specific workspace.
- Dynamically generates GraphQL object types, queries, and mutations based on that metadata.
- Constructs and caches the executable schema for that workspace.
This allows each user to have a GraphQL API tailored to their specific data model customizations.
The following diagram illustrates the dynamic schema generation process for a GraphQL request.
REST API (OpenAPI)
Alongside GraphQL, a REST API is provided for more traditional integrations. The API is documented using the OpenAPI 3.1 standard. The base schema for this documentation is generated dynamically and includes detailed usage instructions.
Sources: packages/twenty-server/src/engine/core-modules/open-api/utils/base-schema.utils.ts
Key features of the REST API include:
| Feature | Description |
|---|---|
| Authentication | Requires a Bearer token in the Authorization header for all requests. |
| Filtering | Supports complex filtering via the filter query parameter with comparators like eq, gte, in, ilike, etc. |
| Pagination | Uses cursor-based pagination with limit, starting_after, and ending_before parameters. |
| Ordering | Allows sorting results using the order_by parameter with specified directions. |
The OpenAPI schema itself is exposed via endpoints, which can be used by AI agents or other tools to understand and interact with the API.
Sources: packages/twenty-server/src/engine/core-modules/open-api/utils/base-schema.utils.ts:10-90
AI Agent System
The backend includes a sophisticated AI agent system designed to handle complex user requests. The system is fronted by a ROUTER agent that analyzes the user's intent and delegates the task to a specialized agent.
This flow shows how the AI Router agent directs tasks.
This modular agent design allows the system to handle a wide variety of tasks, from creating a new custom object (metadata-builder) to finding and updating a company record (data-manipulator).
Summary
The Twenty backend architecture is a well-structured, modern system built with NestJS. Its core strengths lie in its modularity, multi-tenant data handling via a custom ORM layer, and a flexible dual-API design. The dynamic, per-workspace GraphQL schema generation is a particularly powerful feature that enables deep customization. The inclusion of a REST API ensures broad compatibility, and the AI agent system points to a forward-looking approach to user interaction and automation.