Strapi provides a set of powerful APIs to let the developers create custom API end-points. In this article, we will be creating a custom API endpoint from scratch.
Not sure if this is the “proper” way, but my solution for this was to send the strapi jwt with the API request and then validating it on the server side in a custom policy.
for limiting the route to authenticated users, we don’t really need to write a custom policy. From the Roles & permissions UI, instead of public, we can select the roles to be one of the authenticated roles.
yes, once the access is restricted to authenticated roles, we need to make an authenticated request.
I hope you find the links useful. In case of queries, please feel free to ask.
I am getting the output in results, pagination format. Now I want to transform this to the default data, attributes format since the output of custom API is different from others.
this.transformResponse is not working in strapi new version.
Looking forward to the solution. I am stuck with this
Hi, there is a way to format the custom API response to the format from the content type endpoints that wish to include the data and meta objects for pagination?
At first I thought this was a typo in the code snippet as strapi appears to be an undefined global variable. But then I saw the same thing repeated in other docs and realized that, lo and behold, this just works:
export default {
async index(ctx, next) {
console.log(strapi); // Strapi { ... }
// called by GET /hello
ctx.body = "Hello World!"; // we could also send a JSON
},
};
Can somebody point me to any documentation for this magic global object?
I am just diving into Strapi and this really confused me. Especially since strapi is provided as an argument to the callback function when you use a factory to create a core controller – is that the same strapi that I logged above? why is it provided in a callback for core controllers but not custom ones?
Hi @vivmagarwal I was searching for creating custom api on meilisearch. Thankfully today I got this thread and checked your plugin. Is it possible to integrate custom api with meilisearch, so that instead of using meilisearch sdk native, all data communication happens via api layer.
Outside that is magically available globally, not much I can add. But if you wanted to see all the methods that it has, you can do so by running Strapi via console command as follows.
yarn strapi console
Thaw will start Strapi in “interactive console” mode
Then you can type strapi and hit enter. It will spit out all methods that are available to you. A great way to explore the strapi object.
I believe it the future version of Strapi they are going to change it not to be a global object.