How to Create a Custom API with Strapi
Strapi makes it easier for developers to create and customize APIs, in this article we will walk through the process of crafting a customized API endpoint from the ground up.
Prerequisites
Basic knowledge of Javascript.
Basic Knowledge of REST APIs.
Basic understanding of Strapi.
Installing Strapi
The commands below will create a Strapi project folder named my-api in the current directory with the necessary dependencies for the Strapi project to function.
npx create-strapi-app@latest my-api
in case you use yarn as your package manager , you change the "npx" to "yarn"
Once the installation is complete, your browser will automatically open a new tab, in cases where the browser doesn't open, navigate to http://localhost:1337/admin/
Fill out the form to create the first administrator user of this Strapi application
Once you've successfully logged in click on the option that says " Create your first Content-type", After that, you'll see another button labeled "Create collection type ", here you'll need to enter a name for your collection type, Let use the name "products"
After that, head over to the "content-type builder" section, look for your collection type, which we named "product " and click on it, inside, you'll see an option that says "add fields" Click on it.
Here you will need to decide how you want to structure your data, for our example, we'll use a text data format. Enter the name "shoes" as the name of this field. Additionally, select a number data format for the next field and provide the name "price"
Now head over to the "content manager" section. and Find the collection type we made earlier and click on it. Inside, you'll see a button that says "new entry". Click on that button and fill in the details for the products you want to add to your collection
Setting User Permissions for the API
Head over to the "settings" section, which you can usually find in the admin panel.
Once there, click "user and permissions" and then "roles",
Choose "public".
Scroll down to "permissions" click the "product" collection type and check the boxes for "find" and "findOne."
This allows users to get all products or just one product from your database. Lastly, copy the API link shown on the side. And now your API is fully prepared for everyone to use.
Querying the API
There are two ways you can get to it, First, you can go straight to the web address in your browser: http://localhost:1337/api/products.
Alternatively, you can make a GET request using the vs code extension called REST client, and make a new file with the name "rest.http" This file is where you can play around and try things out
Generating a Basic API
The command npx strapi generate
begins an interactive session using the command line (CLI), if you got yarn
and Strapi is already installed on your computer, you can use yarn
instead of npx
command.
Navigate to your my-api
folder and run npx strapi generate
to create a custom API endpoint.
Select the API option and press enter
Type in a name for your API
I will call mine blog
and select N for "Is this API for a plugin"
This will generate the files below
You'll find these in the /src/api/blog
folder, this command creates a directory called blog inside the src/api
directory, which contains three directories called controllers, routes, and services.
Controllers and Routes
Controllers are files that hold a collection of methods that we call "actions".When a user asks for something using a specific path on a website, these actions are like the steps the website follows.
Navigate to the blog.js
file in the /src/api/blog/controllers
the directory.
This default controller file provides you with a template to define various actions that your API can perform, You would uncomment the example action and modify it according to your needs, adding to logic to handle the different API actions such as fetching data, creating data, updating data and deleting data, but for this example this will do just fine.
Routes files you get with Strapi help your API know which URLs correspond to different actions you've defined in the controller file.
Open up the blog.js
in /src/api/blog/routes
and uncomment the code
This routes files acts like a guide for your API, explaining what actions it should take when the visitors go to specific URLs. Now you can use the rest client extension to query your API and get responses.
References
GitHub Repository
Conclusion
In summary, the process of creating a custom API with Strapi involves installing the platform, defining the content structure, configuring permissions, and generating endpoints. This empowers developers to tailor their applications to their exact requirements, offering a flexible and efficient way to handle data and interactions. With the ability to control API behavior through controllers and routes, developers can ensure seamless user experiences and efficient data management.