# Services
Services are a set of reusable functions. They are particularly useful to respect the DRY (donβt repeat yourself) programming concept and to simplify controllers logic.
# Implementation
A new service can be implemented:
- with the interactive CLI command
strapi generate
- or manually by creating a JavaScript file in the appropriate folder (see project structure):
./src/api/[api-name]/services/
for API services- or
./src/plugins/[plugin-name]/services/
for plugin services.
To manually create a service, export a factory function that returns the service implementation (i.e. an object with methods). This factory function receives the strapi
instance:
/**
* @param {{ strapi: import('@strapi/strapi').Strapi }} opts
*/
module.exports = ({ strapi }) => {
return {
archiveArticles(ids) {
// do some logic here
},
};
};
π€ Entity Service API
To get started creating your own services, see Strapi's built-in functions in the Entity Service API documentation.
Example of an email service
The goal of a service is to store reusable functions. An email
service could be useful to send emails from different functions in our codebase:
// path: ./src/api/email/services/email.js
const nodemailer = require('nodemailer'); // Requires nodemailer to be installed (npm install nodemailer)
// Create reusable transporter object using SMTP transport.
const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'user@gmail.com',
pass: 'password',
},
});
module.exports = {
send: (from, to, subject, text) => {
// Setup e-mail data.
const options = {
from,
to,
subject,
text,
};
// Return a promise of the function that sends the email.
return transporter.sendMail(options);
},
};
The service is now available through the strapi.services
global variable. It can be used in another part of the codebase, like in the following controller:
// path: ./src/api/user/controllers/user.js
module.exports = {
// GET /hello
signup: async ctx => {
// Store the new user in database.
const user = await User.create(ctx.query);
// Send an email to validate his subscriptions.
strapi.service('api::email.send')('welcome@mysite.com', user.email, 'Welcome', '...');
// Send response to the server.
ctx.send({
ok: true,
});
},
};
βοΈ NOTE
When a new content-type is created, Strapi builds a generic service with placeholder code, ready to be customized.
# Usage
Once a service is created, it's accessible from controllers or from other services:
// access an API service
strapi.service('api::apiName.serviceName');
// access a plugin service
strapi.service('plugin::pluginName.serviceName');
π‘ TIP
To list all the available services, run yarn strapi services:list
.
β Requests & Responses Models β