How to Create a Custom API Endpoint in Strapi

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.


This is a companion discussion topic for the original entry at https://strapi.io/blog/how-to-create-a-custom-api-endpoint-in-strapi

This is useful, is there a way I can lock this down so it’s only accessible by people logged in to the admin area?

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.

Hi Chrift, thanks for the question.

  • 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.

Update: We have a plugin now that attempts to give us a UI for some of the use cases: https://market.strapi.io/plugins/strapi-plugin-custom-api

Can you share the source code? I try to create a custom endpoint with a media type.When I get all return url images,

example

imageUrl:"https://domain.com/uploads/filename.jpg"

Thank you for this great tutorial. How do I change the design of the API endpoint from

[ 
{
*here are all the custom results*
}
]

to this structure:

{
"results": [ 
{
*here are all the custom results*
}
]
}

Looking forward to your answer!

from where did we get ```
console.log(‘pages-report’, strapi.service(‘api::pages-report.pages-report’)


should not this be 

api::postsReport.pages-report’

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

1 Like

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?

Example:

data [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
0 Object { id: 1, attributes: {…} }
1 Object { id: 2, attributes: {…} }
2 Object { id: 3, attributes: {…} }
3 Object { id: 4, attributes: {…} }
4 Object { id: 5, attributes: {…} }
5 Object { id: 6, attributes: {…} }
6 Object { id: 7, attributes: {…} }
7 Object { id: 8, attributes: {…} }
8 Object { id: 9, attributes: {…} }
9 Object { id: 10, attributes: {…} }
10 Object { id: 11, attributes: {…} }
11 Object { id: 12, attributes: {…} }
12 Object { id: 13, attributes: {…} }
13 Object { id: 14, attributes: {…} }
14 Object { id: 15, attributes: {…} }
15 Object { id: 16, attributes: {…} }
16 Object { id: 17, attributes: {…} }
17 Object { id: 54, attributes: {…} }
18 Object { id: 55, attributes: {…} }
meta Object { pagination: {…} }
pagination Object { page: 1, pageSize: 25, pageCount: 1, … }

Preferred to the extent the controller core from strapi factory or calling any object of function in strap not by plugins.

Thanks in advance