<:strapimono:1044611225188831252>
This topic has been created from a Discord post (1238727359717441557) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord
<:strapimono:1044611225188831252>
This topic has been created from a Discord post (1238727359717441557) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord
Really interested on this one too!
I am actually, deploying strapi in a test environment where it doesn’t have a browser and internet so cant download browser either.
I guess you can just call the create
or regenerate
services.
const attributes = {
name: toBeModified,
description: toBeModified,
type: toBeModified,
permissions: toBeModified,
lifespan: toBeModified,
};
const apiToken = await strapi.service('admin::api-token').create(attributes);
Can confirm that the above works as expected
Please be sure to set it’s attributes to fit your needs. E.g. type of ‘full-access’, or a set lifespan.
can you give me example code, how can I use strapi object in cli
To use this in the CLI you’ll have to create a file in the root of your project. Let’s call this file ./cli.js
In the file you can programmatically start Strapi, upon which you can query it’s APIs. See this example code:
#!/usr/bin/env node
const fs = require('fs');
const strapi = require('@strapi/strapi'); // eslint-disable-line
const getStrapiApp = async () => {
process.env.CONFIG_SYNC_CLI = true;
try {
const tsUtils = require('@strapi/typescript-utils'); // eslint-disable-line
const appDir = process.cwd();
const isTSProject = await tsUtils.isUsingTypeScript(appDir);
const outDir = await tsUtils.resolveOutDir(appDir);
const alreadyCompiled = await fs.existsSync(outDir);
if (isTSProject && !alreadyCompiled) {
await tsUtils.compile(appDir, {
watch: false,
configOptions: { options: { incremental: true } },
});
}
const distDir = isTSProject ? outDir : appDir;
const app = await strapi({ appDir, distDir }).load();
return app;
} catch (e) {
// Fallback for pre Strapi 4.2.
const app = await strapi().load();
return app;
}
};
const generateApiToken = async () => {
const app = await getStrapiApp();
const attributes = {
name: 'test',
description: 'test',
type: 'read-only',
lifespan: null,
};
const apiToken = await app.service('admin::api-token').create(attributes);
console.log(apiToken);
process.exit(0);
};
generateApiToken();
If you now run node cli.js
it’ll log a newly generated token
Thank you so much <@344388867446669315>
You’re most welcome