Skip to main content

Strapi

info

Current state: Stable

The Strapi class is the main object used in Strapi projects. An instance of Strapi class is available as a global in any Strapi project: global.strapi.

Class: Strapi

new Strapi(opts)

  • opts: <Object> Options that can be used on Strapi startup
    • autoReload: <Boolean> Default: true
      • If false, deactivate auto reload
      • If you modify any file in your Strapi project, it reloads your nodejs app
      • If any content-type is changed, it will reload the nodejs app
    • serveAdminPanel: <Boolean> Default: true
      • Should the admin panel be loaded and serve as a web client
      • The admin panel build will not be delivered if false
    • appDir: <String> Default: process.cwd()
      • The directory relative or absolute path where Strapi will write every file (schemas, generated APIs, controllers or services)
    • distDir: <String> Default: appDir value
      • The directory relative or absolute path where Strapi will read configurations, schemas and any compiled code

Instances of the Strapi class can be created using the new keyword.

const strapiInstance = new Strapi();

strapi.container

The container provides a simple and efficient way to register and manage resources, making it easy to access and use them throughout the application. By registering a registry with the container, it can be easily retrieved by other parts of the application, making it a powerful tool for organizing and reusing code across the entire codebase.

See Container.

strapi.dirs

  • <Object>

Stored paths of file system.

  • dirs.dist: <StrapiPathObject>
    • Build folder
  • dirs.app: <StrapiPathObject>
    • Sources folder
  • dirs.static: <Object> Define path to directories involving web client display
    • public: <String> Path to the folder to serve publicly (like files, images, etc..)

StrapiPathObject

  • <Object>

A set of paths to specific Strapi project parts.

  • root: <String> Root path
  • src: <String> Sources route path to project files
  • api: <String> Path to the folder containing project developers' API files (content-types, controllers, services, routes, etc..)
  • components: <String> Path to the folder containing project developers' components
  • policies: <String> Path to the folder where the Strapi project developers' policies are stored
    • A set of functions that check the state of the data and prevent the access to the API accordingly
  • middlewares: <String> Path to the folder where the Strapi project developers' middlewares are stored
    • A set of function that wrap around routes and requests
  • config: <String> Path to the folder containing project developers' config files

strapi.isLoaded

  • <Boolean>
    • true: Everything (all register and bootstrap functions available in your strapi project) has been loaded
    • false: There is something loading

Note: register functions are called before the bootstrap functions.

strapi.reload()

Reload the app.

This function defines itself at the construction of the Strapi class.

strapi.server

Strapi server object.

strapi.fs

Wrapper around FS NodeJS module.

strapi.eventHub

The strapi.eventHub object is used to manipulate events within a Strapi project. It is an instance of the built-in EventEmitter class from Node.js, which provides a simple way to emit and listen for events.

The strapi.eventHub object is created using the createEventHub() function in the EventHub module of the Strapi core. This function returns a new instance of the EventHub class, which extends the EventEmitter class and adds some additional functionality specific to Strapi.

Examples:

// Listen for a 'user.updated' event and log the data
strapi.eventHub.on('user.updated', (data) => {
console.log(`User ${data.id} has been updated`);
});

// Emit a 'user.created' event with some data
strapi.eventHub.emit('user.created', { username: 'johndoe', email: 'johndoe@example.com' });

In this example, we are emitting a user.created event with some data attached to it, and then listening for a user.updated event and logging the data. These events can be used to trigger actions within the Strapi application or to communicate with external systems.

For more information on how to use the EventEmitter class and its methods, see the Node.js documentation.

strapi.startupLogger

Object containing predefined logger functions. Used for Strapi startup. (do not use as a logger elsewhere)

strapi.log

A logger provided by Strapi that uses the Winston logging library. It is the result of calling the winston.createLogger() function with the configuration defined by the user of the Strapi application.

The logger provides various methods for logging messages at different levels of severity, including error, warn, info, verbose, debug, and silly. The logging level can be set via the configuration to control which messages are logged.

Examples

// Log an error message
strapi.log.error('Failed to start server', { error: err });

// Log a warning message
strapi.log.warn('Server is running in development mode');

// Log an informational message
strapi.log.info(`Server started on port ${PORT}`);

// Log a verbose message
strapi.log.verbose('Application state', { user: currentUser });

// Log a debug message
strapi.log.debug('API request received', { method: req.method, path: req.path });

// Log a silly message
strapi.log.silly('Entered loop', { count: i });

In these examples, we are logging messages at different levels of severity, including error, warn, info, verbose, debug, and silly. We are also passing in metadata as an object in the second parameter of each logging method.

The messages logged by strapi.log will be output according to the logging configuration set by the user of the Strapi application. This configuration determines which messages are logged and where they are logged (e.g. console, file, etc.).

strapi.cron

Module to schedule cron jobs for Strapi project. It is an instance of a custom Cron object.

strapi.telemetry

The strapi.telemetry property provides access to the telemetry service instance. This service collects anonymous usage data about your Strapi application to help the Strapi team improve the product.

By default, the telemetry service is enabled, but you can disable it by setting the telemetryDisabled property to true in your application's package.json file, or by setting the STRAPI_TELEMETRY_DISABLED environment variable to true. You can also disable telemetry programmatically by setting the isDisabled property of the strapi.telemetry instance to true.

strapi.requestContext

  • <Object> Context Storage
    • run(store, cb): <Function>
      • store: <Any> Value that should be retrieved
      • cb: <Function> Callback
    • get() <Function>

The request context stores the ctx object from KoaJS on each request. This allows users to have access to the context from anywhere through the Strapi instance.

