twenty

The open alternative to Salesforce, designed for AI.

54,836 stars TypeScript Markdown Skills API Spec #crm#crm-system#customer#good-first-issue
AI Prompts & Specs

Repository: twentyhq/twenty


Stars: 44499

CLAUDE.md

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

Project Overview

Twenty is an open-source CRM built with modern technologies in a monorepo structure. The codebase is organized as an Nx workspace with multiple packages.

Key Commands

Development


bash

Start development environment (frontend + backend + worker)


yarn start

Individual package development


npx nx start twenty-front # Start frontend dev server
npx nx start twenty-server # Start backend server
npx nx run twenty-server:worker # Start background worker

Testing


bash

Preferred: run a single test file (fast)


npx jest path/to/test.test.ts --config=packages/PROJECT/jest.config.mjs

Run all tests for a package


npx nx test twenty-front # Frontend unit tests
npx nx test twenty-server # Backend unit tests
npx nx run twenty-server:test:integration:with-db-reset # Integration tests with DB reset

To run an indivual test or a pattern of tests, use the following command:


cd packages/{workspace} && npx jest "pattern or filename"

Storybook


npx nx storybook:build twenty-front
npx nx storybook:test twenty-front

When testing the UI end to end, click on "Continue with Email" and use the prefilled credentials.

Code Quality


bash

Linting (diff with main - fastest, always prefer this)


npx nx lint:diff-with-main twenty-front
npx nx lint:diff-with-main twenty-server
npx nx lint:diff-with-main twenty-front --configuration=fix # Auto-fix

Linting (full project - slower, use only when needed)


npx nx lint twenty-front
npx nx lint twenty-server

Type checking


npx nx typecheck twenty-front
npx nx typecheck twenty-server

Format code


npx nx fmt twenty-front
npx nx fmt twenty-server

Build


bash

Build packages (twenty-shared must be built first)


npx nx build twenty-shared
npx nx build twenty-front
npx nx build twenty-server

Database Operations


bash

Database management


npx nx database:reset twenty-server # Reset database
npx nx run twenty-server:database:init:prod # Initialize database
npx nx run twenty-server:database:migrate:prod # Run instance commands (fast only)

Generate an instance command (fast or slow)


npx nx run twenty-server:database:migrate:generate --name <name> --type <fast|slow>

Database Inspection (Postgres MCP)

A read-only Postgres MCP server is configured in .mcp.json. Use it to:
- Inspect workspace data, metadata, and object definitions while developing
- Verify migration results (columns, types, constraints) after running migrations
- Explore the multi-tenant schema structure (core, metadata, workspace-specific schemas)
- Debug issues by querying raw data to confirm whether a bug is frontend, backend, or data-level
- Inspect metadata tables to debug GraphQL schema generation issues

This server is read-only โ€” for write operations (reset, migrations, sync), use the CLI commands above.

GraphQL


bash

Generate GraphQL types (run after schema changes)


npx nx run twenty-front:graphql:generate
npx nx run twenty-front:graphql:generate --configuration=metadata

Architecture Overview

Tech Stack


- Frontend: React 18, TypeScript, Jotai (state management), Linaria (styling), Vite
- Backend: NestJS, TypeORM, PostgreSQL, Redis, GraphQL (with GraphQL Yoga)
- Monorepo: Nx workspace managed with Yarn 4

Package Structure


text
packages/
โ”œโ”€โ”€ twenty-front/ # React frontend application
โ”œโ”€โ”€ twenty-server/ # NestJS backend API
โ”œโ”€โ”€ twenty-ui/ # Shared UI components library
โ”œโ”€โ”€ twenty-shared/ # Common types and utilities
โ”œโ”€โ”€ twenty-emails/ # Email templates with React Email
โ”œโ”€โ”€ twenty-website/ # Next.js documentation website
โ”œโ”€โ”€ twenty-zapier/ # Zapier integration
โ””โ”€โ”€ twenty-e2e-testing/ # Playwright E2E tests

Key Development Principles


- Functional components only (no class components)
- Named exports only (no default exports)
- Types over interfaces (except when extending third-party interfaces)
- String literals over enums (except for GraphQL enums)
- No 'any' type allowed โ€” strict TypeScript enforced
- Event handlers preferred over useEffect for state updates
- Props down, events up โ€” unidirectional data flow
- Composition over inheritance
- No abbreviations in variable names (user not u, fieldMetadata not fm)

Naming Conventions


