docs/docs/api/Strapi.mdx
import Type from '@site/docs/api/components/type';
:::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.
new Strapi(opts)opts: <Type>Object</Type> Options that can be used on Strapi startup
autoReload: <Type>Boolean</Type> Default: true
serveAdminPanel: <Type>Boolean</Type> Default: true
appDir: <Type>String</Type> Default: process.cwd()
distDir: <Type>String</Type> Default: appDir value
Instances of the Strapi class can be created using the new keyword.
Strapi extends the Container class.
const strapiInstance = new Strapi();
strapi.dirsStored paths of file system.
dirs.dist: <Type>StrapiPathObject</Type>
dirs.app: <Type>StrapiPathObject</Type>
dirs.static: <Type>Object</Type> Define path to directories involving web client display
public: <Type>String</Type> Path to the folder to serve publicly (like files, images, etc..)A set of paths to specific Strapi project parts.
root: <Type>String</Type> Root pathsrc: <Type>String</Type> Sources route path to project filesapi: <Type>String</Type> Path to the folder containing project developers' API files (content-types, controllers, services, routes, etc..)components: <Type>String</Type> Path to the folder containing project developers' componentspolicies: <Type>String</Type> Path to the folder where the Strapi project developers' policies are stored
middlewares: <Type>String</Type> Path to the folder where the Strapi project developers' middlewares are stored
config: <Type>String</Type> Path to the folder containing project developers' config filesstrapi.isLoaded<Type>Boolean</Type>
true: Everything (all register and bootstrap functions available in your strapi project) has been loadedfalse: There is something loadingNote: 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.serverStrapi server object.
strapi.fsWrapper around FS NodeJS module.
strapi.eventHubThe 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.
// 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: '[email protected]' });
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.startupLoggerObject containing predefined logger functions. Used for Strapi startup. (do not use as a logger elsewhere)
strapi.logA 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.
// 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.cronModule to schedule cron jobs for Strapi project. It is an instance of a custom Cron object.
strapi.telemetryThe 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<Type>Object</Type> Context Storage
run(store, cb): <Type>Function</Type>
store: <Type>Any</Type> Value that should be retrievedcb: <Type>Function</Type> Callbackget() <Type>Function</Type>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<Type>Object</Type>
register(customField): <Type>Function</Type> Register a new custom fieldThis property is a shortcut to strapi.get('custom-fields').add(customField).
strapi.customFields.register({
name: 'color',
plugin: 'color-picker',
type: 'string',
});
strapi.configShortcut to strapi.get('config').
See the config container.
strapi.servicesShortcut to strapi.get('services').getAll().
See the services' container.
strapi.service(uid)uid: <Type>String</Type>Shortcut to strapi.get('services').get(uid).
See the services' container.
strapi.controllersShortcut to strapi.get('controllers').getAll().
See the controllers' container.
strapi.controller(uid)uid: <Type>String</Type>Shortcut to strapi.get('controllers').get(uid).
See the controllers' container.
strapi.contentTypesShortcut to strapi.get('content-types').getAll().
See the content-types' container.
strapi.contentType(name)name: <Type>String</Type>Shortcut to strapi.get('content-types').get(name).
See the content-types' container.
strapi.policiesShortcut to strapi.get('policies').getAll().
See the policies' container.
strapi.policy(name)name: <Type>String</Type>Shortcut to strapi.get('policies').get(name).
See the policies' container.
strapi.middlewaresShortcut to strapi.get('middlewares').getAll().
See the middlewares container.
strapi.middleware(name)name: <Type>String</Type>Shortcut to strapi.get('middlewares').get(name).
See the middlewares container.
strapi.pluginsShortcut to strapi.get('plugins').getAll().
See the plugins' container.
strapi.plugin(name)name: <Type>String</Type>Shortcut to strapi.get('plugins').get(name).
See the plugins' container.
strapi.hooksShortcut to strapi.get('hooks').getAll().
See the hooks' container.
strapi.hook(name)name: <Type>String</Type>Shortcut to strapi.get('hooks').get(name).
See the hooks' container.
strapi.apisShortcut to strapi.get('apis').getAll().
See the apis container.
strapi.api(name)name: <Type>String</Type>Shortcut to strapi.get('apis').get(name).
See the apis container.
strapi.authShortcut to strapi.get('auth').
See the auth' container.
strapi.contentAPIShortcut to strapi.get('content-api').
See the content-api container.
strapi.sanitizersShortcut to strapi.get('sanitizers').
See the sanitizers' container.
strapi.validatorsShortcut to strapi.get('validators').
See the validators' container.
strapi.start():::info TODO :::
strapi.destroy():::info TODO :::
strapi.sendStartupTelemetry():::info TODO :::
strapi.openAdmin({ isInitialized }):::info TODO :::
strapi.postListen():::info TODO :::
strapi.listen():::info TODO :::
strapi.stopWithError():::info TODO :::
strapi.stop(exitCode):::info TODO :::
strapi.loadAdmin():::info TODO :::
strapi.loadPlugins():::info TODO :::
strapi.loadPolicies():::info TODO :::
strapi.loadAPIs():::info TODO :::
strapi.loadComponents():::info TODO :::
strapi.loadMiddlewares():::info TODO :::
strapi.loadApp():::info TODO :::
strapi.loadSanitizers():::info TODO :::
strapi.registerInternalHooks():::info TODO :::
strapi.register():::info TODO :::
strapi.bootstrap():::info TODO :::
strapi.load():::info TODO :::
strapi.reload():::info TODO :::
strapi.runLifecyclesFunctions():::info TODO :::
strapi.getModel(uid)uid: <Type>String</Type>:::info TODO :::
strapi.db.query(uid)uid: <Type>String</Type>:::info TODO :::
The strapi containers are accessible via strapi.get('name-of-the-container').
config<Type>Object</Type>
get(path, defaultValue): <Type>Function</Type>
path: <Type>String</Type>defaultValue: <Type>Any</Type>path or, if undefined, defaultValue.set(path, value): <Type>Function</Type>
path: <Type>String</Type> - Where the value should be storedvalue: <Type>Any</Type>has(path): <Type>Function</Type>
path: <Type>String</Type>path match a value stored in the config container.launchedAt: <Type>Number</Type> Default: Date.now()
Date in milliseconds when the server has startedserveAdminPanel: <Type>Boolean</Type> Default: true
See Strapi constructor optionsautoReload: <Type>Boolean</Type> Default: false
See Strapi constructor optionsenvironment: <Type>String</Type> - process.env.NODE_ENVuuid: <Type>String</Type> - string extracted from package.json located in strapi.uuidpackageJsonStrapi: <Type>Object</Type> - object extracted from package.json located in strapi (except uuid)info: <Type>Object</Type>
package.jsonstrapi: <Type>String</Type> - Current version of StrapiEvery 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 :::
middlewares:::info TODO :::
plugins:::info TODO :::
hooks:::info TODO :::
apis:::info TODO :::
auth:::info TODO :::
content-api:::info TODO :::
sanitizers:::info TODO :::
validators:::info TODO :::