strapi.customFields

  • <Object>
    • register(customField): <Function> Register a new custom field

This property is a shortcut to strapi.container.get('custom-fields').add(customField).

Examples

  strapi.customFields.register({
name: 'color',
plugin: 'color-picker',
type: 'string',
});

strapi.config

  • <Object>

Shortcut to strapi.container.get('config').

See the config container.

strapi.services

  • <Object[]>

Shortcut to strapi.container.get('services').getAll().

See the services' container.

strapi.service(uid)

  • uid: <String>

Shortcut to strapi.container.get('services').get(uid).

See the services' container.

strapi.controllers

  • <Object[]>

Shortcut to strapi.container.get('controllers').getAll().

See the controllers' container.

strapi.controller(uid)

  • uid: <String>

Shortcut to strapi.container.get('controllers').get(uid).

See the controllers' container.

strapi.contentTypes

  • <Object[]>

Shortcut to strapi.container.get('content-types').getAll().

See the content-types' container.

strapi.contentType(name)

  • name: <String>

Shortcut to strapi.container.get('content-types').get(name).

See the content-types' container.

strapi.policies

  • <Object[]>

Shortcut to strapi.container.get('policies').getAll().

See the policies' container.

strapi.policy(name)

  • name: <String>

Shortcut to strapi.container.get('policies').get(name).

See the policies' container.

strapi.middlewares

  • <Object[]>

Shortcut to strapi.container.get('middlewares').getAll().

See the middlewares container.

strapi.middleware(name)

  • name: <String>

Shortcut to strapi.container.get('middlewares').get(name).

See the middlewares container.

strapi.plugins

  • <Object[]>

Shortcut to strapi.container.get('plugins').getAll().

See the plugins' container.

strapi.plugin(name)

  • name: <String>

Shortcut to strapi.container.get('plugins').get(name).

See the plugins' container.

strapi.hooks

  • <Object[]>

Shortcut to strapi.container.get('hooks').getAll().

See the hooks' container.

strapi.hook(name)

  • name: <String>

Shortcut to strapi.container.get('hooks').get(name).

See the hooks' container.

strapi.api

  • <Object[]>

Shortcut to strapi.container.get('apis').getAll().

See the apis container.

strapi.auth

  • <Object>

Shortcut to strapi.container.get('auth').

See the auth' container.

strapi.contentAPI

  • <Object>

Shortcut to strapi.container.get('content-api').

See the content-api container.

strapi.sanitizers

  • <Object>

Shortcut to strapi.container.get('sanitizers').

See the sanitizers' container.

strapi.validators

  • <Object>

Shortcut to strapi.container.get('validators').

See the validators' container.

strapi.start()

  • Returns: Promise
info

TODO

strapi.destroy()

  • Returns: Promise
info

TODO

strapi.sendStartupTelemetry()

info

TODO

strapi.openAdmin({ isInitialized })

  • Returns: Promise
info

TODO

strapi.postListen()

  • Returns: Promise
info

TODO

strapi.listen()

  • Returns: Promise
info

TODO

strapi.stopWithError()

info

TODO

strapi.stop(exitCode)

info

TODO

strapi.loadAdmin()

  • Returns: Promise
info

TODO

strapi.loadPlugins()

  • Returns: Promise
info

TODO

strapi.loadPolicies()

  • Returns: Promise
info

TODO

strapi.loadAPIs()

  • Returns: Promise
info

TODO

strapi.loadComponents()

  • Returns: Promise
info

TODO

strapi.loadMiddlewares()

  • Returns: Promise
info

TODO

strapi.loadApp()

  • Returns: Promise
info

TODO

strapi.loadSanitizers()

  • Returns: Promise
info

TODO

strapi.registerInternalHooks()

info

TODO

strapi.register()

  • Returns: Promise
info

TODO

strapi.bootstrap()

  • Returns: Promise
info

TODO

strapi.load()

  • Returns: Promise
info

TODO

strapi.startWebhooks()

  • Returns: Promise
info

TODO

strapi.reload()

info

TODO

strapi.runLifecyclesFunctions()

  • Returns: Promise
info

TODO

strapi.getModel(uid)

  • uid: <String>
info

TODO

strapi.query(uid)

  • uid: <String>
info

TODO

Strapi containers

The strapi containers are accessible via strapi.container.get('name-of-the-container').

config

  • <Object>
    • get(path, defaultValue): <Function>
      • path: <String>
      • defaultValue: <Any>
      • Returns: <Any> - The value located at path or, if undefined, defaultValue.
    • set(path, value): <Function>
      • path: <String> - Where the value should be stored
      • value: <Any>
    • has(path): <Function>
      • path: <String>
      • Returns: <Boolean> - Does the path match a value stored in the config container.
    • launchedAt: <Number> Default: Date.now() Date in milliseconds when the server has started
    • serveAdminPanel: <Boolean> Default: true See Strapi constructor options
    • autoReload: <Boolean> Default: false See Strapi constructor options
    • environment: <String> - process.env.NODE_ENV
    • uuid: <String> - string extracted from package.json located in strapi.uuid
    • packageJsonStrapi: <Object> - object extracted from package.json located in strapi (except uuid)
    • info: <Object>
      • everything stored in the package.json
      • strapi: <String> - Current version of Strapi

Every file stored under the config folder will be injected in this config container object.

services

info

TODO

controllers

info

TODO

content-types

info

TODO

policies

info

TODO

plugins

info

TODO

hooks

info

TODO

apis

info

TODO

auth

info

TODO

content-api

info

TODO

sanitizers

info

TODO

validators

info

TODO