- Variables/functions: camelCase
- Constants: SCREAMING_SNAKE_CASE
- Types/Classes: PascalCase (suffix component props with Props, e.g. ButtonProps)
- Files/directories: kebab-case with descriptive suffixes (.component.tsx, .service.ts, .entity.ts, .dto.ts, .module.ts)
- TypeScript generics: descriptive names (TData not T)

File Structure


- Components under 300 lines, services under 500 lines
- Components in their own directories with tests and stories
- Use index.ts barrel exports for clean imports
- Import order: external libraries first, then internal (@/), then relative

Comments


- Use short-form comments (//), not JSDoc blocks
- Explain WHY (business logic), not WHAT
- Do not comment obvious code
- Multi-line comments use multiple // lines, not / */

State Management


- Jotai for global state: atoms for primitive state, selectors for derived state, atom families for dynamic collections
- Component-specific state with React hooks (useState, useReducer for complex logic)
- GraphQL cache managed by Apollo Client
- Use functional state updates: setState(prev => prev + 1)

Backend Architecture


- NestJS modules for feature organization
- TypeORM for database ORM with PostgreSQL
- GraphQL API with code-first approach
- Redis for caching and session management
- BullMQ for background job processing

Database & Upgrade Commands


- PostgreSQL as primary database
- Redis for caching and sessions
- ClickHouse for analytics (when enabled)
- When changing entity files, generate an instance command (database:migrate:generate --name <name> --type <fast|slow>)
- Fast instance commands handle schema changes; slow ones add a runDataMigration step for data backfills
- Workspace commands iterate over all active/suspended workspaces for per-workspace upgrades
- Commands use @RegisteredInstanceCommand and @RegisteredWorkspaceCommand decorators for automatic discovery
- Include both up and down logic in instance commands
- Never delete or rewrite committed instance command up/down logic
- See packages/twenty-server/docs/UPGRADE_COMMANDS.md for full documentation

Utility Helpers


Use existing helpers from twenty-shared instead of manual type guards:
- isDefined(), isNonEmptyString(), isNonEmptyArray()

Development Workflow

IMPORTANT: Use Context7 for code generation, setup or configuration steps, or library/API documentation. Automatically use the Context7 MCP tools to resolve library IDs and get library docs without waiting for explicit requests.

Before Making Changes


1. Always run linting (lint:diff-with-main) and type checking after code changes
2. Test changes with relevant test suites (prefer single-file test runs)
3. Ensure instance commands are generated for entity changes (database:migrate:generate)
4. Check that GraphQL schema changes are backward compatible
5. Run graphql:generate after any GraphQL schema changes

Code Style Notes


- Use Linaria for styling with zero-runtime CSS-in-JS (styled-components pattern)
- Follow Nx workspace conventions for imports
- Use Lingui for internationalization
- Apply security first, then formatting (sanitize before format)

Testing Strategy


- Test behavior, not implementation โ€” focus on user perspective
- Test pyramid: 70% unit, 20% integration, 10% E2E
- Query by user-visible elements (text, roles, labels) over test IDs
- Use @testing-library/user-event for realistic interactions
- Descriptive test names: "should [behavior] when [condition]"
- Clear mocks between tests with jest.clearAllMocks()

Dev Environment Setup

All dev environments (Claude Code web, Cursor, local) use one script:

bash
bash packages/twenty-utils/setup-dev-env.sh

This handles everything: starts Postgres + Redis (auto-detects local services vs Docker), creates databases, and copies .env files. Idempotent โ€” safe to run multiple times.

- --docker โ€” force Docker mode (uses packages/twenty-docker/docker-compose.dev.yml)
- --down โ€” stop services
- --reset โ€” wipe data and restart fresh
- Skip the setup script for tasks that only read code โ€” architecture questions, code review, documentation, etc.

Note: CI workflows (GitHub Actions) manage services via Actions service containers and run setup steps individually โ€” they don't use this script.

Important Files


- nx.json - Nx workspace configuration with task definitions
- tsconfig.base.json - Base TypeScript configuration
- package.json - Root package with workspace definitions
- .cursor/rules/ - Detailed development guidelines and best practices


README.md

<p align="center">
<a href="https://www.twenty.com">
<img src="./packages/twenty-website/public/images/core/logo.svg" width="100px" alt="Twenty logo" />
</a>
</p>

<h2 align="center" >The #1 Open-Source CRM </h2>

<p align="center"><a href="https://twenty.com">๐ŸŒ Website</a> ยท <a href="https://docs.twenty.com">๐Ÿ“š Documentation</a> ยท <a href="https://github.com/orgs/twentyhq/projects/1"><img src="./packages/twenty-website/public/images/readme/planner-icon.svg" width="12" height="12"/> Roadmap </a> ยท <a href="https://discord.gg/cx5n4Jzs57"><img src="./packages/twenty-website/public/images/readme/discord-icon.svg" width="12" height="12"/> Discord</a> ยท <a href="https://www.figma.com/file/xt8O9mFeLl46C5InWwoMrN/Twenty"><img src="./packages/twenty-website/public/images/readme/figma-icon.png" width="12" height="12"/> Figma</a></p>
<br />


<p align="center">
<a href="https://www.twenty.com">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/github-cover-dark.png" />
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/github-cover-light.png" />
<img src="./packages/twenty-website/public/images/readme/github-cover-light.png" alt="Cover" />
</picture>
</a>
</p>

<br />

Installation

See:
๐Ÿš€ Self-hosting
๐Ÿ–ฅ๏ธ Local Setup

Why Twenty

We built Twenty for three reasons:

CRMs are too expensive, and users are trapped. Companies use locked-in customer data to hike prices. It shouldn't be that way.

A fresh start is required to build a better experience. We can learn from past mistakes and craft a cohesive experience inspired by new UX patterns from tools like Notion, Airtable or Linear.

We believe in open-source and community. Hundreds of developers are already building Twenty together. Once we have plugin capabilities, a whole ecosystem will grow around it.

<br />

What You Can Do With Twenty

Please feel free to flag any specific needs you have by creating an issue.

Below are a few features we have implemented to date:

+ Personalize layouts with filters, sort, group by, kanban and table views
+ Customize your objects and fields
+ Create and manage permissions with custom roles
+ Automate workflow with triggers and actions
+ Emails, calendar events, files, and more


Personalize layouts with filters, sort, group by, kanban and table views

<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/views-dark.png" />
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/views-light.png" />
<img src="./packages/twenty-website/public/images/readme/views-light.png" alt="Companies Kanban Views" />
</picture>
</p>

Customize your objects and fields

<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/data-model-dark.png" />
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/data-model-light.png" />
<img src="./packages/twenty-website/public/images/readme/data-model-light.png" alt="Setting Custom Objects" />
</picture>
</p>

Create and manage permissions with custom roles

<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/permissions-dark.png" />
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/permissions-light.png" />
<img src="./packages/twenty-website/public/images/readme/permissions-light.png" alt="Permissions" />
</picture>
</p>

Automate workflow with triggers and actions

<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/workflows-dark.png" />
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/workflows-light.png" />
<img src="./packages/twenty-website/public/images/readme/workflows-light.png" alt="Workflows" />
</picture>
</p>

Emails, calendar events, files, and more

<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/plus-other-features-dark.png" />
<source media="(prefers-color-scheme: light)" srcset="https://raw.githubusercontent.com/twentyhq/twenty/refs/heads/main/packages/twenty-website/public/images/readme/plus-other-features-light.png" />
<img src="./packages/twenty-website/public/images/readme/plus-other-features-light.png" alt="Other Features" />
</picture>
</p>

<br />

Stack


- TypeScript
- Nx
- NestJS, with BullMQ, PostgreSQL, Redis
- React, with Jotai, Linaria and Lingui

Thanks

<p align="center">
<a href="https://www.chromatic.com/"><img src="./packages/twenty-website/public/images/readme/chromatic.png" height="30" alt="Chromatic" /></a>
<a href="https://greptile.com"><img src="./packages/twenty-website/public/images/readme/greptile.png" height="30" alt="Greptile" /></a>
<a href="https://sentry.io/"><img src="./packages/twenty-website/public/images/readme/sentry.png" height="30" alt="Sentry" /></a>
<a href="https://crowdin.com/"><img src="./packages/twenty-website/public/images/readme/crowdin.png" height="30" alt="Crowdin" /></a>
<a href="https://e2b.dev/"><img src="./packages/twenty-website/public/images/readme/e2b.svg" height="30" alt="E2B" /></a>
</p>

Thanks to these amazing services that we use and recommend for UI testing (Chromatic), code review (Greptile), catching bugs (Sentry) and translating (Crowdin).


Join the Community

- Star the repo
- Subscribe to releases (watch -> custom -> releases)
- Follow us on Twitter or LinkedIn
- Join our Discord
- Improve translations on Crowdin
- Contributions are, of course, most welcome!