You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Strapi [plugin](/dev-docs/plugins) can interact with both the [back end](/dev-docs/api/plugins/server-api)or the front end of the Strapi app. The Admin Panel API is about the front end part, i.e. it allows a plugin to customize Strapi's [admin panel](/user-docs/intro).
9
+
A Strapi [plugin](/dev-docs/plugins) can interact with both the [back end](/dev-docs/api/plugins/server-api)and the front end of a Strapi application. The Admin Panel API is about the front end part, i.e. it allows a plugin to customize Strapi's [admin panel](/user-docs/intro).
9
10
10
11
The admin panel is a [React](https://reactjs.org/) application that can embed other React applications. These other React applications are the admin parts of each Strapi plugin.
11
12
12
-
To create a plugin that interacts with the Admin Panel API:
13
-
14
-
1. Create an [entry file](#entry-file).
15
-
2. Within this file, declare and export a plugin interface that uses the [available actions](#available-actions).
16
-
3. Require this plugin interface in a `strapi-admin.js` file at the root of the plugin package folder:
13
+
:::prerequisites
14
+
You have [created a Strapi plugin](/dev-docs/plugins/development/create-a-plugin).
15
+
:::
17
16
18
-
```js title="[plugin-name]/strapi-admin.js"
17
+
The Admin Panel API includes:
19
18
20
-
'use strict';
19
+
- an [entry file](#entry-file) which exports the required interface,
20
+
-[lifecycle functions](#lifecycle-functions) and the `registerTrad()`[async function](#async-function),
21
+
- and several [specific APIs](#available-actions) for your plugin to interact with the admin panel.
21
22
22
-
module.exports=require('./admin/src').default;
23
-
```
23
+
:::note
24
+
The whole code for the admin panel part of your plugin could live in the `/strapi-admin.js|ts` or `/admin/src/index.js|ts` file. However, it's recommended to split the code into different folders, just like the [structure](/dev-docs/plugins/development/plugin-structure) created by the `strapi generate plugin` CLI generator command.
Copy file name to clipboardExpand all lines: docusaurus/docs/dev-docs/api/plugins/server-api.md
+58-33Lines changed: 58 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,18 +3,29 @@ title: Server API for plugins
3
3
sidebar_label: Server API
4
4
displayed_sidebar: devDocsSidebar
5
5
description: Strapi's Server API for plugins allows a Strapi plugin to customize the back end part (i.e. the server) of your application.
6
-
sidebarDepth: 3
7
-
8
6
---
9
7
10
8
# Server API for plugins
11
9
12
-
A Strapi [plugin](/dev-docs/plugins) can interact with the backend or the [frontend](/dev-docs/api/plugins/admin-panel-api) of the Strapi application. The Server API is about the backend part.
10
+
A Strapi [plugin](/dev-docs/plugins) can interact with both the back end and the [front end](/dev-docs/api/plugins/admin-panel-api) of a Strapi application. The Server API is about the back-end part, i.e. how the plugin interacts with the server part of a Strapi application.
11
+
12
+
:::prerequisites
13
+
You have [created a Strapi plugin](/dev-docs/plugins/development/create-a-plugin).
14
+
:::
15
+
16
+
The Server API includes:
17
+
18
+
- an [entry file](#entry-file) which export the required interface,
19
+
-[lifecycle functions](#lifecycle-functions),
20
+
- a [configuration](#configuration) API,
21
+
- the ability to add [cron](#cron) jobs,
22
+
- and the ability to [customize all elements of the back-end server](#backend-customization).
13
23
14
-
Creating and using a plugin interacting with the Server API consists of 2 steps:
24
+
Once you have declared and exported the plugin interface, you will be able to [use the plugin interface](#usage).
15
25
16
-
1. Declare and export the plugin interface within the [`strapi-server.js` entry file](#entry-file)
17
-
2.[Use the exported interface](#usage)
26
+
:::note
27
+
The whole code for the server part of your plugin could live in the `/strapi-server.js|ts` or `/server/index.js|ts` file. However, it's recommended to split the code into different folders, just like the [structure](/dev-docs/plugins/development/plugin-structure) created by the `strapi generate plugin` CLI generator command.
28
+
:::
18
29
19
30
## Entry file
20
31
@@ -81,7 +92,7 @@ module.exports = () => ({
81
92
82
93
## Configuration
83
94
84
-
`config` stores the default plugin configuration.
95
+
`config` stores the default plugin configuration. It loads and validates the configuration inputted from the user within the [`./config/plugins.js` configuration file](/dev-docs/configurations/plugins).
85
96
86
97
**Type**: `Object`
87
98
@@ -113,6 +124,10 @@ Once defined, the configuration can be accessed:
113
124
- with `strapi.plugin('plugin-name').config('some-key')` for a specific configuration property,
114
125
- or with `strapi.config.get('plugin.plugin-name')` for the whole configuration object.
115
126
127
+
:::tip
128
+
Run `yarn strapi console` or `npm run strapi console` to access the strapi object in a live console.
129
+
:::
130
+
116
131
## Cron
117
132
118
133
The `cron` object allows you to add cron jobs to the Strapi instance.
@@ -155,6 +170,12 @@ strapi.cron.jobs
155
170
156
171
## Backend customization
157
172
173
+
All elements of the back-end server of Strapi can be customized through a plugin using the Server API.
174
+
175
+
:::prerequisites
176
+
To better understand this section, ensure you have read through the [back-end customization](/dev-docs/backend-customization) documentation of a Strapi application.
177
+
:::
178
+
158
179
### Content-types
159
180
160
181
An object with the [content-types](/dev-docs/backend-customization/models) the plugin provides.
@@ -461,50 +482,54 @@ An object with the [middlewares](/dev-docs/configurations/middlewares) the plugi
Once a plugin is exported and loaded into Strapi, its features are accessible in the code through getters. The Strapi instance (`strapi`) exposes top-level getters and global getters.
529
+
Once a plugin is exported and loaded into Strapi, its features are accessible in the code through getters. The Strapi instance (`strapi`) exposes both top-level getters and global getters:
506
530
507
-
While top-level getters imply chaining functions, global getters are syntactic sugar that allows direct access using a feature's uid:
- global getters are syntactic sugar that allows direct access using a feature's uid<br/>(e.g., `strapi.controller('plugin::plugin-name.controller-name')`).
508
533
509
534
```js
510
535
// Access an API or a plugin controller using a top-level getter
C{"bootstrap()"} -- The Strapi back-end server starts. --> D
19
+
D(Request)
20
+
D
21
+
click B "#register"
22
+
click C "#bootstrap"
23
+
click D "/dev-docs/backend-customization/requests-responses"
24
+
```
25
+
14
26
## Synchronous function
15
27
16
28
<TabsgroupId="js-ts">
@@ -147,6 +159,8 @@ It can be used to:
147
159
- load some [environment variables](/dev-docs/configurations/environment)
148
160
- register a [custom field](/dev-docs/custom-fields) that would be used only by the current Strapi application.
149
161
162
+
`register()` is the very first thing that happens when a Strapi application is starting. This happens _before_ any setup process and you don't have any access to database, routes, policies, or any other backend server elements within the `register()` function.
163
+
150
164
## Bootstrap
151
165
152
166
The `bootstrap` lifecycle function, found in `./src/index.js` (or in `./src/index.ts`), is called at every server start.
@@ -157,6 +171,12 @@ It can be used to:
157
171
- fill the database with some necessary data
158
172
- declare custom conditions for the [Role-Based Access Control (RBAC)](/dev-docs/configurations/rbac) feature
159
173
174
+
The `bootstrapi()` function is run _before_ the back-end server starts but _after_ the Strapi application has setup, so you have access to anything from the `strapi` object.
175
+
176
+
:::tip
177
+
You can run `yarn strapi console` (or `npm run strapi console`) in the terminal and interact with the `strapi` object.
178
+
:::
179
+
160
180
## Destroy
161
181
162
182
The `destroy` function, found in `./src/index.js` (or in `./src/index.ts`), is an asynchronous function that runs before the application gets shut down.
This section is about developing Strapi plugins to use them as local plugins or to submit them to the Marketplace. Not what you're looking for? Read the [plugins introduction](/dev-docs/plugins) and find your use case and recommended section to read from there.
13
13
:::
14
14
15
-
Strapi allows the development of plugins that work exactly like the built-in plugins or 3rd-party plugins available from the Marketplace. Once created, your plugin can be:
15
+
Strapi allows the development of plugins that work exactly like the built-in plugins or 3rd-party plugins available from the [Marketplace](https://market.strapi.io). Once created, your plugin can be:
16
16
17
17
- used as a local plugin, working only with a specific Strapi project,
18
-
- or submitted to the [Marketplace](https://market.strapi.io) to be shared with the community.
18
+
- or [submitted to the Marketplace](https://market.strapi.io/submit-plugin) to be shared with the community.
19
19
20
-
The first step to developing a Strapi plugin is to create it using the CLI-based generator. Then you'll be able to leverage the [plugin APIs](#plugin-apis) to add features to your plugin.
20
+
👉 To start developing a Strapi plugin:
21
21
22
-
## Plugin creation
23
-
24
-
Strapi provides a [command line interface (CLI)](/dev-docs/cli) for creating plugins. To create a plugin:
25
-
26
-
1. Navigate to the root of a Strapi project.
27
-
2. Run `yarn strapi generate` or `npm run strapi generate` in a terminal window to start the interactive CLI.
28
-
3. Choose "plugin" from the list, press Enter, and give the plugin a name in kebab-case (e.g. `my-plugin`)
29
-
4. Choose either `JavaScript` or `TypeScript` for the plugin language.
30
-
5. Create a plugins configuration file if one does not already exist: `./config/plugins.js` or `./config/plugins.ts` for TypeScript projects.
31
-
6. Enable the plugin by adding it to the [plugins configurations](/dev-docs/configurations/plugins) file:
32
-
33
-
<Tabs>
34
-
<TabItemvalue="js"label="JavaScript">
35
-
36
-
```js title="./config/plugins.js"
37
-
module.exports= {
38
-
// ...
39
-
"my-plugin": {
40
-
enabled:true,
41
-
resolve:"./src/plugins/my-plugin", // path to plugin folder
42
-
},
43
-
// ...
44
-
};
45
-
```
46
-
47
-
</TabItem>
48
-
49
-
<TabItemvalue="ts"label="TypeScript">
50
-
51
-
```js title=./config/plugins.ts
52
-
exportdefault {
53
-
// ...
54
-
"my-plugin": {
55
-
enabled:true,
56
-
resolve:"./src/plugins/my-plugin", // path to plugin folder
57
-
},
58
-
// ...
59
-
};
60
-
```
61
-
62
-
</TabItem>
63
-
</Tabs>
64
-
65
-
7. Run `npm install` or `yarn` in the newly-created plugin directory.
66
-
8. (_TypeScript-specific_) Run `yarn build` or `npm run build` in the plugin directory. This step transpiles the TypeScript files and outputs the JavaScript files to a `dist` directory that is unique to the plugin.
67
-
9. Run `yarn build` or `npm run build` at the project root.
68
-
10. Run `yarn develop` or `npm run develop` at the project root.
69
-
70
-
Plugins created using the preceding directions are located in the `plugins` directory of the application (see [project structure](/dev-docs/project-structure)).
71
-
72
-
:::note
73
-
During plugin development it is helpful to use the `--watch-admin` flag to toggle hot reloading of the admin panel. See the [Admin panel customization](/dev-docs/admin-panel-customization) documentation for more details. (TypeScript specific) While developing your plugin, you can run `yarn develop --watch-admin` or `npm run develop -- --watch-admin` in the plugin directory to watch the changes to the TypeScript server files. From 4.15.1 this is no longer required.
74
-
:::
22
+
1.[Create a plugin](/dev-docs/plugins/development/create-a-plugin) using the CLI-based generator.
23
+
2. Learn more about the [structure of a plugin](/dev-docs/plugins/development/plugin-structure).
24
+
3. Get an overview of the [plugin APIs](#plugin-apis) to add features to your plugin.
25
+
4. Read some [guides](#guides) based on your use case(s).
75
26
76
27
## Plugin APIs
77
28
78
29
Strapi provides the following programmatic APIs for plugins to hook into some of Strapi's features:
79
30
80
31
<CustomDocCardsWrapper>
81
-
<CustomDocCardemoji=""title="Server API"description="Use the Server API to have your plugin interact with the backend server of Strapi."link="/dev-docs/api/plugins/server-api" />
82
32
<CustomDocCardemoji=""title="Admin Panel API"description="Use the Admin Panel API to have your plugin interact with the admin panel of Strapi."link="/dev-docs/api/plugins/admin-panel-api" />
33
+
<CustomDocCardemoji=""title="Server API"description="Use the Server API to have your plugin interact with the backend server of Strapi."link="/dev-docs/api/plugins/server-api" />
83
34
</CustomDocCardsWrapper>
84
35
85
36
:::strapi Custom fields plugins
86
37
Plugins can also be used to add [custom fields](/dev-docs/custom-fields) to Strapi.
87
38
:::
39
+
40
+
## Guides
41
+
42
+
<CustomDocCardsmallemoji="💁"title="How to store and access data from a Strapi plugin"description=""link="/dev-docs/plugins/guides/store-and-access-data" />
43
+
<CustomDocCardsmallemoji="💁"title="How to pass data from the backend server to the admin panel with a plugin"description=""link="/dev-docs/plugins/guides/pass-data-from-server-to-admin" />
44
+
45
+
<br />
46
+
47
+
:::strapi Additional resources
48
+
The Strapi blog features a [tutorial series](https://strapi.io/blog/how-to-create-a-strapi-v4-plugin-server-customization-4-6) about creating a Strapi v4 'Todo' plugin. The [contributors documentation](https://contributor.strapi.io/) can also include additional information useful while developing a Strapi plugin.
0 commit comments