How to understand a Strapi codebase
Strapi is a headless CMS: content types are defined as JSON schemas and Strapi generates the REST and GraphQL APIs. Custom behaviour goes into controllers, services, routes, policies and middlewares under `src/api/`, plus bootstrap code in `src/index.ts`.
How to tell it's Strapi
@strapi/strapiinpackage.jsonsrc/api/<name>/content-types/<name>/schema.json- A
config/folder withdatabase,server,pluginsandmiddlewaresfiles
Where things usually live
- src/api/<name>/content-types/
- Content type schemas: fields, relations, options.
- src/api/<name>/controllers/
- Request handlers, usually extending the core controller.
- src/api/<name>/services/
- Business logic used by controllers.
- src/api/<name>/routes/
- Core router config plus custom routes.
- src/index.ts
registerandbootstraphooks that run at startup.- config/
- Database, server, admin, plugin and middleware configuration.
- src/components/
- Reusable field groups shared across content types.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
schema.jsonfiles, to learn the data model- Controllers and services that are customised rather than default
src/index.tsbootstrap, for anything that runs at startup
How a request moves through the code
- A request reaches the route defined for the content type.
- Global and route middlewares run, then policies check access.
- The controller runs, calling services and the Document Service API.
- Lifecycle hooks such as
beforeCreatecan change data before it is saved. - The response is sanitised according to the user's permissions.
Questions to ask about the repository
- Which content types have custom controllers or services, and what do they change?
- Where are permissions configured for public and authenticated users?
- Which lifecycle hooks exist, and what side effects do they have?
What trips people up
- Much of the API is generated from schemas, so a route may exist with no code file behind it.
- Permissions are often set in the admin panel and stored in the database, not in code.
- Strapi v4 and v5 APIs differ (Entity Service versus Document Service); check the version.