I want to enable the manually created API by default

System Information
  • Strapi Version: 4.3.4
  • Operating System: debian 11
  • Database: SQLite
  • Node Version: v16.15.0
  • NPM Version: 8.5.5
  • Yarn Version: 1.22.18

Hello. Please help me.
Automatically create Strapi APIs with CI/CD.
Manually create files and folders for /src/api/**

If the API is created with the above flow, the API on the configuration screen at the following URL will be disabled by default.(The checkbox for Authority for Role is unchecked)

I would like to prevent this from happening, but can I start with the specified API enabled by editing a specific file?

Thanks for looking.


Welcome to the Strapi Community Forums :waffle: @yuichiromukaiyama
I would suggest here that the way you can do this, is to use bootstrap.js

From here you can query the permissions you have and set them. It’s a bit of setup but I did it a while ago at my old work.

What I did was the following.

  • Made an array of the plugins, api’s I wanted to enable etc by default
  • Queried permission plugin to get all the permissions,
  • Pushed the new permissions into the existing permissions

All this was done on bootstrap.js

1 Like

thank you very much!
I checked it out and tried it out right away.

But… Sorry. I think I need to take a few more hints.
I referred to the following article.

I created config/functions/bootstrap.js and created the file as linked.

However, when I restarted Strapi, this file was not loaded.

Is there any change in the way things are done since Strapi v4?
Do I need to use bootstrap() in src/index.js by any chance?
※ Ive tried that too, but its still not working.

thank you very much!
I checked it out and tried it out right away.

But… Sorry. I think I need to take a few more hints.
I referred to the following article.

I created config/functions/bootstrap.js and created the file as linked.

However, when I restarted Strapi, this file was not loaded.

Is there any change in the way things are done since Strapi v4?
Do I need to use bootstrap() in src/index.js by any chance?
※ Ive tried that too, but its still not working.

i could to solved.
I made it as follows.

/config/functions/bootstrap.js

"use strict";

async function isFirstRun() {
  const pluginStore = strapi.store({
    environment: strapi.config.environment,
    type: "type",
    name: "setup",
  });
  const initHasRun = await pluginStore.get({ key: "initHasRun" });
  await pluginStore.set({ key: "initHasRun", value: true });
  return !initHasRun;
}

async function setPublicPermissions(newPermissions) {
  const publicRole = await strapi.query("plugin::users-permissions.role").findOne({
    where: {
      type: "public",
    },
  });

  // Create the new permissions and link them to the public role
  const allPermissionsToCreate = [];
  Object.keys(newPermissions).map((controller) => {
    const actions = newPermissions[controller];
    const permissionsToCreate = actions.map((action) => {
      return strapi.query("plugin::users-permissions.permission").create({
        data: {
          action: `api::${controller}.${controller}.${action}`,
          role: publicRole.id,
        },
      });
    });
    allPermissionsToCreate.push(...permissionsToCreate);
  });
  await Promise.all(allPermissionsToCreate);
}

async function importSeedData() {
  // Allow read of application content types
  await setPublicPermissions({
    {{ api name }}: [ {{ permission name }} ],
    // example --> article: [ "find" ],
  });
}
module.exports = async () => {
  const shouldImportSeedData = await isFirstRun();

  if (shouldImportSeedData) {
    try {
      console.log("Setting up the template...");
      await importSeedData();
      console.log("Ready to go");
    } catch (error) {
      console.log("Could not import seed data");
      console.error(error);
    }
  }
};

src/index.js

"use strict";
const bootstrap = require("../config/functions/bootstrap");
module.exports = {
  register() {},
  bootstrap: bootstrap,
};

Glad you got it sorted, yes it’s a big structural difference from V3 and V4.

1 Like